Learner
Learner

Reputation: 71

How to get button Id and the field name in gridviewc

I have a gridview in which I'm creating dynamic controls for each column.

For ex:

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

Note: LB refers to Link Button

I'm generating the buttons as follows in RowCreated Event

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

On the click event is there any way to find out the Name and the link button Id of that particular instance

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

I mean if I click the LB of first row and first column I want to get the LB Id and the name i.e., Country

Upvotes: 2

Views: 974

Answers (5)

Apurva Pawar
Apurva Pawar

Reputation: 11

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

Upvotes: 0

Apurva Pawar
Apurva Pawar

Reputation: 11

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

Upvotes: 0

Slashy
Slashy

Reputation: 1881

Simply convert the sender object to a button type and get the values:

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

Upvotes: 0

A_Sk
A_Sk

Reputation: 4630

Try:

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

And in Click Event:

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

Upvotes: 2

Dah Sra
Dah Sra

Reputation: 4425

You should probably add a rowcommand button for the grid view. Please refer here

and you can get all the expected values in RowCommand Event

protected void gridMembersList_RowCommand(object sender, GridViewCommandEventArgs e)

 {
     }

Upvotes: 0

Related Questions