foreach (GridViewRow gr in GridView1.Rows) { int au_id = Convert.ToInt32(GridView1.DataKeys[gr.DataItemIndex].Value.ToString().Trim()); }
你可以在一莫板列中的加一HiddenField 并绑定该KEY,然后去找到该控件.....
foreach (GridViewRow gr in GridView1.Rows) { int au_id = Convert.ToInt32(GridView1.DataKeys[gr.DataItemIndex].Value.ToString().Trim()); } 这个好象不行吧,获取的应该是最后一行的DataKeys 我是在dropdownlist的SelectedIndexChanged事件里写的
foreach (GridViewRow gr in GridView1.Rows)
{
int au_id = Convert.ToInt32(GridView1.DataKeys[gr.DataItemIndex].Value.ToString().Trim());
}
{
int au_id = Convert.ToInt32(GridView1.DataKeys[gr.DataItemIndex].Value.ToString().Trim());
}
这个好象不行吧,获取的应该是最后一行的DataKeys
我是在dropdownlist的SelectedIndexChanged事件里写的
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());
}
可以根据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");
.....
}
这一行的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;
如
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"]);
}