如何在gridviewc中获取按钮ID和字段名称

时间:2015-08-21 09:34:44

标签: c# asp.net gridview

我有一个gridview,我在其中为每列创建dynamic控件。

例如:

  -----------------------------------------------------------------
       Name | Column 1 | Column 2|
   ------------------------------------------------------------------
    Country |  LB      |  LB     |
            |          |         |
   -------------------------------------------------------------------
     City   |  LB      |  LB     |
            |          |         |
   -------------------------------------------------------------------

注意:LB指的是链接按钮

我在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);
}

点击事件有任何方法可以找出该特定实例的Namelink button Id

void lb_Click(object sender, EventArgs e)
{
    //what to do to retrieve values
}

我的意思是,如果我点击第一行和第一列的LB,我想获得LB Idname,即国家

5 个答案:

答案 0 :(得分:2)

尝试:

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事件中:

void btnForTvDisplay_Click(object sender, EventArgs e)
 {
      LinkButton bt= sender as LinkButton ;
      string name=bt.CommandName;
     string id=bt.CommandArgument;
 }

答案 1 :(得分:0)

只需将发件人对象转换为按钮类型即可获取值:

void lb_Click(object sender, EventArgs e)
 {
      Button bt= sender as Button;
      string id=bt.Id;
     string text=bt.Text;
       //get more information...
 } 

答案 2 :(得分:0)

您应该为网格视图添加一个rowcommand按钮。 请参阅here

您可以在RowCommand Event

中获取所有预期值
protected void gridMembersList_RowCommand(object sender, GridViewCommandEventArgs e)

 {
     }

答案 3 :(得分:0)

   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");
        }
    }

答案 4 :(得分:0)

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();
  }
}