【问题标题】:try to access text box value inside grid view giving exception尝试访问网格视图中的文本框值给出异常
【发布时间】:2017-09-20 07:09:02
【问题描述】:

我试图根据同一行中的其他值禁用 GridView 内的文本框,但是当我尝试设置启用属性时,我如何获得空引用异常。

下面是我的 ASPX 代码:

 <asp:GridView ID="gvPRCertInfo" runat="server" AutoGenerateColumns="False" GridLines="None"
                    CellSpacing="1" CellPadding="1"
                    Width="100%" BorderWidth="0"
                    AllowSorting="True"
                    PageSize="30"
                    OnRowDataBound="gvPRCertInfo_RowDataBound"                        
                    CssClass="data responsive">
                    <Columns>
                        <asp:TemplateField HeaderText="Select" SortExpression="">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkCert" runat="server" /><input type="hidden" id="hdnCertId" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "CertId") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="CertificateID" HeaderText="Certificate ID" />
                        <asp:TemplateField HeaderText="OrderQuantity">
                            <EditItemTemplate>
                                <asp:TextBox ID="txtOrderQty" runat="server"
                                    Text='<%# Bind("OrderQty") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="AvailableQuantity">
                            <EditItemTemplate>
                                <asp:TextBox ID="txtAvaiableQty" runat="server"
                                    Text='<%# Bind("AvailableQty") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="RedeemQuantity">
                            <EditItemTemplate>
                                <asp:TextBox ID="txtRedeemQty" runat="server"></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField  DataField="Specification" HeaderText="specification" Visible ="false" />
                        <asp:BoundField  DataField="ActCertId" HeaderText="ActivatedCerts" Visible ="false" />
                    </Columns>
                    <EmptyDataRowStyle CssClass="AlternatingRowStyle" />
                    <HeaderStyle CssClass="HeaderStyle" HorizontalAlign="Center" />
                    <PagerSettings Visible="False" />
</asp:GridView>

这是我的 aspx.cs 代码:

 protected void gvPRCertInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string specifications = Convert.ToString(e.Row.Cells[5].Text);
        if(string.IsNullOrEmpty(specifications))
        {

            TextBox txtOrderQty = e.Row.FindControl("txtOrderQty") as TextBox;
            TextBox txtAvailableQty = e.Row.FindControl("txtAvaiableQty") as TextBox;
            TextBox txtRedeemQty = e.Row.FindControl("txtRedeemQty") as TextBox;
            txtOrderQty.Enabled = false; // getting error at here
            txtAvailableQty.Enabled = false;
            txtRedeemQty.Enabled = false;
        }

        string ActCertId  = Convert.ToString(e.Row.Cells[6].Text);
        if(string.IsNullOrEmpty(ActCertId))
        {
            CheckBox chkCert = (CheckBox)e.Row.FindControl("chkCert");
            chkCert.Enabled = false;
        }
    }
}

我不确定这段代码哪里做错了。

任何人都可以帮助解决这个问题,非常感谢我吗?

【问题讨论】:

    标签: c# asp.net .net gridview


    【解决方案1】:

    您的文本框在 EditTemplate 中,所以试试这个:

    if (e.Row.RowType == DataControlRowType.DataRow)
    { 
        if(e.Row.RowState == DataControlRowState.Edit)
        {
            // Here logic to apply only on rows in edit mode
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多