C# ASP.NET文本框在单独的updatepanel内单击按钮时返回空文本
我在gridview中动态创建按行相互对应的文本框和按钮。按钮位于其自己的updatepanel中,以防止文本框在回发时丢失输入的文本。在onclick方法中,文本框返回空的,但是我似乎正在使用来自此站点的FindControlRecursive方法(具有预期的ClientID)查找正确的控件 我尝试过将控件移入和移出updatepanels,更改textbox的EnableViewState值,甚至还尝试过使用javascript提供一些建议 ASPX页面:C# ASP.NET文本框在单独的updatepanel内单击按钮时返回空文本,c#,asp.net,C#,Asp.net,我在gridview中动态创建按行相互对应的文本框和按钮。按钮位于其自己的updatepanel中,以防止文本框在回发时丢失输入的文本。在onclick方法中,文本框返回空的,但是我似乎正在使用来自此站点的FindControlRecursive方法(具有预期的ClientID)查找正确的控件 我尝试过将控件移入和移出updatepanels,更改textbox的EnableViewState值,甚至还尝试过使用javascript提供一些建议 ASPX页面: <%@ Page Title
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm1.aspx.cs" Inherits="Orders.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="gridViewItems0" runat="server">
<Columns>
<asp:TemplateField HeaderText="QUANTITY">
<ItemTemplate>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtQty0" runat="server" ValidationExpression="\d+"></asp:RegularExpressionValidator>
<asp:TextBox Width="40px" ID="txtQty0" runat="server" AutoPostBack="false" placeholder="0" MaxLength="3" EnableViewState="true"></asp:TextBox>
<asp:UpdatePanel ID="panelFood0" runat="server">
<ContentTemplate>
<asp:Button ID="btnAdd0" runat="server" Text="Add/Update" OnClick="btnAdd0_Click" CausesValidation="false" CommandArgument="" />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Content>
母版页
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Orders.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%: Page.Title %></title>
<asp:PlaceHolder runat="server">
<%: Scripts.Render("~/bundles/modernizr") %>
</asp:PlaceHolder>
<webopt:bundlereference runat="server" path="~/Content/css" />
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">
<Scripts>
<%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
<%--Framework Scripts--%>
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="bootstrap" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<asp:ScriptReference Name="WebFormsBundle" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
<div class="navbar navbar-expand-lg py-0 navbar-dark bg-dark fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand py-0" runat="server" href="~/">
<span><img src="Images/sitelogo.png" style="margin-right:5px">Orders</span>
</a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#collapseTarget">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="collapseTarget" style="padding-bottom:3px">
<ul class="nav navbar-nav mr-auto py-0">
<li class="nav-item"><a class="nav-link py-0" runat="server" href="~/">Home</a></li>
<li class="nav-item"><a class="nav-link py-0" runat="server" href="~/About">About</a></li>
<li class="nav-item"><a class="nav-link py-0" runat="server" href="~/Contact">Contact</a></li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<hr />
<footer>
<p>© <%: DateTime.Now.Year %> - Company Name</p>
</footer>
</div>
</form>
</body>
</html>
&复制;-公司名称
我应该从文本框中获取输入的文本,但是文本总是返回空字符串,即使文本仍然存在。我希望这是一个简单的东西,我只是不完全理解,因为我不是这方面的专家。我不确定您的FindControl递归方法中有什么,我能够在单击按钮时通过以下按钮返回相应的文本框值,甚至单击代码 您可以用以下方法替换btnAdd0_Click方法以查看其是否有效:
protected void btnAdd0_Click(object sender, EventArgs e)
{
Button btnClicked = (Button)sender;
string buttonClientID = btnClicked.ClientID;
buttonClientID = buttonClientID.Split('_').Last();
int buttonRowIndex = 0;
int.TryParse(buttonClientID, out buttonRowIndex);
GridViewRow row = gridViewItems0.Rows[buttonRowIndex];
TextBox txtCurrentQty = row.FindControl("txtQty0") as TextBox;
System.Diagnostics.Debug.WriteLine("ClientID: " + txtCurrentQty.ClientID);
System.Diagnostics.Debug.WriteLine("Text: " + txtCurrentQty.Text);
}
我不确定FindControlRecursive方法中有什么内容,我可以使用下面的按钮onclick-偶数代码在按钮单击时发回相应的文本框值 您可以用以下方法替换btnAdd0_Click方法以查看其是否有效:
protected void btnAdd0_Click(object sender, EventArgs e)
{
Button btnClicked = (Button)sender;
string buttonClientID = btnClicked.ClientID;
buttonClientID = buttonClientID.Split('_').Last();
int buttonRowIndex = 0;
int.TryParse(buttonClientID, out buttonRowIndex);
GridViewRow row = gridViewItems0.Rows[buttonRowIndex];
TextBox txtCurrentQty = row.FindControl("txtQty0") as TextBox;
System.Diagnostics.Debug.WriteLine("ClientID: " + txtCurrentQty.ClientID);
System.Diagnostics.Debug.WriteLine("Text: " + txtCurrentQty.Text);
}
“按钮位于其自己的updatepanel中,以防止文本框在回发时丢失输入的文本。”这不是更新面板的用途。如果动态添加文本框,然后导致回发,则页面将返回到以前的状态,因此您可能会丢失输入的文本。使用动态控件搜索回发时保留文本。有很多信息。谢谢你的评论。我将更新问题以提供更清晰的信息,因为文本框本身似乎保留了输入的数字,但即使使用断点,我也可以看到找到的控件的“Text”属性等于“”。按钮位于其自己的updatepanel中,以防止文本框在回发时丢失输入的文本这不是更新面板的用途。如果动态添加文本框,然后导致回发,则页面将返回到以前的状态,因此您可能会丢失输入的文本。使用动态控件搜索回发时保留文本。有很多信息。谢谢你的评论。我将更新这个问题以提供更清晰的信息,因为文本框本身似乎保留了输入的数字,但即使使用断点,我也可以看到找到的控件的“Text”属性等于“”。谢谢你的建议,Rashid。这并没有改变我的结果,但它让我相信,这可能与具有母版页的Web表单有关。我正在为母版页添加代码。您能找到文本框吗?或者TXTDcurrentQTY为空?是的,使用FindControlRecursive方法以及当我用您的方法完全替换我的btnAdd0\U Click方法时,可以正确找到文本框。在输出中,两种方法的“ClientID:”行相同。但是,“文本:”行仍然返回为空。我进行了编辑,尝试更新的btnAdd0_Click事件代码。-意外删除了一条注释-无更改。我得到的文本框ClientID是MainContent\u gridViewItems0\u txtQty0\u 0,但是文本仍然是空的。谢谢你的建议,Rashid。这并没有改变我的结果,但它让我相信,这可能与具有母版页的Web表单有关。我正在为母版页添加代码。您能找到文本框吗?或者TXTDcurrentQTY为空?是的,使用FindControlRecursive方法以及当我用您的方法完全替换我的btnAdd0\U Click方法时,可以正确找到文本框。在输出中,两种方法的“ClientID:”行相同。但是,“文本:”行仍然返回为空。我进行了编辑,尝试更新的btnAdd0_Click事件代码。-意外删除了一条注释-无更改。我将文本框ClientID作为MainContent\u gridViewItems0\u txtQty0\u 0获取,但文本仍然为空。