Server Control to manipulate GridView

J

Jeronimo Bertran

I have created some procedures to export the content of a gridView to a text
file. I am now trying to encapsulate all of this into a self contained
WebControl that basically behaves like an image button that when it is
clicked the export is performed.

I am not completely sure if the best approach is to define the control as a
PostBackEventHandler, but I created a WebControl defined as follows:

[ToolboxData("<{0}:GridViewExportButton runat=server></
{0}:GridViewExportButton>")]
public class GridViewExportButton : WebControl, IPostBackEventHandler


protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");
writer.AddAttribute(HtmlTextWriterAttribute.Name, base.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ImageURL);

base.AddAttributesToRender(writer);
}



My first problem has to do with being able to link the correct gridView to
the control from the properties. I tried the following:

[Bindable(true)]
public GridView Grid
{
get
{
return (GridView)ViewState["Grid"];
}
set
{
ViewState["Grid"] = value;
}
}

This works very well in the designer since it shows a dropdownbox that allows
me to assign one of the existing gridviews on the page to the control.
However, I get the following error when it is interpreted:

Cannot create an object of type GridView from it string representation.

<cc1:GridViewExportButton ID="GridViewExportButton1" runat="server"
Grid="GridView1" OnClick="GridViewExportButton1_Click" />


My second problem has to do with the postback handler, when the user clicks
the control I need to execute server code that use the gridView object.


Thanks,

Jeronimo Bertran
 
S

Steven Cheng[MSFT]

Hi Jeronimo,

Thank you for posting.

Regarding on the the custom server control for exporting gridview question,
I think you can develop a custom composite control which include a certain
linkbutton as inner nested control and in the linkbutton's postback event
do the gridview exporting work. Also, as for how to reference the GridView
in our custom control, I suggest you consider using the controlID to
reference GridView on the page. At runtime, our custom Control can use Page
or other container control's FindControl method to locate the GridView we
want to export. Also, this require that the GridView is either in the
page's top level control collection or within the same Namingcontainer of
our custom control, otherwise, it'll be very difficult to locate the
GridView from our custom control through FindControl.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

You're welcome Jeronimo,

Please feel free to post here when you need any assistance.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top