Raising an event of a control inside Templatefield within GridView

G

Guest

I have a DropDownList control in a TemplateField inside my GridView
How do I access this control’s SelectedIndexChanged event in codeBehind
page? I don’t see the ID “DropDownList1†of this control in the list of
objects in odeBehind page.

GridView1 and DropDownList1 are bound to TWO DIFFERENT tables (DataSources).
The last column of each row in the GridView is shown as a DropDownList. I
want to use the SelectedIndexChanged event of DropDownList1 to capture the
valued selected in the dropdown by the user.

Following is the sample code from aspx file:

Thanks,
Nam

<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="filmTypeID"
DataSourceID="SqlDataSource1" CellPadding="4"
ForeColor="#333333" GridLines="None">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="filmTypeID"
HeaderText="filmTypeID" InsertVisible="False"
ReadOnly="True" SortExpression="filmTypeID" />
<asp:BoundField DataField="filmType" HeaderText="filmType"
SortExpression="filmType" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2"
DataTextField="answer" DataValueField="answerID"
Enabled="false"</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2"
runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT CustomerID, CompanyName
FROM Customers">
<SelectParameters>
<asp:parameter DefaultValue="7"
Name="questionID" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT LastName FROM
Employees"></asp:SqlDataSource>
<br />
<asp:Label ID="Label1" runat="server" Text="test"
Width="160px"></asp:Label></div>
</form>
</body>
 

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

Top