如何仅在表单提交后才在表单上显示重置按钮?
How can I display a Reset button on a form ony after the form has submitted?
表单提交后,它会恢复到原始状态(大部分情况下),我们希望有一个“Reset
”按钮,允许用户启动页面“afresh
”。我希望我可以在代码中定义和配置 HtmlButton
:
HtmlButton btnStartNewDirPay = null;
. . .
private void CreateAndConfigureButtonStartNewDirectPayment()
{
btnStartNewDirPay = new HtmlButton();
btnStartNewDirPay.Attributes["type"] = "reset";
btnStartNewDirPay.InnerHtml = "Start new Direct Pay";
btnStartNewDirPay.ID = "btnStartNewDirPay";
}
...然后仅在 postback
上实例化它(在 form
提交之后),如下所示:
if (Page.IsPostBack)
{
if (null == btnStartNewDirPay)
{
CreateAndConfigureButtonStartNewDirectPayment();
}
this.Controls.Add(btnStartNewDirPay); //this causes problems
}
但是最后一行(按钮被添加到页面的(this)控件)失败了:
System.Web.HttpException was unhandled by user code Message=Failed
to load viewstate. The control tree into which viewstate is being
loaded must match the control tree that was used to save viewstate
during the previous request. For example, when adding controls
dynamically, the controls added during a post-back must match the type
and position of the controls added during the initial request.
Source=System.Web ErrorCode=-2147467259 StackTrace:
at System.Web.UI.Control.LoadViewStateRecursive(Object savedState)
at System.Web.UI.Control.AddedControl(Control control, Int32 index)
at DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.Page_Load(Object
sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
我还尝试了 creating/configuring HtmlButton 在开始时(当 IsPostBack 为 false 时),通过将控件从 git-go 添加到 "this",如下所示:
private void CreateAndConfigureButtonStartNewDirectPayment()
{
btnStartNewDirPay = new HtmlButton();
btnStartNewDirPay.Attributes["type"] = "reset";
btnStartNewDirPay.InnerHtml = "Start new Direct Pay";
btnStartNewDirPay.ID = "btnStartNewDirPay";
this.Controls.Add(btnStartNewDirPay);
}
...然后通过 jQuery:
隐藏它
$(window).load(function () {
. . .
$('[id$=btnStartNewDirPay]').hide();
});
...但是在 "CreateAndConfigureButtonStartNewDirectPayment()" 中的同一行 ("this.Controls.Add(btnStartNewDirPay);") 也失败了如上所述,一旦 IsPostBack 为真。
所以它不允许我在开始时实例化和隐藏 HtmlButton,然后在 IsPostBack 为 true 时显示它;看起来,它也不允许我将实例化推迟到 IsPostBack 为真时。当然可以有一个元素在回发之后显示,但不在回发之前显示...但是如何?
创建新按钮到此结束。
像这样在视图上设置按钮并使其可见=false。
<asp:Button id="btnStartNewDirPay" ClientIDMode="Static" Text="Reset" runat="server" Visible="False" />
将其 ClientIDMode 设置为静态,以便更容易通过 jQuery 获取 ID。
然后在 post 保存结束时将可见设置为真。
if(IsPostBack)
{
....
btnStartNewDirPay.Visible = true;
}
然后在前端有一个点击监听器
$(document).ready(function(){
$("#btnStartNewDirPay").click(function(){
//Reset inputs to default values.
})
})
表单提交后,它会恢复到原始状态(大部分情况下),我们希望有一个“Reset
”按钮,允许用户启动页面“afresh
”。我希望我可以在代码中定义和配置 HtmlButton
:
HtmlButton btnStartNewDirPay = null;
. . .
private void CreateAndConfigureButtonStartNewDirectPayment()
{
btnStartNewDirPay = new HtmlButton();
btnStartNewDirPay.Attributes["type"] = "reset";
btnStartNewDirPay.InnerHtml = "Start new Direct Pay";
btnStartNewDirPay.ID = "btnStartNewDirPay";
}
...然后仅在 postback
上实例化它(在 form
提交之后),如下所示:
if (Page.IsPostBack)
{
if (null == btnStartNewDirPay)
{
CreateAndConfigureButtonStartNewDirectPayment();
}
this.Controls.Add(btnStartNewDirPay); //this causes problems
}
但是最后一行(按钮被添加到页面的(this)控件)失败了:
System.Web.HttpException was unhandled by user code Message=Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Source=System.Web ErrorCode=-2147467259 StackTrace: at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.AddedControl(Control control, Int32 index) at DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.Page_Load(Object sender, EventArgs e) at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
我还尝试了 creating/configuring HtmlButton 在开始时(当 IsPostBack 为 false 时),通过将控件从 git-go 添加到 "this",如下所示:
private void CreateAndConfigureButtonStartNewDirectPayment()
{
btnStartNewDirPay = new HtmlButton();
btnStartNewDirPay.Attributes["type"] = "reset";
btnStartNewDirPay.InnerHtml = "Start new Direct Pay";
btnStartNewDirPay.ID = "btnStartNewDirPay";
this.Controls.Add(btnStartNewDirPay);
}
...然后通过 jQuery:
隐藏它$(window).load(function () {
. . .
$('[id$=btnStartNewDirPay]').hide();
});
...但是在 "CreateAndConfigureButtonStartNewDirectPayment()" 中的同一行 ("this.Controls.Add(btnStartNewDirPay);") 也失败了如上所述,一旦 IsPostBack 为真。
所以它不允许我在开始时实例化和隐藏 HtmlButton,然后在 IsPostBack 为 true 时显示它;看起来,它也不允许我将实例化推迟到 IsPostBack 为真时。当然可以有一个元素在回发之后显示,但不在回发之前显示...但是如何?
创建新按钮到此结束。
像这样在视图上设置按钮并使其可见=false。
<asp:Button id="btnStartNewDirPay" ClientIDMode="Static" Text="Reset" runat="server" Visible="False" />
将其 ClientIDMode 设置为静态,以便更容易通过 jQuery 获取 ID。
然后在 post 保存结束时将可见设置为真。
if(IsPostBack)
{
....
btnStartNewDirPay.Visible = true;
}
然后在前端有一个点击监听器
$(document).ready(function(){
$("#btnStartNewDirPay").click(function(){
//Reset inputs to default values.
})
})