【问题标题】:TextBox in Gridview return null or 'System.Web.UI.WebControls.TextBox'Gridview 中的 TextBox 返回 null 或 'System.Web.UI.WebControls.TextBox'
【发布时间】:2018-11-22 17:31:54
【问题描述】:

我在从文本框中检索数据时遇到问题。 TextBox 位于 Gridview 中。我尝试调试,它显示我收集文本框值的变量返回nullSystem.Web.UI.WebControls.TextBox

你能帮我找出问题所在吗?

这是后端和gridview的代码

public void Button_Submit_Onclick(object sender, EventArgs e)
{
    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        //Textbox from Gridview
        TextBox txt_field = (TextBox)GridView2.Rows[i].Cells[12].FindControl("txt_Comment");
        //Connection string
        con.ConnectionString = ConfigurationManager.ConnectionStrings["TestDeductionsConnectionString2"].ToString();
        //id of column
        int recordid = Convert.ToInt32(GridView2.DataKeys[i].Values[0]);
        //checkbox value
        CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
        bool private1 = Convert.ToBoolean(cbox.Checked); 
        //open connection and creating sqlcommand
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = "Update DetailCosts set private='" + private1 + "' Komentar='" + txt_field + "'  where recordid=" + recordid;
        //
        cmd.Parameters.AddWithValue("@private1", SqlDbType.Bit).Value = private1;
        cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
        cmd.Parameters.AddWithValue("@Komentar", SqlDbType.NChar).Value = txt_field.ToString();               
        cmd.ExecuteNonQuery();

        if (private1==true)
        {
            //DateTime date = DateTime.Now;
            //cmd.CommandText = "Update AprovedCosts set AprovedCosts.AprovalUser = ";

            cmd.CommandText = "Update DetailCosts set privateCost=Costs where recordid=" + recordid;
            cmd.ExecuteNonQuery();
        }
        else
        {

            cmd.CommandText = "Update DetailCosts set privateCost=0 where recordid=" + recordid;
            cmd.ExecuteNonQuery();
        }
        con.Close();
    }
}

网格视图:

<asp:TemplateField HeaderText="Comment">
    <ItemTemplate>
        <asp:TextBox ID="txt_Comment" runat="server" Text='<%# Eval("Komentar") %>'></asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>

【问题讨论】:

    标签: c# asp.net visual-studio gridview textbox


    【解决方案1】:

    尝试使用txt_field.Text 而不是txt_field.ToString() 来获取值。

    【讨论】:

    • 但是 System.Web.UI.Control 不包含 Text 方法,我不能使用它
    【解决方案2】:

    您正在搜索可能不存在的控件并将其分配给文本框控件。

    TextBox txt_field = (TextBox)GridView2.Rows[i].Cells[12].FindControl("txt_Comment");
    

    为什么不将 textbox.text 值分配给 GridView2.Rows[i].Cells[12].Text ?

    假设它存在,它将返回一个值。

    【讨论】:

      【解决方案3】:

      为了更简单,只需像这样使用它

      TextBox txt_field = (TextBox)GridView2.Rows[i].Cells[12].FindControl("txt_Comment");
      string txt = txt_field.text; //store it's text in string
      

      并更改command

      代替txt_field 只需写txt 或txt.ToString();

       cmd.CommandText = "Update DetailCosts set private='" + private1 + "' Komentar='" + txt + "'  where recordid=" + recordid;
          //
          cmd.Parameters.AddWithValue("@private1", SqlDbType.Bit).Value = private1;
          cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
          cmd.Parameters.AddWithValue("@Komentar", SqlDbType.NChar).Value = txt.ToString();   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-24
        • 2013-03-16
        • 1970-01-01
        • 1970-01-01
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        • 2017-04-06
        相关资源
        最近更新 更多