gpt4 book ai didi

c# - 将 GridView 中 TextBox 的值存储到数据库?

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:28 24 4
gpt4 key购买 nike

我有一个 GridView,我在其中将文本框放在每一列的项目模板中。我想在单击按钮时将我将在文本框中输入的值存储到数据库中。

protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
DataTable dt = new DataTable();
DataRow row = dt.NewRow();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string UserID = GridView1.Rows[i].Cells[1].Text;
string q="insert into details (name) values('"+UserID+"')";

SqlCommand cmd = new SqlCommand(q, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}

问题是当我运行页面时文本框甚至不可见。

最佳答案

您不能直接从网格中获取文本框的引用。所以首先你需要从每一行网格中找到文本框。代码应该是这样的。。

protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
DataTable dt = new DataTable();
DataRow row = dt.NewRow();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
## add below code ##
TextBox txtUsrId=(TextBox)GridView1.Rows[i].FindControl("Your textbox id");
string UserID = txtUsrId.Text;

string q="insert into details (name) values('"+UserID+"')";

SqlCommand cmd = new SqlCommand(q, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}

让我知道..

关于c# - 将 GridView 中 TextBox 的值存储到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13582873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com