尝试访问网格视图中的文本框值,给出异常

时间:2017-09-20 07:09:02

标签: c# asp.net .net gridview

我试图根据同一行中的其他值禁用GridView中的文本框,但是当我尝试设置enable属性时,我会得到一些空引用异常。

以下是我的 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;
        }
    }
}

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

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

1 个答案:

答案 0 :(得分:2)

你的TextBox在EditTemplate中,所以试试这个:

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