0
votes

I have a webpage where I have a gridview. I have populated the gridview on page load event.

protected void Page_Load(object sender, EventArgs e)
{          
   if (!IsPostBack)
   {
      loadGridView();
   }
}

This is the load gridview method.

private void loadGridView()
{
   dataTable dt = getData(); // this function populates the data table fine.
   gridView1.dataSource = dt;
   gridview1.dataBind();
}

Now I have added linkButtons in one of the gridview columns in the RowDataBound event of the grid view.

protected void gvTicketStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
   LinkButton lb = new LinkButton();
   lb.Text = str1; // some text I am setting here
   lb.ID = str2;   // some text I am setting here
   lb.Click += new EventHandler(lbStatus_click);
   e.Row.Cells[3].Controls.Add(lb);
}

Finally This is the event Handler code for the link button click event.

private void lbStatus_click(object sender, EventArgs e)
{
    string str = ((Control)sender).ID;
    // next do something with this string
}

The problem is, the LinkButtons appear in the data grid fine, but the click event does not get execute. the control never reaches the event handler code. when I click the link button, the page simply gets refreshed. What could be the problem?

I have tried calling the loadGridView() method from outside the (!isPostBack) scope, but it did not help!

2

2 Answers

0
votes

Try to work with the "Command" property instead of Click event

LinkButton lnkStatus = new LinkButton();
lnkStatus.ID = string.Format("lnkStatus_{0}", value);
lnkStatus.Text = "some text here";                    
lnkStatus.CommandArgument = "value";
lnkStatus.CommandName = "COMMANDNAME";
lnkStatus.Command += new CommandEventHandler(lnkStatus_Command);

Otherwise, if my proposal doesn't satisfy you, you have to remove the !Postback on Page_Load event.

0
votes

you have to use OnRowCommand event of the GridView.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("LinkButton1")) //call the CommandArgument name here.
    {
       //code            
    }      
}