Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manually creating postback with javascript

Status
Not open for further replies.

RicksAtWork

Programmer
Nov 1, 2005
120
GB
I wasnt to generate a postback as if a specific button element was pressed. Looking at how .net does this it seems to add the following html to a form:

<input type="hidden" name="__EVENTTARGET" />
<input type="hidden" name="__EVENTARGUMENT" />

With this an additional javascript method is created which populates __EVENTTARGET with the name of the button pressed and then submits the form.

I cannot get this to work manually as the server side event assocaited with the button doesnt fire.

What am i doing wrong?




 
Why?! If you need to click a button for some reason in javascript the get a reference to the button and call it's click method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
It's fairly straight-forward. In javascript to get a reference to an element use:
Code:
var elem = document.getElementById("myButton");
Then, call it's click method:
Code:
elem.click();
I can't many situations where this would be useful though...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I've rephrased the question slightly:

I want to generate a postback using client side javascript.

I am currently using unobtrusive javascript (for accessibility reasons) so, I am NOT using code like this:

<input type="button" id="btnUpdate" onclick="Validate()" />

Instead I am using:

addEvent(document.getElementById(btnUpdate), 'click', Validate);

This means that I dont have any javascript in my HTML.

My Validate method looks like this:

function Validate(e)
{

eventObject = (e)? e:event;

//IE
originalObject = e.srcElement;

//Mozilla
if(originalObject == null)
{
originalObject = e.target;
}

document.getElementById('__EVENTTARGET').value= originalObject.id;
document.getElementById('__EVENTARGUMENT').value=null;
document.getElementById('Form1').submit();

}

In the HTML I have manually added:

<input type="hidden" name="__EVENTTARGET" />
<input type="hidden" name="__EVENTARGUMENT" />

So this method should populate the EVENTTARGET hidden field with the ID of the button which was pressed and then submit the form.

This works, BUT the .NET server side event is not fired so I cannot catch the event in my btnUpdate_Click method in my C#.


I am under the impression that there is something additional I need to be doing.

What am i doing wrong?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top