簡體   English   中英

如何在gridview中禁用行數據綁定事件中的按鈕?

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

我想禁用行數據綁定中的按鈕。 當其文本或值為“等待批准”時。 我得到這個錯誤。 對象引用未設置為對象的實例.// 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.net的<asp:button /> web服務器控件來更好地控制讀取和禁用服務器控件。

使用OnClientClick屬性指定在引發Button控件的Click事件時執行的其他客戶端腳本。

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

通過上面的設置,現在您將能夠訪問行數據綁定事件中的button而不會出現更多對象引用錯誤。

使用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