Click here to Skip to main content
15,914,109 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
My code as follows

C#
protected void gvPkgcbndate_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
         LinkButton lnk = new LinkButton();
         lnk.Text = "click here";
         e.Row.Cells[1].Controls.Add(lnk);
         lnk.Click +=new EventHandler(lnk_Click);
         lnk.Attributes.Add("onClick", "return false;");
   }

protected void lnk_Click(object sender, EventArgs e)
{

 var exist = lbllist.Items.FindByValue(gvPkgcbndate.SelectedRow.Cells[2].Text);
    string[] smsg = gvPkgcbndate.SelectedRow.Cells[2].Text.Split('-');
    string txt1 = smsg[1].ToString().Trim();
    lbllist.Items.Add(txt1.Substring(0, txt1.Length - 2));
}

Dynmaically i created link button click event in the asp.net page.

But this event is not firing.

what is the problem in my above code.
Posted
Updated 25-Jul-15 7:26am
v2
Comments
Sergey Alexandrovich Kryukov 25-Jul-15 1:00am    
How could "return false" possible work? Return from what? How it could affect any behavior?
—SA
[no name] 25-Jul-15 1:47am    
You would think that after years of getting people to do your work for you, you would have at least learned how to format your code.

C#
protected void gvPkgcbndate_RowDataBound(Object sender, GridViewRowEventArgs e)
{
   LinkButton lnk = new LinkButton();
   lnk.Text = "click here";
   //set unique id for your dynamic link button 
   lnk.ID = "btn" + e.Row.RowIndex.ToString();
   e.Row.Cells[1].Controls.Add(lnk);
   lnk.Click +=new EventHandler(lnk_Click);
}
 
Share this answer
 
v2
Hello,

C#
lnk.Attributes.Add("onClick", "return false;"); // this code stopping linkbutton to fire.


and set the ID:

C#
protected void gvPkgcbndate_RowDataBound(Object sender, GridViewRowEventArgs e)
{
LinkButton lnk = new LinkButton();
lnk.ID = "lnk"
lnk.Text = "click here";
e.Row.Cells[1].Controls.Add(lnk);
lnk.Click +=new EventHandler(lnk_Click);
lnk.Attributes.Add("onClick", "return false;");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900