473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Raising an event of a control inside Templatefield within GridView

Nam
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>

Jul 4 '06 #1
1 10419
Nam
I got the answer to my own question from the following article:

http://webthingsconsidered.blogspot....idview-or.html

Thanks,
Nam

"Nam" wrote:
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>
Jul 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: huzz | last post by:
I am trying to access a DropDownList control inside a repeater using ItemCommand as shown below but for some reason i can't access the DropDownList. When i step through the debug i get <undefine...
4
by: vatech1993 | last post by:
I'm stumped on this problem. I've created a user control that dynamically creates 5 linkbuttons in the CreateChildControls method. Each of these child controls is linked to a commandeventhandler,...
1
by: waihung.wee | last post by:
Hi I got the following error in my xxx.cs file (code behind file) According to the error message, I should add a form tag in the file. But form tag should not appear in a code behind file ...
2
by: ccbalapatel | last post by:
Hi, We have recently converted our code to ASP.NET 2.0. We have an user control that is embedded inside the page. The user control has a link button and the event handler for the link button is...
14
by: Mike | last post by:
I have a gridview that has a button in the last cell of each row. I want to get the text from cell 1 for that row the button was clicked on. So if my gridview looks like this: N-12 BMW ...
1
by: ajrock2000 | last post by:
I am using a DetailsView to display some data. I am basically getting a firstname, and lastname fields from a database (the database cannot be changed, nor the queries), and I must concatenate the ,...
1
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
How can I use Findcontrol inside a master gridview's RowDatabound event to search for another gridview (detail) control that is inside an ASP Panel, in a template column of the master gridview?
3
by: remohy1 | last post by:
i cant control properties of the control inside the gridview lets give an example when i put a button in a itemplate in gridview and write code protected void Button4_Click(object sender,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.