我想禁用行数据绑定(bind)中的按钮。当其文本或值为“等待批准”时。我收到这个错误。对象引用未设置为对象的实例。//button.Enabled = true;
protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
Button button = (Button)e.Row.FindControl("btnReportedlink");
string Id =((DataRowView)e.Row.DataItem)["ReportLinks"].ToString();
if (Id == "Waiting for Approval")
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}
我的 aspx
<asp:TemplateField HeaderText="Reportd Link" ItemStyle-HorizontalAlign="center" >
<ItemTemplate>
<button onclick="window.open('<%#Eval("ReportLinks")%>', '_blank');" title='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"> Link</button>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
最佳答案
为什么要使用 HTML <button />
元素 ?使用 <asp:button />
来自 asp.net 的 Web 服务器控件可以更好地控制读取和禁用服务器控件。
使用 OnClientClick
属性以指定在引发 Button 控件的 Click 事件时执行的其他客户端脚本。
<ItemTemplate>
<asp:button onclientclick="javascript:window.open('<%#Eval("ReportLinks")%>', '_blank');"
text='<%#Eval("ReportLinks")%>' id="btnReportedlink" runat="server"/>
</ItemTemplate>
通过以上设置,现在您将能够访问 button
在没有更多对象引用错误的行数据绑定(bind)事件中。
关于c# - 如何禁用 gridview 中的行数据绑定(bind)事件中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33123590/