簡體   English   中英

C# TextBox.text 返回 null

[英]C# TextBox.text returns null

我正在嘗試為網頁制作一個編輯器。 但是,每當我嘗試從 gridview 中的文本框中訪問文本時,我都會收到錯誤消息“對象引用未設置為 object 的實例”。 我的代碼如下。

 SqlConnection con = new SqlConnection(DatabaseClient.ConnectionString);
            try
            {
            int id = (int)CompoundTable.DataKeys[e.RowIndex].Value;



            TextBox name = CompoundTable.Rows[e.RowIndex].FindControl("name") as TextBox;
                TextBox cat = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("cation_quantity");
                TextBox catname = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("cation");
                TextBox an = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("anion");
                TextBox anName = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("anion_quantity");
                TextBox diff = (TextBox)CompoundTable.Rows[e.RowIndex].FindControl("difficulty_level");

                con.Open();
                SqlCommand command = new SqlCommand("UPDATE Compound SET compound_name='" + name.Text  + "' WHERE compound_name = @compound",con); //The line that the error is thrown.
                command.Parameters.Add("@compound", SqlDbType.NVarChar, CompoundName.Text.Trim().Length).Value = CompoundName.Text.Trim();
                command.ExecuteNonQuery();
                con.Close();


                CompoundTable.EditIndex = -1;
                BindGridView();
            }
<asp:GridView ID="CompoundTable" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" PageSize="20" OnSorting="CompoundTable_Sorting" OnPageIndexChanging="CompoundTable_PageIndexChanging" OnRowCancelingEdit="CompoundTable_CancelEdit" OnRowEditing="CompoundTable_Edit" OnRowUpdating="CompoundTable_Update" CellPadding="4">
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="Compound" SortExpression="name" />
                    <asp:BoundField DataField="name" HeaderText="Compound" SortExpression="name" />
                    <asp:BoundField DataField="cation" HeaderText="Cation" SortExpression="cation" />
                    <asp:BoundField DataField="cation_quantity" HeaderText="Cation Quantity" SortExpression="cation" />
                    <asp:BoundField DataField="anion" HeaderText="Anion" SortExpression="anion" />
                    <asp:BoundField DataField="ANION_QUANTITY" HeaderText="Anion Quantity" SortExpression="anion" />
                    <asp:BoundField DataField="difficulty_level" HeaderText="Difficulty" SortExpression="difficulty_level" />
                    <asp:HyperLinkField DataNavigateUrlFields="compound_id" DataNavigateUrlFormatString="DeleteCompound.aspx?compound={0}" Text="Delete Compound" />
                     <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="LBEdit" runat="server"  CommandName="Edit" >Edit</asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="LBCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                <asp:LinkButton ID="LBUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
            </EditItemTemplate>
        </asp:TemplateField>
                </Columns>
            </asp:GridView>

查看CompoundTable.Rows[e.RowIndex].FindControl("name")返回的內容。 我不太了解 webforms,但快速查看文檔表明BoundField沒有繼承自TextBox

當您使用as進行轉換時,如果轉換失敗,則不會引發異常,但返回的 object 是 null。您在嘗試訪問 null 上的屬性時遇到異常 object。

因為你的name.Text是null。

如果您的目標是通過單擊更新按鈕來更新 object,則可以通過以下簡單方法來完成:

                protected void YourGridIdHere_RowCommand(object sender, GridViewCommandEventArgs e)
                {
                    // If multiple buttons are used in a GridView control, use the
                    // CommandName property to determine which button was clicked.
                    if(e.CommandName=="Your command in CommandName")
                    {
                      // Convert the row index stored in the CommandArgument
                      // property to an Integer.
                      int index = Convert.ToInt32(e.CommandArgument);

                      // Retrieve the row that contains the button clicked 
                      // by the user from the Rows collection.
                      GridViewRow row = YourGridIdHere.Rows[index];

                      // Get data from current selected row.    
                      int ID_COL = 0; 
                      TableCell wantedData = row.Cells[ID_COL];
                      string str = wantedData.Text;

                      // So on....
                }

在表格 html 中,您可以通過添加以下內容來運行它:

OnRowCommand="YourGridIdHere_RowCommand"

給你的:

<asp:GridView ID="CompoundTable" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" PageSize="20" OnSorting="CompoundTable_Sorting" OnPageIndexChanging="CompoundTable_PageIndexChanging" OnRowCancelingEdit="CompoundTable_CancelEdit" OnRowEditing="CompoundTable_Edit" OnRowUpdating="CompoundTable_Update" CellPadding="4">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM