c# - Gridview 中的 TextBox 返回 null 或 'System.Web.UI.WebControls.TextBox'

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

我在从文本框中检索数据时遇到问题。 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();
    }
}

GridView :

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

最佳答案

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

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

并更改命令

代替 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();   

关于c# - Gridview 中的 TextBox 返回 null 或 'System.Web.UI.WebControls.TextBox',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832060/

相关文章:

c# - SqlDataAdapter - 结合存储过程和带参数的 Select 语句

c# - 如何将 System.Data.SqlClient nuget 包添加到 .Net Portable (PCL)4.5 配置文件中

c++ - Moc'ed 文件神秘地从 Visual Studio 项目中排除

c# - 使用 ASP.Net 的 Crystal Reports 中的 Blob 字段

.net - 如何构建 .NET MVC 和 Web API 项目并将其发布到不同的文件夹

c# - 为什么 add-migration 命令没有创建迁移?不识别包裹

c# - 您的项目未引用 ".NETFramework,Version=v4.6.2"框架。在 ".NETFramework,Version=v4.6.2"中添加对 "TargetFrameworks"的引用

c# - MarshalAs(UnmanagedType.ByValArray, SizeConst 大小限制

asp.net - 显示默认阴影的 div

javascript - 在 ASP.NET Web 窗体中缩小和捆绑 CSS 时出现 "403 - Forbidden: Access is denied"错误