无法清除 ASP.NET CreateUserWizard 控件上的凭据文本框
我的登录/创建用户页面上有一个使用表单身份验证的 CreateUserWizard 控件。我自定义了 CreateUserWizardStep1,以便可以添加一些额外的验证器。
使用控件成功创建用户后,显示“完成 您的帐户已成功创建。”我添加了一个附加按钮,允许该人通过设置 ActiveStepIndex = 0 来创建另一个用户。问题是,虽然它正确设置了 ActiveStepIndex,但它保留了旧的用户帐户凭据。尝试使用以下代码手动清除它们,但它们仍然粘住......有人有什么想法
Protected Sub btnCreateAnotherUser_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.cuwMain.ActiveStepIndex = 0
CleanCreateNewUserInput()
End Sub
Private Sub CleanCreateNewUserInput()
Dim txtUserName As TextBox
txtUserName = FindControlIterative(Me.cuwMain, "UserName")
txtUserName.Text = String.Empty
Dim txtPassword As TextBox
txtPassword = FindControlIterative(Me.cuwMain, "Password")
txtPassword.Text = String.Empty
Dim txtConfirmPassword As TextBox
txtConfirmPassword = FindControlIterative(Me.cuwMain, "ConfirmPassword")
txtConfirmPassword.Text = String.Empty
Dim txtEmail As TextBox
txtEmail = FindControlIterative(Me.cuwMain, "Email")
txtEmail.Text = String.Empty
Dim txtQuestion As TextBox
txtQuestion = FindControlIterative(Me.cuwMain, "Question")
txtQuestion.Text = String.Empty
Dim txtAnswer As TextBox
txtAnswer = FindControlIterative(Me.cuwMain, "Answer")
txtAnswer.Text = String.Empty
End Sub
?它正确地找到了文本框,但它实际上并没有重置它们的值,即使在调试器中它说它做到了?
吗
I have a CreateUserWizard control using forms authentication on my login/create user page. I customized the CreateUserWizardStep1 so that I could add some additional validators.
After successfully creating a user with the control and it displays "Complete
Your account has been successfully created." I have added an additional button that will allow the person to create another user by setting the ActiveStepIndex = 0. The problem is, while it sets the ActiveStepIndex correctly, it retains the old user account credentials. I try to clear them manually using the following code, but they still stick...Anyone have any ideas?
Protected Sub btnCreateAnotherUser_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.cuwMain.ActiveStepIndex = 0
CleanCreateNewUserInput()
End Sub
Private Sub CleanCreateNewUserInput()
Dim txtUserName As TextBox
txtUserName = FindControlIterative(Me.cuwMain, "UserName")
txtUserName.Text = String.Empty
Dim txtPassword As TextBox
txtPassword = FindControlIterative(Me.cuwMain, "Password")
txtPassword.Text = String.Empty
Dim txtConfirmPassword As TextBox
txtConfirmPassword = FindControlIterative(Me.cuwMain, "ConfirmPassword")
txtConfirmPassword.Text = String.Empty
Dim txtEmail As TextBox
txtEmail = FindControlIterative(Me.cuwMain, "Email")
txtEmail.Text = String.Empty
Dim txtQuestion As TextBox
txtQuestion = FindControlIterative(Me.cuwMain, "Question")
txtQuestion.Text = String.Empty
Dim txtAnswer As TextBox
txtAnswer = FindControlIterative(Me.cuwMain, "Answer")
txtAnswer.Text = String.Empty
End Sub
It finds the textboxes correctly, but it does not actually reset their values, even though in the debugger it says it did.
Thoughts ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果调用 Response.Redirect(Request.Url.ToString(), true) 会发生什么?这应该会为你清除一切。
此外,FindControlIterative 调用的递归性质将使您的代码运行成本非常昂贵,因为它必须深入了解您正在查找的每个控件的控件层次结构。
您的代码的问题是:
在 Wizard 控件中,ViewState 不负责存储 TextBox 等控件的修改值。这些控件实现 IPostBackDataHandler 接口。 LoadPostBackData 事件在页面生命周期中触发,其中控件的值从表单 HTTP POST 标头加载...由客户端重新提交...
那么如何销毁 HTTP POST 标头以清除控件值?
新请求会产生新的 HTTP POST 标头...只需在按钮单击事件处理程序中执行此操作:
Response.Redirect(Page.Request.Url.ToString());
这样做的另一个好处是它会进入向导的第 1 步,因此您也不必执行... wiz.MoveTo(wiz.WizardSteps[0])。
感谢 Konrad - ASP.Net 向导 - 如何清除Web控件的内容
What happens if you call
Response.Redirect(Request.Url.ToString(), true)
? That should clear everything for you.Also, the recursive nature of the FindControlIterative call would make your code quite expensive to run as it has to drill down into the control heirarchy for every control that you are looking for.
The problem with your code is that:
In a Wizard control, ViewState is not responsible for storing the modified values for controls such as TextBoxes. These controls implement the IPostBackDataHandler interface. The LoadPostBackData event fires in the page lifecycle, in which the VALUES of the controls load from the form HTTP POST headers... which are resubmitted by the client...
So how to destroy the HTTP POST Headers to clear the control values?
A new request results in new HTTP POST Headers... simply do this in the Button click event handler:
Response.Redirect(Page.Request.Url.ToString());
This has the added benefit that it goes to Step 1 of the wizard so you also dont have to do... wiz.MoveTo(wiz.WizardSteps[0]).
Credit to Konrad - ASP.Net Wizard - How to clear contents of web controls
我觉得发布这个很愚蠢...,但我只是在 CreateUserWizard 控件上关闭了视图状态,然后就完成了。
感谢大牛的帮助,我现在对 ASP.NET 如何存储信息有了更好的了解。
I feel silly posting this..., but I just turned viewstate off on the CreateUserWizard control and that did it.
Thanks for the help Daniel, I now have a better understanding on how ASP.NET stores information.