从标签中检索值会发送异常"Object reference not set to an instance of an object."



虽然有其他关于这个主题的重复帖子,但他们没有提供简单的答案,所以像我这样的新手程序员不理解

问题: 没有从gridview内的标签中获取文本值,以便我可以在我的数据库中更新该值。

源视图:

    <asp:Content ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="true"
AutoGenerateColumns="false"  PageSize="8" DataKeyNames="ID"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<%--asset_name as 'Asset Name'--%>
<asp:TemplateField HeaderText="Asset Name">
<ItemTemplate >
<asp:Label ID="labelAssetName" runat="server" Text='<%# Eval("Asset Name") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

代码:

    Label lb2=(Label)GridView1.Rows[e.RowIndex].FindControl("labelAssetName");//Asset_Name
   int assettype = 0;
    if (lb2.Text == "Clothes & Apparels")
        assettype = 1;
    else if (lb2.Text == "Stationeries")
        assettype = 2;
    else if (lb2.Text == "Furnitures")
        assettype = 3;
    else if (lb2.Text == "Electronics")
        assettype = 4;
    else if (lb2.Text == "Transportations")
        assettype = 5;
    else if (lb2.Text == "Communications")
        assettype = 6;

当我运行程序时,我得到一个异常,如下所示:

系统。

第97行:if (lb2;Text == "衣服&服装")

我在同一gridview内的文本框中获取值,但我无法获得标签值。

我将非常感谢任何帮助解释这个异常的原因和简单的解决方法。

请尝试为gridview添加EditItemTemplate,然后将labelAssetName标签添加到该模板,然后尝试在RowUpdating事件中获取它。我认为目前你只有itemtemplate。如果这不能解决你的问题,请告诉我。

也请看看这个片段,如果你可以。这可能有帮助。

http://www.aspsnippets.com/Articles/GridView-RowUpdating-event-example-in-ASPNet-using-C-and-VBNet.aspx

如果您还有问题。请让我知道。

相关内容

最新更新