Shiny Donkey!
Shiny Donkey! Shiny Donkey! Shiny Donkey!
                  Fake Banner Ads!  Mini-Sites!  
    New Shiny Donkey Posts  more >>
* BREAKING NEWS *  9 out of 10 fry cooks recommend doing your homework instead of shinydonkey.com

.NET DoPostBack from .NET
[reply]   

09/07/04 11:08 AM EST
posted by alex email

The .NET Framework builds a __doPostBack JavaScript event for the firing of an AutoPostBack. Within this postback, there is a call to theform.submit() that does the form submission. On my form when the page is submitted it is supposed to take some info from a div and then write that info into a hidden form variable for use in the form's submission. The problem that I am having is that if I have an item in the OnSubmit of the Form (using the page.RegisterOnSubmitStatement() method in .NET) it will not fire on the AutoPostBack of the page when something forces it, and I do not have access to the data that should be in the hidden form field. So I put the same logic in an onload event that binds the javascript method to the onsubmit of the form as follows:

var theform;
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.__aspnetForm;
}else {
theform = document.forms["__aspnetForm"];
}
theform.onsubmit = getRichText;


But this also fails to fire when the Submit method is called.

I added the call to the window.unload() of the page and that gets it to fire, but I think it is too late and the form data has already been submitted. How can I get this to call whenever the form is submitted regardless of who initiates the submission?

 


[reply]   

09/07/04 11:21 AM EST
posted by JER email web

Why doesn't the RegisterOnSubmitStatement method work? Can't you just write:

RegisterOnSubmitStatement("alexExample", "document.forms[0].someFormVar.value=document.getElementById('somethingElse').innerHTML;");

 


[reply]   

09/07/04 11:25 AM EST
posted by alex email

I basically did:

RegisterOnSubmitStatement("alexExample", "methodName()");

And this successfully puts the methodName() in teh onsubmit of the <form> tag, but this fails to fire when form.submit() is called from within the page. Lame to me.

 


[reply]   

09/07/04 11:27 AM EST
posted by JER email web

wait... It DOES put it in the client-side code but it DOES NOT appear to fire??? Replace "methodName()" with "return confirm('Are you sure Alex is gay?')"

 


[reply]   

09/07/04 11:37 AM EST
posted by nate web

Shouldn't it be RegisterOnSubmitStatement("alexExample","return alexIsGay()"); and won't it always be true?

 


[reply]   

09/07/04 11:37 AM EST
posted by alex email

This is correct Jer.

I used your example exactly simply for comedy, but also had an alert in the method prior. The issue is that it simply doesn't fire.

This is when I have an asp:checkbox with an AutoPostBack = true and OnCheckedChanged server side method. When the box is checked or unchecked the OnSubmit for the form does not fire.

 


[reply]   

09/07/04 11:49 AM EST
posted by JER email web

Ok, got it. This works for Framework version 1.1 but is a COMPLETE HACK.

In your page load, simply add:

myCheckBox.Attributes["onclick"] = "return confirm('Are you sure Alex is gay'); " + testCheck.Attributes["onclick"];

This will probably behave badly without a (!IsPostBack), so be wary...

 


[reply]   

09/07/04 11:52 AM EST
posted by alex email

Yep, that is the same lame ass hack I came up with too. Since this is a UserControl (the infamous RichTextEditor) I just put the onclick event on all controls in the parent page where AutoPostBack = true

Totally lame .NET, totally lame.

 


[reply]   

09/07/04 12:06 PM EST
posted by JER email web

You've been promising this rich text editor for 5 months. Is it finished yet????

 


[reply]   

09/07/04 12:08 PM EST
posted by alex email

Patience young man, patience. I actually just figured out the primary bug that has been holding it up for the entire last 4 months.

 


[reply]   

09/08/04 07:13 AM EST
posted by alex email

Ok, check this lamitude out. When I got to thinking about it, I decided I didn't want to deal with the whole putting an onclick event on all controls that have AutoPostBack = true. Rather than that I wanted to just make it function the way it was supposed to. Ideally, Microsoft with address this as a bug and fix it in the next version of .NET. What it would require is an additional method called something like Page.RegisterOnAutoPostbackStatement(). This method would alter the generated ASP.NET form __doPostBack method to include the registered script just prior to the "theform.submit()" statement. MS taking care of it is in a perfect world... I was thinking about doing my own thing and inheriting from Page, but that would make the UserControls dependent on this thing and as a result not entirely portable. So in the meantime, before MS develops its "patch," I developed a seriously lame kludge to do this very thing.

