0

I show a page in a jQuery dialog box. The user fills in the fields and submits the form.

In the function I need to call e.preventDefault(). In the underlying code I used the RegisterStartupScript method on the submit button. I know how to do it by passing parameters:

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page),
 "Script", "OrderForm('Item.aspx?OrderNum=" & _ID & "');", True)

How can I pass an event object in registerStartupScript?

I want a function like this:

function OrderForm(e, number) {
 e.preventDefault();
 //do something using number;
}
trincot
357k38 gold badges282 silver badges338 bronze badges
asked Dec 16, 2015 at 22:19
3
  • 1
    Where does the number come from when you click on the button? Commented Dec 16, 2015 at 22:29
  • There's no such thing as <btn>. Did you mean for that to be a class or ID? Commented Dec 16, 2015 at 22:31
  • As I understand, when you call to OrderForm() from RegisterClientScriptBlock(), there is no event to pass because you are calling the method directly. I mean in your code there is no event to handle. See the example from msdn; in that example is possible to pass an event because the user needs to click the button. Commented Dec 17, 2015 at 18:42

2 Answers 2

1

The .click() method is part of a framework (in this case I'm pretty sure it's jQuery) and it accepts a predefined number of arguments. You can use (for example) a data- attribute in your button with the number and then access to it through the object this.

// html
<button data-number="3">my button</button>
// js
$('button').click(function(e){
 e.preventDefault();
 var number = $(this).data().number; // or $(this).data('number')
 alert(number);
});

Try with this jsfiddle: https://jsfiddle.net/q3fvxk93/

answered Dec 16, 2015 at 22:30
Sign up to request clarification or add additional context in comments.

8 Comments

It should be $(this).data("number")
.data() returns an object. It is accessible (also) by object notation.
True, although the usual way to call it is with the data name argument. Returning the entire data object is mainly useful if you need very efficient access to multiple properties.
Well, .data().something returns just the value for something. Is it less efficient than .data('something')? Sincerely I don't know (I will review it in free time), but probably is the same (talking about performance).
Unless you're accessing multiple data values, the performance difference is probably negligible. .data().something returns the whole data object, then performs an object property access to get the value for just something.
|
0

the event object is in the scope $(this) I don't know where number should come from.

So your code would look this

$('btn').click(function(e) {
 e.preventDefault();
 $(this).attr('id'); //the button and it's properties like attributes in this example I get the ID of the button
});
answered Dec 16, 2015 at 22:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.