Reputation: 8078
I created a row (header row) in a gridview with a dropdownlist among other input controls. I also put a button on that row (fltbttn-see below). I created an addhandler for the button called fltbttn_Click. I want gain access to my dropdownlist (ddlscantype) using findcontrol but how can I get to the header row where the filter controls exist?
Dim fltbttn As New ImageButton
fltbttn.ImageUrl = "\images\bttnFilter.gif"
AddHandler fltbttn.Click, AddressOf fltbttn_Click
Dim cell As New TableCell
Dim row As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
Dim ddlscantype As New DropDownList
AddHandler ddlscantype.SelectedIndexChanged, AddressOf ddlscantype_Changed
ddlscantype.DataSource = SqlDataSource2
ddlscantype.DataValueField = "value"
ddlscantype.DataTextField = "name"
ddlscantype.DataBind()
row.Cells.Add(cell)
cell.Controls.Add(ddlscantype)
MARKUP
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate >
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataSourceID="SqlDataSource1" Font-Names="Estrangelo Edessa" Font-Size="Small"
ShowFooter="True"
Caption = '<table border="" width="100%" cellpadding="3" cellspacing="0" bgcolor="#4A3C8C"><tr><td style = "font-size:X-large;font-family:Arial CE;color:White"><b>Receiving Error Log</u></td></tr></table>' >
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<Columns>
<asp:BoundField DataField="scan" HeaderText="Scan" SortExpression="scan" />
<asp:BoundField DataField="ScanType" HeaderText="ScanType" ReadOnly="True"
SortExpression="ScanType" />
<asp:BoundField DataField="Vendor" HeaderText="ht" ReadOnly="True"
SortExpression="ht" />
<asp:BoundField DataField="Name" HeaderText="ht2" ReadOnly="True"
SortExpression="ht2" />
</Columns>
//footer style and pager style blah blah blah goes here.
</ContentTemplate>
</asp:UpdatePanel>
<ajaxToolkit:UpdatePanelAnimationExtender ID="upae" BehaviorID ="animation" runat="server" TargetControlID = "UpdatePanel1" >
<Animations >
<OnUpdating>
<Parallel duration="0">
<ScriptAction Script="onUpdating();" />
</Parallel>
</OnUpdating>
<OnUpdated>
<Parallel duration="0">
<ScriptAction Script="onUpdated();" />
</Parallel>
</OnUpdated>
</Animations>
Upvotes: 0
Views: 821
Reputation: 2640
Why don't you use the TemplateField type for one of your datacolumns, then use a templated header?
for example:
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Vendor") %>
</ItemTemplate>
<HeaderTemplate>
<asp:DropDownList ID="ddlscantype" runat="server" DataSourceID="SqlDataSource2" DataTextField="name" DataValueField="id" OnSelectedIndexChanged="YourCallbackMethod" />
</HeaderTemplate>
</asp:TemplateField>
This should allow you to fit the drop down inside of your header row, as well as not requiring any dynamic control creation.
Don't forget to wire up your event here...
Upvotes: 1
Reputation: 3938
Well the first thing that jumps out at me is that you've dynamically created your DropDownList...but you've created it within a function.
You have to keep in mind that variables/objects have scope. The scope of your DropDownList is within the function that you've declared it. This means that it will be added to the page and rendered but once it's finished, it's destroyed. When the user causes a postback to the server, the DropDownList no longer exists...and so the event will never fire.
You have to give the DropDownList a scope of the whole page. Declare it at the page level.
The other thing you have to keep in mind when using dynamic controls is the ASP Page Life Cycle.
The page is posted to the server...
The server creates all of the objects needed to do server calculations in the Page Init Event....
Right after the page Init Event the ViewState is loaded for the page...the ViewState is used to determine what event(s) caused the postback to happen.
If your DropDownList does not exist when the ViewState is loaded for the control, then the event will be lost and your code will not handle it.
My recommendation for you is to try to avoid using dynamic controls. Sometimes you Have to use them...but they are not easy to use.
I wrote a little article on using dynamic ASP.NET controls that may help you better understand.
-Frinny
Upvotes: 1