Reputation: 71
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
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
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
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
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