Earlgray
Earlgray

Reputation: 647

Events in ASP NET Custom Server Control

I wonder what is the difference between that two methods of calling postback in my custom server control:

Page.ClientScript.GetPostBackEventReference(new PostBackOptions (this)) 

or manually

__doPostBack('MyControl1', '')

which of these two solutions is better to prefer and why?

Upvotes: 1

Views: 132

Answers (1)

SCleveland
SCleveland

Reputation: 46

I find that as a general rule of thumb it's better avoid hardcoding IDs in JavaScript that way. I've also found that it's better to let ASP generate things for you where it can so ASP gets what ASP is expecting.

If you let ASP generate your __doPostback for you then you know that ASP knows which control you're talking about. You're going to get the correct eventtarget and eventargument for the control in question when you use ClientScript.GetPostbackEventReference. Even if you move it around in the mark-up or otherwise do something to change its clientID ASP still knows what you're talking about because its generating the JavaScript.

Granted, you could avoid that particular pitfall with a well-place <%= MyControl1.ClientID%>, but I think keeping it in the back-end is cleaner.

I tried to find an article or blog talking about this to back me up so this wouldn't be strictly an opinion, but most of what I found was instructional on HOW to use it rather than an they WHY you should use it.

Upvotes: 1

Related Questions