简体   繁体   English

Linkbutton单击事件在GridView中不起作用

[英]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列之一中添加了linkBut​​tons。

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. 问题是,LinkBut​​tons很好地出现在数据网格中,但是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.

相关问题 在Gridview中未调用LinkBut​​ton事件 - LinkButton Event does not get called inside Gridview 如何在驻留在ASP.NET GridView中的ASP.NET LinkBut​​ton的click事件中显示消息? - How to disply message upon the click event of ASP.NET LinkButton residing inside ASP.NET GridView? 如何使用c#asp.net在LinkBut​​ton的click事件中获取gridview的DataKeyNames值 - How to get DataKeyNames value of gridview inside LinkButton's click event using c# asp.net gridview 内的链接按钮未触发 - Linkbutton inside a gridview not firing 如何在LinkBut​​ton Click事件的GridView中获取单元格值? 还是RowCommand? - How to get cell value in a GridView on LinkButton Click Event? Or RowCommand? Gridview弹出窗口未从与Linkbutton单击相对应的RowCommand事件中打开 - Gridview popup window not opening from RowCommand event corresponding to Linkbutton click 如何通过动态添加和捕获gridview内部的linkbutton事件 - how to add and catch event of linkbutton inside the gridview by dynamically 为什么GridView中存在于Updatepanel中的LinkBut​​ton没有触发OnClientClick事件? - Why LinkButton inside GridView which is present in Updatepanel not firing OnClientClick event? 无法在链接按钮单击事件内触发运行时单选按钮checkedchanged事件 - Cannot fire runtime radiobutton checkedchanged event inside linkbutton click event 在链接按钮上添加点击事件 - Add click event on linkbutton
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM