views:

391

answers:

4

Hi folks,

I have a situation where I need to ignore parts of page load sub inside a isPostback = true. Basically inside the isPostBack I want something like if button1 caused postback.... do this else do this...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If IsPostBack = True Then

        If TextBox1.Text <> String.Empty Then

            CheckDate()
        End If

    End If


End Sub
+1  A: 

You should probably not have all this going on inside the Page_Load event. Instead, you should be handling events for each control that can cause a postback. This helps with code clarity, and ease of maintenance, not to mention better control in the first place.

Here's a nice brief blog entry I found on the subject: http://www.sitepoint.com/blogs/2007/01/21/page_load-is-evil/

Incidentally, handling events is much different in ASP.NET than in other environments, My guess, just based on the fact that you're trying to accomplish this in the Page_Load event is that you're not yet "getting" the event-driven programming model. (If I'm wrong, I apologize, I don't mean to be insulting).

If I'm right, however, once you get used to it, it's going to be a lot simpler for you than things were in the classic ASP days, for example, where you had to do things like try to figure out which button was clicked. Here's another nice article to explain this further: http://articles.sitepoint.com/article/driven-asp-net-development-c

David Stratton
thanks for the links. I get the event driven model. The problem is that page_load is going to be triggered wether I like it or not when I click a submit button. At the moment I use the page_load when a date is selected from a ajax calendar via some javascript. I only want to not trigger page_load when a submit button is clicked. I'm not using page_load for anything else
iamjonesy
+1  A: 

It's hard to see this as a good idea. From the short snippet you posted, it looks like what you really need is a Validation control attached to your textbox.

Joel Coehoorn
+1. Man, that was obvious. Why didn't I see that?
David Stratton
my form has a calendar control attached to a textbox. when a date is selected from the calendar a bit of javascript submits the page. When page_load is hit it checks if the textbox is empty or not. If not it goes to check if an entry already exists for that date. so I'm not sure a validation control would work here
iamjonesy
@Jonesy you can attach a validator to the calendar, or you can add javacript to make the calendar populate the textbox _before_ submitting the page. In fact, I suspect you could use javascript here to skip this postback entirely, and whenever you can do that it's a good thing.
Joel Coehoorn
A: 

Have a look at the POSTed items. You should see some sort of reference to that button in there. IIRC, if it was clicked, you will see some sort of reference in there, and if it wasn't it wouldn't be in there. I had this same problem a while a ago and that's how I circumvented loading some stuff.

Mike
sorry but how do i see the posted items?
iamjonesy
+1  A: 

I think what you need is the reference to the control name which triggered the postback.

http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx

Implement the solution which is there in the above link (Got it from here.... http://stackoverflow.com/questions/2236383/how-to-get-source-of-postback)

If the control name is your button then do not do what needs to be done upon postback.

HTH

Raja
hey thanks man, code looks good but I'm getting an error when I convert it to VB.NET. If (TypeOf c is (TypeOf (System.Web.UI.WebControls.Button OrElse c) Is System.Web.UI.WebControls.ImageButton))) Then control = c Exit For End Ifthe error is "type expected" and its after the first "is". please help!
iamjonesy
Try this: If TypeOF c IS Button Or TypeOF c is ImageButton thencontrol = cend if
Raja
brialliant, thanks very much Raja!!
iamjonesy
You are welcome. Glad it worked.
Raja
interestingly actually when i postback with a textbox the control id is TextBox1 but after that when I postback with the button it remains as TextBox1 not Button1. Does that mean a submit button is not technically a post back trigger?
iamjonesy
Submit button definitely triggers a postback. Please check to see if you have removed the runat="server" by mistake.
Raja