[英]How to pass CommandName of LinkButton in GridView to OnClick event
[英]Disable “onclick” event in Gridview LinkButton
我在gridview中得到了这个模板字段。
<asp:TemplateField HeaderText="PersonID">
<ItemTemplate>
<asp:LinkButton runat="server" ID="BtnPersonDashboard" Text='<%# Bind("PersonId") %>' OnClientClick='<%# "PopupPersonRecord("+ Eval("RefId") +","+Eval("MemberId")+");" %>' />
</ItemTemplate>
</asp:TemplateField>
此按钮工作正常,我只需要禁用按钮的回发。 我尝试在Gridview
的“ RowDatabound
”事件中执行以下操作:
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// disable the Person id link button post back
((LinkButton)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "return false;");
}
}
但是,我似乎根本无法获得对linkbutton的引用。 我试图在Debug中“观看” e.Row.Cells[1].Controls[0]
,它的值显示为“ Text = {1235}
” <-我的人ID。
我不确定为什么要为该控件获取“ Text
”值而不是“ Linkbutton
”。 任何想法可能是什么问题? 是否有其他建议可以禁用onclick
事件?
尝试这样的事情:
if (e.Row.RowType == DataControlRowType.DataRow)
{
// find the control on the row on gridView
var btnPersonDashboard = e.Row.FindControl("BtnPersonDashboard") as LinkButton;
// check if the control was found and disable it
if (btnPersonDashboard != null)
btnPersonDashboard.OnClientClick = "return false";
}
如果要禁用LinkButton的回发,只需要添加return false; 在OnClientClick中
<asp:LinkButton
...
OnClientClick='<%# "PopupPersonRecord("+ Eval("RefId") +","+Eval("MemberId")+"); return false;" %>' />
注意:如果执行上述方法,请不要在RowDataBound事件中添加"onclick"
。
只需在后面的代码中禁用它即可。
BtnPersonDashboard.Enabled = false;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.