Who is online? 112 guests and 0 members
Member login | Become a member
home » articles » Access dropdownlist inside the gridview
12/10/2009 by dpkngm
Hi Friends,Many times we came cross the requirement when we need to access the dropdownlist's selectedindexchanged event inside the gridview, like in a shopping cart changing the item's quantity or binding the other dropdown based on the first dropdown.In such a case we just need to access the controls that has fired the event and this can be done just by using this line GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;I have written an example too for the same reuirement, and you may check it below. <asp:GridView ID="GV" runat="server" AutoGenerateColumns="false" OnDataBound="GV_DataBound" OnRowDataBound="GV_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblId" runat="server" Text='<%#Eval("id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("price") %>'></asp:Label> <asp:Label ID="lblOriginalPrice" runat="server" Text='<%#Eval("price") %>' Visible="false"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:DropDownList ID="ddlQty" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlQty_SelectedIndexChanged"> <asp:ListItem Text="1" Value="1"></asp:ListItem> <asp:ListItem Text="2" Value="2"></asp:ListItem> <asp:ListItem Text="3" Value="3"></asp:ListItem> <asp:ListItem Text="4" Value="4"></asp:ListItem> <asp:ListItem Text="5" Value="5"></asp:ListItem> <asp:ListItem Text="6" Value="6"></asp:ListItem> <asp:ListItem Text="7" Value="7"></asp:ListItem> <asp:ListItem Text="8" Value="8"></asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent; DropDownList ddlQty = (DropDownList)gr.FindControl("ddlQty"); if (ddlQty != null) { Label lblPrice = (Label)gr.FindControl("lblPrice"); Label lblOriginalPrice = (Label)gr.FindControl("lblOriginalPrice"); int price = Convert.ToInt32(lblOriginalPrice.Text); int NewPrice = price * Convert.ToInt16(ddlQty.SelectedValue); lblPrice.Text = Convert.ToString(NewPrice); } }Happy Coding........:)
Hi Friends,
Many times we came cross the requirement when we need to access the dropdownlist's selectedindexchanged event inside the gridview, like in a shopping cart changing the item's quantity or binding the other dropdown based on the first dropdown.
In such a case we just need to access the controls that has fired the event and this can be done just by using this line
GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;
I have written an example too for the same reuirement, and you may check it below.
<asp:GridView ID="GV" runat="server" AutoGenerateColumns="false" OnDataBound="GV_DataBound" OnRowDataBound="GV_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblId" runat="server" Text='<%#Eval("id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("price") %>'></asp:Label> <asp:Label ID="lblOriginalPrice" runat="server" Text='<%#Eval("price") %>' Visible="false"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:DropDownList ID="ddlQty" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlQty_SelectedIndexChanged"> <asp:ListItem Text="1" Value="1"></asp:ListItem> <asp:ListItem Text="2" Value="2"></asp:ListItem> <asp:ListItem Text="3" Value="3"></asp:ListItem> <asp:ListItem Text="4" Value="4"></asp:ListItem> <asp:ListItem Text="5" Value="5"></asp:ListItem> <asp:ListItem Text="6" Value="6"></asp:ListItem> <asp:ListItem Text="7" Value="7"></asp:ListItem> <asp:ListItem Text="8" Value="8"></asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent; DropDownList ddlQty = (DropDownList)gr.FindControl("ddlQty"); if (ddlQty != null) { Label lblPrice = (Label)gr.FindControl("lblPrice"); Label lblOriginalPrice = (Label)gr.FindControl("lblOriginalPrice"); int price = Convert.ToInt32(lblOriginalPrice.Text); int NewPrice = price * Convert.ToInt16(ddlQty.SelectedValue); lblPrice.Text = Convert.ToString(NewPrice); } }Happy Coding........:)
<asp:GridView ID="GV" runat="server" AutoGenerateColumns="false" OnDataBound="GV_DataBound" OnRowDataBound="GV_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblId" runat="server" Text='<%#Eval("id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblPrice" runat="server" Text='<%#Eval("price") %>'></asp:Label> <asp:Label ID="lblOriginalPrice" runat="server" Text='<%#Eval("price") %>' Visible="false"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:DropDownList ID="ddlQty" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlQty_SelectedIndexChanged"> <asp:ListItem Text="1" Value="1"></asp:ListItem> <asp:ListItem Text="2" Value="2"></asp:ListItem> <asp:ListItem Text="3" Value="3"></asp:ListItem> <asp:ListItem Text="4" Value="4"></asp:ListItem> <asp:ListItem Text="5" Value="5"></asp:ListItem> <asp:ListItem Text="6" Value="6"></asp:ListItem> <asp:ListItem Text="7" Value="7"></asp:ListItem> <asp:ListItem Text="8" Value="8"></asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow gr = (GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent; DropDownList ddlQty = (DropDownList)gr.FindControl("ddlQty"); if (ddlQty != null) { Label lblPrice = (Label)gr.FindControl("lblPrice"); Label lblOriginalPrice = (Label)gr.FindControl("lblOriginalPrice"); int price = Convert.ToInt32(lblOriginalPrice.Text); int NewPrice = price * Convert.ToInt16(ddlQty.SelectedValue); lblPrice.Text = Convert.ToString(NewPrice); } }
Comment (No HTML)
It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.
enter your email address: