如何获取DataView控件的行号?
我在DataView中创建了模板列,并指定为linkbutton,现在希望通过点击linkbutton获取这一行上的某个值,但是不知道如何获取行号,请指教Label lb = (Label)(GridMain.Rows[1].FindControl("LabelID"))
其中GridMain是dataview的实例,现在这句话可以获取第一行的LabelID的值,但是实际上不一定会点击哪一行,怎么把那个“1”变成动态获取的行号呢? --------------------编程问答-------------------- dataview不是个类吗?怎么会有模板呢?
使用的是什么控件显示的数据呢? --------------------编程问答-------------------- 不好意思,写错了,是gridview,我想获取当前操作的行号 --------------------编程问答-------------------- 那你可以在RowCommand事件里来写啊,使用e.RowIndex就是行号了。
或者其它的事件里,查看一下参数e是否存在rowIndex,存在的话使用就是了。 --------------------编程问答-------------------- 但是我现在写e.后,没有rowindex这个属性,是不是需要增加什么引用? --------------------编程问答-------------------- dataview.DataKeys[e.Item.ItemIndex].ToString() 试一下 --------------------编程问答-------------------- 那你就使用CommandArgument 参数来记录行数。 --------------------编程问答-------------------- 你可以在RowDataBound事件里通过e.Row.RowIndex来给控件的CommandArgument设置值。这样方便了在RowCommand事件里通过CommandArgument来得到所在的行号。 --------------------编程问答-------------------- 看来晚了,ls正确的 --------------------编程问答-------------------- 感觉 还是 不行 ,能否帮忙提供两行代码示例,谢谢了 --------------------编程问答-------------------- 不会写,真不好意思 --------------------编程问答-------------------- ls正解,用CommandArgument可以绑定一些需要的数据,如果于行有关系可以在RowDataBound时间中处理,如果只是简单的在数据库中读取,那么可以直接绑定 --------------------编程问答-------------------- protected void MyGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
}
这样吗 --------------------编程问答--------------------
--------------------编程问答-------------------- 那这个id怎么传出来给 Label lb = (Label)(GridMain.Rows[id].FindControl("LabelID")) 使用呢?
protected void grdBooks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
LinkButton lbl= e.Row.FindControl("LabelID") as LinkButton;
if (lbl != null)
{
lbl.CommandArgument = e.Row.RowIndex.ToString();
}
}
protected void grdBooks_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "xxx" && e.CommandArgument != null)
{
int rowIndex = int.Parse(e.CommandArgument);
}
}
--------------------编程问答-------------------- 这样也可以获取行号:
在GridView中的RowCommand事件中
Control cmdControl = e.CommandSource as Control;
GridViewRow cmdRow = cmdControl.NamingContainer as GridViewRow;
cmdRow.RowIndex --这个就是行号 --------------------编程问答-------------------- 高人一堆啊,来晚了,向高人学习 --------------------编程问答-------------------- 高手太多了,向高手致敬
补充:.NET技术 , ASP.NET