c# - 如何在 C# 中获取修改后的文本框值?

标签 c# asp.net

我在 gridview 中使用文本框在页面加载期间绑定(bind)来自数据库的值。 问题是当我更改文本框值时,我无法在 C# 中获取修改后的值,而是它给出了在我修改之前已经存在的原始值。 我正在使用嵌套的 GridViews... 请帮助我..

<asp:TemplateField HeaderText="Singles" >
   <ItemTemplate>
   <asp:HiddenField ID="hidqua" runat="server" Value='<%#bind("QualityName") %>' />
   <asp:HiddenField ID="hidfau" runat="server" Value='<%#bind("FaultName") %>' />
   <asp:TextBox ID="asptxtsingleg" runat="server" Text='<%# bind("singles") %>'></asp:TextBox>
    </ItemTemplate> 
</asp:TemplateField>  

下面是我的c#编码

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "update")
        {
            foreach (GridViewRow row in grdInspection.Rows)
            {
                HiddenField hv = (HiddenField)row.FindControl("hidval");
                GridView txtsi = (GridView)row.FindControl("grdInsiewChid");
                foreach (GridViewRow row1 in txtsi.Rows)
                {
                    HiddenField htn = (HiddenField)row1.FindControl("hdnPLength");
                    GridView nesgrid = (GridView)row1.FindControl("GridView1");
                    foreach (GridViewRow row2 in nesgrid.Rows)
                    {
                        HiddenField qn = (HiddenField)row2.FindControl("hidqua");
                        TextBox t = (TextBox)row2.FindControl("asptxtsingleg");

                    }
                }
            }



        }
    }

最佳答案

假设您使用按钮触发 RowCommand 事件。

<asp:TemplateField HeaderText="Singles">
     <ItemTemplate>
         <asp:TextBox ID="asptxtsingleg" runat="server" Text='<%# Eval("singles") %>' Width="120px"></asp:TextBox>
     </ItemTemplate>
</asp:TemplateField>
 <asp:TemplateField HeaderText="Edit">
     <ItemTemplate>
         <asp:Button ID="BtnEdit" runat="server" Text="Update" CommandName="updateData" CommandArgument='<%# Eval("Your_ID") %>' />
     </ItemTemplate>
</asp:TemplateField>

代码隐藏:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "updateData")
    {
        //int i = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);

        TextBox tb = (TextBox)row.FindControl("asptxtsingleg");
    }
}

关于c# - 如何在 C# 中获取修改后的文本框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524575/

相关文章:

c# - Windows应用程序的分页控件

c# - 不包含分隔符时的字符串拆分实用程序方法问题

c# - 如何结合 ListBox、TextBox 和 Button 创建 asp.net 应用程序

javascript - 如何将值从 JavaScript 传递到 ASP.NET C# 参数

css - 如何将样式应用到 detailsview TemplateField 的 headertext 属性?

c# - EntityFramework 未使用默认值更新列

c# - 从 GridView TemplateField DropDownLists 更新 SQL 数据库

c# - 在 Windows 8 上的 C# 中注册自定义 URL 处理程序

c# - 以编程方式添加的 TemplateField 中的控件何时设置其 ID 属性?

asp.net - 使用 Identity Core 在 ASP.Net Core 2.1 中登录后立即添加重定向