I am already overriding Render to correct the issue with form naming causing Javascript Errors in Mozilla and other non IE browsers, so adding a little logic to enable my idea was pretty easy. Here is the code for my Render override:


protected override void Render(HtmlTextWriter writer) {
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string html = stringBuilder.ToString();

int start;
int end = 0;
string formId;
string replace;
int count = 0;
while (html.IndexOf("<form name=\"",end) > -1){
start = html.IndexOf("<form name=\"",end) + 12;
end = html.IndexOf("\"", start);
formId = html.Substring(start, end - start);
replace = formId.Replace(":", "_");
html = html.Replace("document." + formId, "document." + replace);
count ++;
}

int onSubmitStart = html.IndexOf("language=\"javascript\" onsubmit=\"",html.IndexOf("<form name=\"__aspnetForm"));
if (onSubmitStart > -1 && html.IndexOf("function __doPostBack(eventTarget, eventArgument) {") > -1){
onSubmitStart += 32;
int onSubmitEnd = html.IndexOf("if (!ValidatorOnSubmit())",onSubmitStart);
if (onSubmitEnd == -1)
onSubmitEnd = html.IndexOf("\"",onSubmitStart);
string onSubmitItems = html.Substring(onSubmitStart,onSubmitEnd - onSubmitStart);
if (onSubmitItems != string.Empty){
string[] onSubmitArray = onSubmitItems.Split(';');
onSubmitItems = string.Empty;
for(int i = 0 ; i < onSubmitArray.Length ; i++){

if (onSubmitArray[i].StartsWith("return")){
onSubmitArray[i] = onSubmitArray[i].Replace("return",string.Empty);
onSubmitArray[i] = "if (!(" + onSubmitArray[i] + ")){return false;}";
}
if(onSubmitArray[i]!= string.Empty)
onSubmitItems += onSubmitArray[i] + ";" + Environment.NewLine;
}
int locationOfSubmit = html.IndexOf("theform.submit();");
html = html.Insert(locationOfSubmit,onSubmitItems + Environment.NewLine);
}
}

writer.Write(html);
}

*Update, corrected small bug above

Basically I look to see if there is anything in the onsubmit tag that I put there. If there is, I am going to place it just prior to the "theform.submit()" line in the doPostBack() Javascript method. The only other thing is in regards to the different between OnSubmit and doing it in a method. In the onsubmit, if you return false, it kills the submission, otherwise it will return true and allow it. If in the method you return anything it will kill the submission because the .submit() will not fire. So as a result I search for "return" and if it exists I replace it with an if(!(line to eval)){return false;}, in effect causing the submission to stop if the line is not fulfilled. This works well and I am rather happy with how lame it is. Let me know what you think. And also, please excuse the sloppy, uncommented, and poor code, it was thrown together and I will be refining it as I have time.

 


[reply]   

09/08/04 07:20 AM EST
posted by alex email

Thats weird, the end of my last message is getting cut off even though it is in the HTML. Here is the rest:

Basically I look to see if there is anything in the onsubmit tag that I put there. If there is, I am going to place it just prior to the "theform.submit()" line in the doPostBack() Javascript method. The only other thing is in regards to the different between OnSubmit and doing it in a method. In the onsubmit, if you return false, it kills the submission, otherwise it will return true and allow it. If in the method you return anything it will kill the submission because the .submit() will not fire. So as a result I search for "return" and if it exists I replace it with an if(!(line to eval)){return false;}, in effect causing the submission to stop if the line is not fulfilled. This works well and I am rather happy with how lame it is. Let me know what you think. And also, please excuse the sloppy, uncommented, and poor code, it was thrown together and I will be refining it as I have time.

 


[reply]   

01/03/05 05:33 PM EST
posted by

 


[reply]   

12/09/05 05:08 AM EST
posted by floook

I know it's a bit too late and you've probably had it figured out but for anybody who visits this page and still wonders what the hell is going on: when you use the submit() function in javascript to submit your form, the onsubmit script is ignored as the onsubmit event is only triggered when you submit the form using its submit button.

So, if your .net code creates a javascript function that submits the form, it's natural that your onsubmit handler is "ignored".

Regards,
Floook

 

Name

 registered? log in!

E-mail (optional)

Website (optional)

  

To ensure security, this site requires unregistered users to enter a verification code:
 
Your code is:  
Enter Code:   

Note: Only registered ShinyDonkey.com users can post images. Only administrators can delete images.

  



"And remember, a shiny new donkey for whoever brings me the head of Colonel Montoya..."
e-mail webmaster