[英]Linkbutton click event does not work inside gridview
I have a webpage where I have a gridview. 我有一个网页,其中有gridview。 I have populated the gridview on page load event.
我已经在页面加载事件中填充了gridview。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadGridView();
}
}
This is the load gridview method. 这是加载gridview方法。
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. 现在,我在网格视图的RowDataBound事件的gridview列之一中添加了linkButtons。
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. 问题是,LinkButtons很好地出现在数据网格中,但是click事件没有得到执行。 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! 我尝试从(!isPostBack)范围之外调用loadGridView()方法,但没有帮助!
Try to work with the "Command" property instead of Click event 尝试使用“ Command”属性而不是Click事件
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. 否则,如果我的建议不能使您满意,则必须删除!Postback on Page_Load事件。
you have to use OnRowCommand event of the GridView. 您必须使用GridView的OnRowCommand事件。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("LinkButton1")) //call the CommandArgument name here.
{
//code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.