c# - GridView 中的文本框

标签 c# asp.net gridview ado.net

我想从 GridView 中的 TextBox 获取 Text 属性。这个 TextBox 有一些数据来 self 的数据库。当我更改此数据时,我想在我的数据库中进行更新。

但是当我搜索我的 TextBox 的文本时,他得到的是来 self 的数据库的旧值,而不是我现在输入的值。

我该怎么做才能获得我在 TextBox 中写入的实际数据,而不是来 self 的数据库的数据?

protected void VerifyPriority()
{
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        GridViewRow RowView = (GridViewRow)GridView1.Rows[i];
        TextBox txt = (TextBox)GridView1.Rows[i].FindControl("txtPriority");

        if (txt != null)
        {
            if (txt.Text != this.changes[i])
            {
                this.UpdatePriority(this.codigo[i], txt.Text);
            }
        }
    }
}

最佳答案

您很可能在每次回发后重新绑定(bind) GridView,而不是绑定(bind)一次并让 ViewState 保留数据。如果每次回发页面时都绑定(bind) GridView,则您所做的任何更改都将被清除并替换为数据库中的信息。

只加载第一页的绑定(bind)(在本例中):

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        GridView1.DataSource = GetSomeData();
        GridView1.DataBind();
    }
}

将上述代码放到位后,您应该能够从 TextBox 中获取正确的数据:

foreach (GridViewRow row in GridView1.Rows)
{
    TextBox txt = row.FindControl("TextBox1") as TextBox;
    if (txt != null)
    {
        string value = txt.Text;
    }
}

关于c# - GridView 中的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856175/

相关文章:

c# - 我无法使用Image to Base64 String函数

c# - ModalpopupExtender 不隐藏,Gridview 不刷新

javascript - ASP gridview的Reload事件

c# - 不支持约束的通用方法

c# - 将 C# 转换为 Java,如何处理 IEnumerable?

c# - 使用哪个隔离级别来防止读取数据?

c# - MONO 客户端和 ASP.NET 服务器之间的序列化/反序列化 - 可能吗?

c# - asp.net 网络表单路由无法正常工作

asp.net - 跨站点 ASP.NET 共享变量

c# - 在gridview中,如何仅在交替行上显示数据