简体   繁体   English

如何在gridview中禁用行数据绑定事件中的按钮?

[英]How to disable button in row databound event in gridview?

i want to disable button in row data bound . 我想禁用行数据绑定中的按钮。 when its text or value is 'Waiting for Approval'. 当其文本或值为“等待批准”时。 im getting this error . 我得到这个错误。 Object reference not set to an instance of an object.// button.Enabled = true; 对象引用未设置为对象的实例.// 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;
        }

    }

my aspx 我的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>

Why are you using the HTML <button /> element ? 为什么使用HTML <button />元素? use <asp:button /> web server control from asp.net for a better control over reading and disabling the server controls. 使用来自asp.net的<asp:button /> web服务器控件来更好地控制读取和禁用服务器控件。

Use the OnClientClick property to specify additional client-side script that executes when a Button control's Click event is raised. 使用OnClientClick属性指定在引发Button控件的Click事件时执行的其他客户端脚本。

<ItemTemplate>
   <asp:button onclientclick="javascript:window.open('<%#Eval("ReportLinks")%>', '_blank');"
        text='<%#Eval("ReportLinks")%>'   id="btnReportedlink" runat="server"/>
</ItemTemplate>

With the above setup, now you will be able to access the button in row data bound event with NO more object references error. 通过上面的设置,现在您将能够访问行数据绑定事件中的button而不会出现更多对象引用错误。

use DataBinder work fine 使用DataBinder工作正常

protected void GridCategoryWise_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType != DataControlRowType.DataRow)
        {
            return;
        }

        Button button = (Button)e.Row.FindControl("btnReportedlink");

    string Id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ReportLinks"));

        if (Id == "Waiting for Approval")
        {
            button.Enabled = false;
        }
        else
        {
            button.Enabled = true;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM