gridview中有一列模板列是dropdownlist,我想要在这个dropdownlist的SelectedIndexChanged事件中获取gridview当前行的的key来修改数据库表中相应的记录要怎么做

解决方案 »

  1.   


    foreach (GridViewRow gr in GridView1.Rows)
            {
               int au_id = Convert.ToInt32(GridView1.DataKeys[gr.DataItemIndex].Value.ToString().Trim());
            }
      

  2.   

    你可以在一莫板列中的加一HiddenField 并绑定该KEY,然后去找到该控件.....
      

  3.   

    foreach (GridViewRow gr in GridView1.Rows)
            {
               int au_id = Convert.ToInt32(GridView1.DataKeys[gr.DataItemIndex].Value.ToString().Trim());
            }
    这个好象不行吧,获取的应该是最后一行的DataKeys
    我是在dropdownlist的SelectedIndexChanged事件里写的
      

  4.   

    你可以在一莫板列中的加一HiddenField 并绑定该KEY,然后去找到该控件..... 在SelectedIndexChanged事件里怎么找到HiddenField ..没有当前行的行号
      

  5.   

    1.在GridView的RowDataBound事件中给每个DropDownList赋行号,放在TabIndex中:
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                ((DropDownList)e.Row.Cells[4].FindControl("DropDownList1")).TabIndex = Convert.ToInt16(e.Row.RowIndex);
        }在DropDownList的SelectedIndexChanged事件中获的结果(DropDownList的AutoPostBack设置true
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList myD = (DropDownList)sender;
            Response.Write(GridView1.DataKeys[myD.TabIndex].Value.ToString());
        }
      

  6.   


    可以根据DropDownList 的ClientID 替换掉ID为HiddenField的ID找到服务端的IDprotected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 

        DropDownList myD = (DropDownList)sender; 
        HiddenField hnd = (HiddenField )GridView.FindControl(myD.ClientID.Replace("DropDownListID","HiddenField");
        .....
    }
      

  7.   

    不论DropdownList放在什么容易内部,我们都可以抛开对容器的纠缠而方便地设计取得一些附加信息。演示两种在SelectedIndexChanged中可以取得附加信息的做法。首先,在设计时可以明确地为DropDownlist自身绑定附加信息:<asp:DropDownList runat="server" ID="myList" DataSource='<%# this.GetRandomString((string)Eval("a_field")) %>'
        这一行的key='<%# Eval("key_field") %>' OnSelectedIndexChanged="myList_Selected" AutoPostBack="true" />
    在事件处理方法中就可以取得它:protected void myList_Selected(object sender, EventArgs args)
    {
        DropDownList dr = sender as DropDownList;
        string key = dr.Attributes["这一行的key"];
        .......
    }这跟DropDowList放在什么容器里并没有关系,所以它是最可靠和清晰的做法。其次,假设同一行中同一模板或者其它模板还有别的内容,例如一个id为Label3的Label(其它类型的控件同理),则可以直接写DropDownList dr = sender as DropDownList;
    string key = (dr.FindControl("Label3") as Label).Text;
      

  8.   

    我在xhtml代码中声明了“这一行的key”(你可以随便设置任何名称的自定义属性),从数据源绑定值,然后就可以在代码中读取这个属性。为控件声明自定义属性,是个非常基本的概念,它在一定程度上类似于ViewState的一些重要作用,但是在csdn上竟然奇怪地几乎看不到这个概念。
      

  9.   

    你可以设置个隐藏控件,然后绑定数据源的时候给dropdownlist加onchange事件,把这一行的id给隐藏控件,然后操作dropdownlist的时候读取出这个隐藏控件的值

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList dr = (DropDownList)e.Row.FindControl("DropDownList1");
                dr.Attributes.Add("onchange", "document.getElementById('TextBox1').value='" + e.Row.Cells[1].Text + "';");
            }
        }protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            this.Response.Write(Request.Form["TextBox1"]);
        }