Find GridView row ID of the row in which the dropdownlist selected item changed.

MMarlin7
MMarlin7 used Ask the Experts™
on
In the event of a dropdownlist selecteditem change, i want to do a database update with the new value of the dropdownlist. For that update i need a where clause which uses the productid in the same row as the changed dropdownlist. How do i find the productid label of the same row as the dropdownlist?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Obadiah ChristopherDeveloper User Interface
Commented:
In the Row Command u'll have to write something like this


GridViewRow gvRow = (GridViewRow)((DropDownList)e.CommandSource).Parent.Parent;

Open in new window

Commented:
did your row command fires on selection index changes of drodown under gridview ?
Commented:
this simple application will help you.
/* ASPX */
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
           >
           <Columns>
            <asp:BoundField DataField="ID" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
                        >
                            <asp:ListItem Text="test" Value="test"></asp:ListItem>
                            <asp:ListItem Text="test1" Value="test1"></asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
           </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
 
/* Code behind default.aspx.cs */
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            DataRow dr = dt.NewRow();
            dr["ID"]  = "1";
            dt.Rows.Add(dr);
 
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
 
 
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drp = sender as DropDownList;
        if (drp != null)
        {
            GridViewRow row = drp.NamingContainer as GridViewRow;
            // now access your data over here from row.
        }
    }
}

Open in new window

Author

Commented:
Thanks for the quick input. I will split the points. Here's how i solved the challenge:
GridViewRow GridView1Row = (GridViewRow)ddl_Owner1.NamingContainer; //casting
Label lblCommitID = (Label)GridView1Row.FindControl("lblCommitID");
string strCommitID = lblCommitID.Text;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial