Click here to Skip to main content
15,891,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. . .

How to find the drop down selected index changed event in a grid view ?

This is my sample code

<asp:Gridview runat="server" id="grid1">

<columns>
XML
<asp:TemplateField HeaderText="Number">

             <ItemTemplate>
               <asp:DropDownList   ID="dropColor" runat="server"   AutoPostBack="true" OnSelectedIndexChanged="DropColor_InGridview_SelectedIndexChangedEvent">

                 <asp:ListItem Text="Zero" Value="0"></asp:ListItem>
                 <asp:ListItem Text="One" Value="1"></asp:ListItem>
                 <asp:ListItem Text="Two" Value="2"></asp:ListItem>
                 <asp:ListItem Text="Three" Value="3"></asp:ListItem>

               </asp:DropDownList>
             </ItemTemplate>


In code behind i wrote like this


C#
protected void DropColor_InGridview_SelectedIndexChangedEvent(object s,EventArgs e)
   {
       Response.Write("Drop down fired");

   }
Posted

First, make sure that EnableViewState of that GridView to false, and check if it works.

Refer following thread:
Raising DropDownList SelectedIndexChanged From GridView Control[^]

Here are some similar discussions on same:
DropDownList SelectedIndexChanged within Gridview[^]
Assign an event to a custom control[^]
 
Share this answer
 
protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = (DropDownList)gvUserInfo.FindControl("yourControlName");
string ddlValue = ddl.SelectedItem.Value;
}


OR (
XML
Suppose if we have controls in each row of gridview then we need find each row of controls for that we need to write a code like this

<pre lang="cs">protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl(&quot;ddlCity&quot;);
int CountryId = Convert.ToInt32(ddl.SelectedItem);
}
}</pre>




)
 
Share this answer
 
Yes Sumit i have written like this but every time i am getting "zero" what is the
solution can you help me ?
 
Share this answer
 
hi kulkarni by putting enableviewstate=false drop down is firing
 
Share this answer
 
Comments
Prasad_Kulkarni 15-Oct-12 2:52am    
Well, that's great. So what next?
You can achieve this by setting AuotoPostback Property to true
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900