如何获得按钮Id和字段名在gridview
本文关键字:gridview 字段 何获得 按钮 Id | 更新日期: 2023-09-27 18:09:08
我有一个gridview,我正在为每个列创建dynamic
控件。
-----------------------------------------------------------------
Name | Column 1 | Column 2|
------------------------------------------------------------------
Country | LB | LB |
| | |
-------------------------------------------------------------------
City | LB | LB |
| | |
-------------------------------------------------------------------
注: LB指Link Button
我在RowCreated
事件
for(int i = 0; i < 4; i ++)
{
LinkButton lb = new LinkButton();
lb.Click += btnForTvDisplay_Click;
lb.ID = lb + i;
lb.Text = "Save";
e.Row.Cells[rowIndex].Controls.Add(btnForTvDisplay);
}
在click事件中是否有办法找出特定实例的Name
和link button Id
void lb_Click(object sender, EventArgs e)
{
//what to do to retrieve values
}
我的意思是,如果我点击LB的第一行和第一列我想得到LB Id
和name
即,Country
尝试:
for(int i = 0; i < 4; i ++)
{
LinkButton lb = new LinkButton();
lb.Click += btnForTvDisplay_Click;
lb.ID = lb + i;
lb.CommandName="Name";
lb.CommandArgument=i;
lb.Text = "Save";
e.Row.Cells[rowIndex].Controls.Add(btnForTvDisplay);
}
和Click
Event:
void btnForTvDisplay_Click(object sender, EventArgs e)
{
LinkButton bt= sender as LinkButton ;
string name=bt.CommandName;
string id=bt.CommandArgument;
}
您可能应该为网格视图添加行命令按钮。请参考此处
,您可以在RowCommand Event
中获得所有期望的值protected void gridMembersList_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
只需将sender对象转换为按钮类型并获取值:
void lb_Click(object sender, EventArgs e)
{
Button bt= sender as Button;
string id=bt.Id;
string text=bt.Text;
//get more information...
}
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = (TextBox)e.Row.FindControl("txtTaskName");
DropDownList ddlTaskStatus = (DropDownList)e.Row.FindControl("ddlTaskStatus");
Label lblSerialNoCat1 = (Label)e.Row.FindControl("lblSerialNoCat1");
}
}
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_ID = (Label)e.Row.FindControl("lbl_ID");
string Id = lbl_ID.Text.Trim();
}
}