Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to resolve the problems of validating controls in ASP.NET1.1, ASP.NET2.0 and ASP.NET3.5

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to resolve ASP.NET1.1, ASP.NET2.0, ASP.NET3.5 verification controls and other issues, the quality of the article content is high, so Xiaobian share for everyone to make a reference, I hope you have a certain understanding of related knowledge after reading this article.

On ASP.NET pages, when there is a validation control and you want to validate the control, a confirmation dialog box pops up to prompt you whether to continue.

Onclick="return confirm ('Are you sure to continue? ')", validation of the validation control fails. Because validation controls are also onclick events that add clients.

In asp.net2.0, asp.net3.5, you can use the following method to solve this problem. (ASP.NET 1.1 later)

First of all, CausesValidation="false" of button, that is,

< asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" OnClick="clickme" />

In fact, CausesValidation="False/True" has no effect.

Next, register the client onclick event in the Page_Load event of the background code.

Method 1:

protected void Page_Load(object sender, EventArgs e) { Button1.OnClientClick = ClientScript.GetPostBackEventReference( new PostBackOptions(Button1, "", "", false, true, false, false, true, "")) + ";return (Page_IsValid && confirm('Are you sure to continue? '));"; }

One thing to note here is that you cannot put it in if(! isPostBack) {...} * **

Microsoft JScript Runtime Error: 'WebForm_PostBackOptions' undefined

That is, every time it is triggered, it is required to re-register an onclick event.

There was this problem during yesterday's test, which may be due to a problem with VS2008 installation (design mode cannot be displayed).

Under today's test, you can only register once, that is, when the page loads, as follows:

protected void Page_Load(object sender, EventArgs e) { if (! IsPostBack) { Button1.OnClientClick = ClientScript.GetPostBackEventReference( new PostBackOptions(Button1, "", "", false, true, false, false, true, "")) + ";return (Page_IsValid && confirm('Are you sure to continue? '));"; } }

Validation Control Method 2:

protected void Page_Load(object sender, EventArgs e) { if (! IsPostBack) { Button1.OnClientClick = "_javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false));return (Page_IsValid && confirm('Are you sure to continue? '));WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false))"; } }

In fact, after method 1 runs, the onclick code generated in the client html is the code in method 2.

If you use method 2 to write javascript strings directly in the background, you can remove the *** sentence, otherwise, you will have one more verification. These are as follows:

protected void Page_Load(object sender, EventArgs e) { if (! IsPostBack) { Button1.OnClientClick = "_javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, '', '', false, false));return (Page_IsValid && confirm('Are you sure to continue? '));"; } }

Method 1 registers once in page_load before each button click, while Method 2 registers once in page_load.

vb.net differs slightly from html client code generated by C#

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Me.Page.IsPostBack Then Me.Button1.OnClientClick = "_javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('Button1', '', true, 'name', '', false, false));return (Page_IsValid && confirm('Are you sure to continue? '));" End If End Sub

or

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender If Not Me.Page.IsPostBack Then 'VB. NET to put the following sentence in Page_PreRender, not in Page_Load, and only need to register once 'There is less code than C#in the client code that generates html. Me.Button1.OnClientClick = Me.Page.ClientScript.GetPostBackEventReference(New System.Web.UI.PostBackOptions(Me.Button1, "", "", False, True, False, False, True, "name")) & _ ";return (Page_IsValid && confirm('Are you sure to continue? '));" End If End Sub

ASP.NET 2.0 and ASP.NET 3.5 are over, here is the solution in ASP.NET 1.1

The first is to set the button CausesValidation="false", i.e.

< asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" OnClick="clickme" />

Next, register the client onclick event in the Page_Load event of the background code.

private void Page_Load(object sender, System.EventArgs e) { //Place user code here to initialize the page if (! this.Page.IsPostBack) { string msg = "_javascript:if (typeof(Page_ClientValidate) == 'function'){ if(Page_ClientValidate()) return window.confirm('Are you sure to continue? ');}"; this.Button1.Attributes.Add("onclick",msg); } }

Because the onclick code registered in asp.net1.1 only contains javascript code, it can be placed in if(! IsPostBack){...} There will be no problem inside.

This will validate the control first, and then pop up the confirmation dialog box.

ASP.NET 1.1, ASP.NET 2.0, ASP.NET 3.5 on how to resolve validation controls and other issues shared here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report