Showing posts with label postback. Show all posts
Showing posts with label postback. Show all posts

Saturday, March 24, 2012

strange event postback behaviour in dynamically created usercontrol

hi,
i have an aspx page which dynamically loads a user control and adds it
to a placeholder. the control is recreated and added to the
placeholder for postbacks as well.
the user control contains a datagrid and some buttons and textboxes.
the button events fire correctly and execute their code. however, the
datagrid events fire a postback, and execute Page_load in the
usercontrol, but the Item_Command code is not executed. the code to
add events to the datagrid is in InitialiseComponent.

i have the datagrid binding in UserControl_PreRender, so that it is
bound between postbacks.

can anyone suggest why the postback is happening without the
ItemCommand event happening? i've verified this with VS debugging.

thanks
timHi Tim_Mac,

Welcome to ASP.NET newsgroup.
From your description, you've a UserControl which contains some button and
a DataGrid on it, the datagrid is subscribing some postback
events(ItemCommand). And in one of your asp.net page, you dynamically load
the usercontrol and add it on your page. However, you found the ItemCommand
of the datagrid(in usercontrol) always not fire, yes?

Based on my experience, such event handler not executing problem is likely
caused by the following things:
1. For dynamic loaded(created) controls, make sure we add them in the
Page_Init or Page_Load every time the page is requested(IsPostback or
not...).

2. For control's event handler, we also need to register them in Init or
Load event. Also, for template databound controls such as DataGrid or
DataList, we should avoid rebinding them with different datasource before
processing postback event since the event may depend on the original binded
datas.

As for your condition, since some other controls (such as button)'s event
handler execute correctly. I think the problem is likely within the
UserControl itself. Would you try registering your datagrid's ItemCommand
handler in the usercontrol's ascx template instead of programmatically
register in codebehind? Just like
<asp:datagrid ..... OnItemCommand="DataGrid_ItemCommand" ...
If you feel it convenient, I suggest you try build a simpler usercontrol
(contains a datagrid) so as to repro the problem and send it to me. Then I
can do some researchs on my side.

In addition, if you feel necessary, I can also generate a test page on my
side and let you have a compare to see whehter there are any difference.

Please feel free to let me know if you have any questions. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
hi Steven,
thank you for your most helpful reply.
i have got it working now, and will attempt to explain how and why for
future reference.

my aspx page contaings the following in Page_Load:
Modules.SportsLadder ladder =
this.LoadControl("Modules/SportsLadder.ascx") as Modules.SportsLadder;
this.serverForm.Controls.Add(ladder);

the user control is loaded regardless of whether a postback has taken
place or not.
the user control then contains a call to if(!IsPostBack) BindGrid() in
the Page_Load.

when i would delete an item in the datagrid, the page would post back,
and Page_Load would fire, but the datagrid_itemcommand event would not
fire. the datagrid would disappear from the page.

i tried adding in a PreRender event for the usercontrol, and calling
BindGrid() there, and that kept the datagrid on screen after postbacks,
but the itemcommand event still didn't fire. what got it working was i
removed the if(!IsPostBack) from the Page_Load of the user control, so
then the datagrid is bound every time. i then had the problem of the
itemcommand event being called twice (what a surprise). the reason is
because it was set in the aspx page, as Steven suggested:
<asp:datagrid ..... OnItemCommand="DataGrid_ItemCommand" ...>
but it was also set in InitialiseComponent in code. i took it out of
the code, and let the aspx handler do it.

now it works perfectly. i didn't run into any problems re-binding the
datagrid in Page_Load, my methods were still able to identify the row
that was clicked and any of the controls in the cells.

thanks for your help.
tim

strange event postback behaviour in dynamically created usercontrol

hi,
i have an aspx page which dynamically loads a user control and adds it
to a placeholder. the control is recreated and added to the
placeholder for postbacks as well.
the user control contains a datagrid and some buttons and textboxes.
the button events fire correctly and execute their code. however, the
datagrid events fire a postback, and execute Page_load in the
usercontrol, but the Item_Command code is not executed. the code to
add events to the datagrid is in InitialiseComponent.
i have the datagrid binding in UserControl_PreRender, so that it is
bound between postbacks.
can anyone suggest why the postback is happening without the
ItemCommand event happening? i've verified this with VS debugging.
thanks
timHi Tim_Mac,
Welcome to ASP.NET newsgroup.
From your description, you've a UserControl which contains some button and
a DataGrid on it, the datagrid is subscribing some postback
events(ItemCommand). And in one of your asp.net page, you dynamically load
the usercontrol and add it on your page. However, you found the ItemCommand
of the datagrid(in usercontrol) always not fire, yes?
Based on my experience, such event handler not executing problem is likely
caused by the following things:
1. For dynamic loaded(created) controls, make sure we add them in the
Page_Init or Page_Load every time the page is requested(IsPostback or
not...).
2. For control's event handler, we also need to register them in Init or
Load event. Also, for template databound controls such as DataGrid or
DataList, we should avoid rebinding them with different datasource before
processing postback event since the event may depend on the original binded
datas.
As for your condition, since some other controls (such as button)'s event
handler execute correctly. I think the problem is likely within the
UserControl itself. Would you try registering your datagrid's ItemCommand
handler in the usercontrol's ascx template instead of programmatically
register in codebehind? Just like
<asp:datagrid ..... OnItemCommand="DataGrid_ItemCommand" ...>
If you feel it convenient, I suggest you try build a simpler usercontrol
(contains a datagrid) so as to repro the problem and send it to me. Then I
can do some researchs on my side.
In addition, if you feel necessary, I can also generate a test page on my
side and let you have a compare to see whehter there are any difference.
Please feel free to let me know if you have any questions. Thanks,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
hi Steven,
thank you for your most helpful reply.
i have got it working now, and will attempt to explain how and why for
future reference.
my aspx page contaings the following in Page_Load:
Modules.SportsLadder ladder =
this.LoadControl("Modules/SportsLadder.ascx") as Modules.SportsLadder;
this.serverForm.Controls.Add(ladder);
the user control is loaded regardless of whether a postback has taken
place or not.
the user control then contains a call to if(!IsPostBack) BindGrid() in
the Page_Load.
when i would delete an item in the datagrid, the page would post back,
and Page_Load would fire, but the datagrid_itemcommand event would not
fire. the datagrid would disappear from the page.
i tried adding in a PreRender event for the usercontrol, and calling
BindGrid() there, and that kept the datagrid on screen after postbacks,
but the itemcommand event still didn't fire. what got it working was i
removed the if(!IsPostBack) from the Page_Load of the user control, so
then the datagrid is bound every time. i then had the problem of the
itemcommand event being called twice (what a surprise). the reason is
because it was set in the aspx page, as Steven suggested:
<asp:datagrid ..... OnItemCommand="DataGrid_ItemCommand" ...>
but it was also set in InitialiseComponent in code. i took it out of
the code, and let the aspx handler do it.
now it works perfectly. i didn't run into any problems re-binding the
datagrid in Page_Load, my methods were still able to identify the row
that was clicked and any of the controls in the cells.
thanks for your help.
tim
Hi Tim_Mac,
Thanks a lot for your detailed response. I'm glat that you've got greate
progress on this issue. Though you've got it worked currently, I'm still
abit confusing about your detailed page and usercontrol's code logic. Based
on my understanding, as long as your UserControl is added into the page's
control collection everytime( and has a fixed it assigned to it). We don't
need to bind data to the datagrid(in the usercontrol) everytime, just in
If(! IsPostback) ) or in a postback event is ok. And the ItemCommand can
also fire correctly.
I've attached a sample page/usercontrol I used to test. You can have a test
on your side if you have interesting.
Hope helps. If there is anything else unclear, please feel free to post
here. Thanks,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Thursday, March 22, 2012

Strange HTML button postback

I have a webform with lots of <asp:...controls but I need couple of buttons
that doesn't cause an automatic postback. I use javascript to handle these
buttons. I tried two approaches with different results:

<IMG onmouseup="src='Bitmaps/Controls/measure_over.bmp';"
onmousedown="src='Bitmaps/Controls/measure_pressed.bmp';"
id="measure" onmouseover="src='Bitmaps/Controls/measure_over.bmp';"
style="LEFT: 170px; WIDTH: 24px; POSITION: absolute; TOP: 2px; HEIGHT: 25px"
onclick="buttonClicked(this.id);"
onmouseout="src='Bitmaps/Controls/measure_btn.bmp';"
alt="" src="http://pics.10026.com/?src=Bitmaps/Controls/measure_btn.bmp"
This is working the way I want to, but I can't disable it as I can with a
button. I then replace the IMG with:

INPUT Type="image"

and it makes a postback every time I click on it. Why? A HTML control
should't do that, right? The script itself (buttonClicked) just fills a
hidden field.

/MiroFrom the MSDN Library about the <input typ="image" tag:

------------------------
INPUT type=image Element | input type=image Object
Creates an image control that, when clicked, causes the form to be
immediately submitted.
------------------------

Maybe add "onclick='return false;'" to the tag.

-Jason Kendall
JasonKendall@.hotmail.com

On Tue, 1 Feb 2005 05:55:13 -0800, "Miro"
<Miro@.discussions.microsoft.com> wrote:

>I have a webform with lots of <asp:...controls but I need couple of buttons
>that doesn't cause an automatic postback. I use javascript to handle these
>buttons. I tried two approaches with different results:
><IMG onmouseup="src='Bitmaps/Controls/measure_over.bmp';"
>onmousedown="src='Bitmaps/Controls/measure_pressed.bmp';"
>id="measure" onmouseover="src='Bitmaps/Controls/measure_over.bmp';"
>style="LEFT: 170px; WIDTH: 24px; POSITION: absolute; TOP: 2px; HEIGHT: 25px"
>onclick="buttonClicked(this.id);"
>onmouseout="src='Bitmaps/Controls/measure_btn.bmp';"
>alt="" src="http://pics.10026.com/?src=Bitmaps/Controls/measure_btn.bmp">
>This is working the way I want to, but I can't disable it as I can with a
>button. I then replace the IMG with:
>INPUT Type="image"
>and it makes a postback every time I click on it. Why? A HTML control
>should't do that, right? The script itself (buttonClicked) just fills a
>hidden field.
>/Miro

Strange HTML button postback

I have a webform with lots of <asp:...controls but I need couple of buttons
that doesn't cause an automatic postback. I use javascript to handle these
buttons. I tried two approaches with different results:
<IMG onmouseup="src='Bitmaps/Controls/measure_over.bmp';"
onmousedown="src='Bitmaps/Controls/measure_pressed.bmp';"
id="measure" onmouseover="src='Bitmaps/Controls/measure_over.bmp';"
style="LEFT: 170px; WIDTH: 24px; POSITION: absolute; TOP: 2px; HEIGHT: 25px"
onclick="buttonClicked(this.id);"
onmouseout="src='Bitmaps/Controls/measure_btn.bmp';"
alt="" src="http://pics.10026.com/?src=Bitmaps/Controls/measure_btn.bmp">
This is working the way I want to, but I can't disable it as I can with a
button. I then replace the IMG with:
INPUT Type="image"
and it makes a postback every time I click on it. Why? A HTML control
should't do that, right? The script itself (buttonClicked) just fills a
hidden field.
/MiroFrom the MSDN Library about the <input typ="image" tag:
----
--
INPUT type=image Element | input type=image Object
Creates an image control that, when clicked, causes the form to be
immediately submitted.
----
--
Maybe add "onclick='return false;'" to the tag.
-Jason Kendall
JasonKendall@.hotmail.com
On Tue, 1 Feb 2005 05:55:13 -0800, "Miro"
<Miro@.discussions.microsoft.com> wrote:

>I have a webform with lots of <asp:...controls but I need couple of buttons
>that doesn't cause an automatic postback. I use javascript to handle these
>buttons. I tried two approaches with different results:
><IMG onmouseup="src='Bitmaps/Controls/measure_over.bmp';"
>onmousedown="src='Bitmaps/Controls/measure_pressed.bmp';"
>id="measure" onmouseover="src='Bitmaps/Controls/measure_over.bmp';"
>style="LEFT: 170px; WIDTH: 24px; POSITION: absolute; TOP: 2px; HEIGHT: 25px
"
>onclick="buttonClicked(this.id);"
>onmouseout="src='Bitmaps/Controls/measure_btn.bmp';"
>alt="" src="http://pics.10026.com/?src=Bitmaps/Controls/measure_btn.bmp">
>This is working the way I want to, but I can't disable it as I can with a
>button. I then replace the IMG with:
>INPUT Type="image"
>and it makes a postback every time I click on it. Why? A HTML control
>should't do that, right? The script itself (buttonClicked) just fills a
>hidden field.
>/Miro

strange label quirk upon a postback

Have a page page1.aspx that has a label1 with text properties set say arial with font size of "large" there is a button on Page1.asp that upon its click event triggers javascript to open another window Page2.aspx. This causes a postback to occur on Page1.aspx as Page2.aspx is opened. If I dismiss Page2.aspx label1 on Page1.aspx has changed to a much larger font that previously set??? Now if I delete lable1 and place another label on the Page1 form and do not set any font properties the issue does not occur ...the font, it remains unchanged. Seems in a labels default mode, when first dragged on a webform the font properties are not set...at least this seems apparently so when viewing them in the properties window.

Anyone have any ideas...strange behaviour that seems to have no apparent answer...yet

Thanks

So you're saying the label's styling changes afterpostback? Is there another class or some style attribute beingassigned to the label, or something (like a span or table) thatsurrounds the label?

Ifwe could see the page in action, we might have a better idea of what'sgoing on; have you tried View Source to see if anything looks differentin the HTML?


As far as I beleive, check the style sheet. You might be applying dynamic style to that particular label. Try changing the name of the label and execute the same code.

Tuesday, March 13, 2012

strange postback/ SQL problem

I have a datagrid which populates from a SQLDataSource. The page is to default to filter the data as the user that is logged on but there is a drop down menu to select to filter by another user. The drop down menu is autopostback and I have the following code in my on page load event:

 Dim teamleader As String Dim currentyear As String = DateTime.Now.Year 'find the current year
 If Page.IsPostBack Then 'pickup the selected teamleader from drop down menu Dim placeholder As ContentPlaceHolder = CType(Master.FindControl("bodybox"), ContentPlaceHolder) Dim thelist As DropDownList = CType(placeholder.FindControl("dropdownlist1"), DropDownList) teamleader = thelist.Text.ToString 'default to logged in persons name Else teamleader = User.Identity.Name.ToString End If teamdata.SelectCommand = "SELECT username, name, holiday_entitlement_" & currentyear & " as holiday_entitlement, holiday_used_" & currentyear & " as holiday_used, holiday_entitlement_" & currentyear & " - holiday_used_" & currentyear & " AS holiday_left FROM holiday.reps WHERE (teamleader = '" & teamleader & "') AND (hasleft = 'N') ORDER BY name;"

The problem is that if the page isnt postback and the user logged in is "ch12" then I get a populated datagrid. If however the page is a postback the datagrid is showing the empty template. I have looked at the SQL the page is generating and as far as I see they are identical:

Not postback:

SELECT username,name, holiday_entitlement_2006as holiday_entitlement, holiday_used_2006as holiday_used, holiday_entitlement_2006 - holiday_used_2006AS holiday_leftFROM holiday.repsWHERE (teamleader ='ch12')AND (hasleft ='N')ORDER BY name;

Is postback:

SELECT username,name, holiday_entitlement_2006as holiday_entitlement, holiday_used_2006as holiday_used, holiday_entitlement_2006 - holiday_used_2006AS holiday_leftFROM holiday.repsWHERE (teamleader ='ch12')AND (hasleft ='N')ORDER BY name;

Any ideas why one datagrid is appearing empty and the other populated when the select command being generated appears to be identical?

Anyone have any idea on this at all?

Set the datasource to the Grid in the SelectedIndexChanged event of the dropdownlist. Make sure you populate the dropdownlist with PostBack check in the page_load.

page_load() {

if (!Page.IsPostBack) {
// load the dropdownlist
// assign datasource and bind grid -- default
}
}

selectedIndexChanged event of dropdownlist {
// assign datasource and bind grid
}

Thanks


Have worked out the problem but not the fix. I am correctly creating the new sqldatasource1.selectcommand on the postback however the datagrid is always displaying the results from the original page load.

How do I make the page discard the original results and re run the query on each page load (in VB)?

Many thanks



1<%@. Page Language="VB" MasterPageFile="~/loggedin/the.master" %>23<script runat="server">45Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs6Dim teamleader as string7Dim currentyearAs String = DateTime.Now.Year8If Not User.IsInRole("teamleader")Then9 Try10 Dim placeholderAs ContentPlaceHolder =CType(Master.FindControl("bodybox"), ContentPlaceHolder)11Dim thelistAs DropDownList =CType(placeholder.FindControl("dropdownlist1"), DropDownList)12 teamleader = thelist.Text.ToString'default to logged in persons name13If teamleader ="."Then teamleader = User.Identity.Name.ToString14Catch exAs Exception15 teamleader = User.Identity.Name.ToString16End Try17 Else18 teamleader = User.Identity.Name.ToString19End If2021 teamdata.SelectCommand ="SELECT username, name, holiday_entitlement_" & currentyear &" as holiday_entitlement, holiday_used_" & currentyear &" as holiday_used, holiday_entitlement_" & currentyear &" - holiday_used_" & currentyear &" AS holiday_left FROM holiday.reps WHERE (teamleader ='" & teamleader & "') AND (hasleft = 'N') ORDER BY name;"22 End Sub23</script>2425<asp:Content ID="Content1" ContentPlaceHolderID="bodybox" Runat="Server">2627 <asp:DropDownList ID="dropdownlist1" runat="server" DataSourceID="teamleaderlist" DataTextField="Username" DataValueField="Username" AutoPostBack="true" AppendDataBoundItems="True" >28 <asp:ListItem Selected="True" Value="." Text=""></asp:ListItem></asp:DropDownList>29 <asp:SqlDataSource ID="teamleaderlist" runat="server" ConnectionString="<%$ ConnectionStrings:holidaybookingConnectionString %>" ProviderName="<%$ ConnectionStrings:holidaybookingConnectionString.ProviderName %>"></asp:SqlDataSource>3031<asp:SqlDataSource ID="teamdata" runat="server" ConnectionString="<%$ ConnectionStrings:holidaybookingConnectionString %>" ProviderName="<%$ ConnectionStrings:holidaybookingConnectionString.ProviderName %>" ></asp:SqlDataSource>3233<asp:GridView ID="teamview" runat="server" AllowSorting="True"34 DataKeyNames="username" DataSourceID="teamdata" CellPadding="5" GridLines="None" HorizontalAlign="Center">35 <EmptyDataTemplate>36 There are currently no Reps to display37 </EmptyDataTemplate>38 </asp:GridView>39</asp:content>
Above is the code for the problem area - I havent posted the code generating the SQL for the dropdownmenu as it is rather long and convoluted and is working fine.

Post your code!

Thanks


DropDownList onselectedIndexchanged will cause postback and your page_load code executes everytime it is posted. So you are getting the same values. Add a postback check in the page_load. Where in the page_load you are binding the grid?? In the SelectIndexChanged event again you need to write the code for the selected userRole


5Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs
6Dim teamleader as string
7Dim currentyearAs String = DateTime.Now.Year
If Not Page.IsPostBack Then
8If Not User.IsInRole("teamleader")Then
9 Try
10 Dim placeholderAs ContentPlaceHolder =CType(Master.FindControl("bodybox"), ContentPlaceHolder)
11Dim thelistAs DropDownList =CType(placeholder.FindControl("dropdownlist1"), DropDownList)
12 teamleader = thelist.Text.ToString'default to logged in persons name
13If teamleader ="."Then teamleader = User.Identity.Name.ToString
14Catch exAs Exception
15 teamleader = User.Identity.Name.ToString
16End Try
17 Else
18 teamleader = User.Identity.Name.ToString
19End If
20
21 teamdata.SelectCommand ="SELECT username, name, holiday_entitlement_" & currentyear &" as holiday_entitlement, holiday_used_" & currentyear &" as holiday_used, holiday_entitlement_" & currentyear &" - holiday_used_" & currentyear &" AS holiday_left FROM holiday.reps WHERE (teamleader ='" & teamleader & "') AND (hasleft = 'N') ORDER BY name;"
End If
protected void DropdownList1_SelectedIndexChanged(object sender, EventArgs e)
{

6Dim teamleader as string
7Dim currentyearAs String = DateTime.Now.Year
8If Not User.IsInRole("teamleader")Then
9 Try
10 Dim placeholderAs ContentPlaceHolder =CType(Master.FindControl("bodybox"), ContentPlaceHolder)
11Dim thelistAs DropDownList =CType(placeholder.FindControl("dropdownlist1"), DropDownList)
12 teamleader = thelist.Text.ToString'default to logged in persons name
13If teamleader ="."Then teamleader = User.Identity.Name.ToString
14Catch exAs Exception
15 teamleader = User.Identity.Name.ToString
16End Try
17 Else
18 teamleader = User.Identity.Name.ToString
19End If
20
21 teamdata.SelectCommand ="SELECT username, name, holiday_entitlement_" & currentyear &" as holiday_entitlement, holiday_used_" & currentyear &" as holiday_used, holiday_entitlement_" & currentyear &" - holiday_used_" & currentyear &" AS holiday_left FROM holiday.reps WHERE (teamleader ='" & teamleader & "') AND (hasleft = 'N') ORDER BY name;"

}
Also try using the dropDownList.SelectedItem.Value rather the .Text when selecting values fromthe dropdownlist.

Thanks
Many thanks, all works great now

strange postback problem

I have a form with a Submit button. The user fills out the form,
clicks the submit button, and then the page posts back to itself.
Every time the page posts back to itself, the "Page_Load" event fires.
So if the user first types the URL of the page in the browser, the
"Page_Load" event fires. Then he hits submit, and the form contents
are posted back, so the "Page_Load" event fires again. The form
appears again (I clear its contents), and he can fill it again, click
submit, and the "Page_Load" event fires again.
I also have a validator control that gives an error message if the user
leaves a field blank. So if the user clicks submit without filling in
a field, the submit code checks "If Page.IsValid()" and puts an error
message on the page. I find that the "Page_Load" event is fired for
this too. In other words, if the form is incorrect and the user clicks
submit, the "Page_Load" event fires, probably just to put the error
message on the page.
Now in the Page_Load event, I want to store the form's contents in a
database. To do that, I have to know why Page_Load is firing. I don't
want to store the form contents if Page.IsPostBack is False, because
that means this is the first time the page is viewed, and the form
hasn't been filled out yet. I also don't want to store the form
contents if there is an error on the page, even though Page_Load is
fired in this case as well.
So I thought the easy way to to this would be to create a Session
variable that tells me if the submit button found the form fields to be
valid. The submit code would look like this:
''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.Text = "IS VALID"
Session("Valid") = True
Else
LabDiagnostic.Text = "NOT VALID"
Session("Valid") = False
End If
If Session("Valid") = True Then
LabDiagnostic2.Text = "Session('Valid') = True"
Else
LabDiagnostic2.Text = "Session('Valid') = False"
End If
End Sub
Notice that I put in Diagnostics just to be sure the code is working.
The diagnostics do show that it works.
Then in the "Page_Load" event, I test Session("Valid") again:
''''
Sub Page_Load(Source As Object, E As EventArgs)

If Page.IsPostBack Then
If Session("Valid") = True Then
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
Else
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
End If
End If
End Sub
''
But this time the diagnostic gives the opposite result than I expect!
This is so strange that I feel I must be doing something obviously
wrong, but I can't tell what it is.
Here is the entire code of the page, in case someone wants to try it
out:
-- Thanks
-- Marvin

<%@dotnet.itags.org.Page Language="VB" debug="true" %>
<%@dotnet.itags.org.Import NameSpace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SqlClient" %
<HTML>
<HEAD>
<TITLE>Enter Passenger Details</TITLE>
</HEAD
<script language="VB" runat="server">
''''
Sub Page_Load(Source As Object, E As EventArgs)

If Page.IsPostBack Then
If Session("Valid") = True Then
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
Else
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
End If
End If
End Sub
''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.Text = "IS VALID"
Session("Valid") = True
Else
LabDiagnostic.Text = "NOT VALID"
Session("Valid") = False
End If
If Session("Valid") = True Then
LabDiagnostic2.Text = "Session('Valid') = True"
Else
LabDiagnostic2.Text = "Session('Valid') = False"
End If
End Sub
'' ''
</script
<BODY>
<form METHOD="POST" name="MyForm" runat="server">
<ASP:Label id="LabDiagnostic" runat="server" />
<hr>
<ASP:Label id="LabDiagnostic2" runat="server" />
<hr>
<ASP:Label id="LabDiagnostic3" runat="server" />
<center>
<table border="1">
<tr>
<td align="right">First Name</td>
<td><ASP:Textbox id="FirstName" MaxLength="40" Columns="40"
runat="server" />
<ASP:RequiredFieldValidator id="rfvFirstName" runat="server"
ControlToValidate="FirstName"
ErrorMessage="* You must enter a first name."
Display="dynamic">
*
</ASP:RequiredFieldValidator
</td>
</tr>
<tr><td align="center" colspan="2">
<asp:Button id="MyButton" Text="Submit" runat="server"
OnClick="SubmitClick" />
</td></tr
</table>
</center>
<center>
<table><tr><td>
<ASP:ValidationSummary id="valSummary" runat="server"
HeaderText = "<b>The following errors were found:</b>"
DisplayMode="List" ShowSummary="True" ShowMessageBox="True" />
<!-- normally I have ShowSummary="False" -->
</td></tr></table>
</center>
</form>
</BODY>
</HTMLThe Page_Load event allways fires. When the page loads off it goes, every
time, no exception.

> Now in the Page_Load event, I want to store the form's contents in a
> database. To do that, I have to know why Page_Load is firing. I don't
> want to store the form contents if Page.IsPostBack is False, because
> that means this is the first time the page is viewed, and the form
> hasn't been filled out yet. I also don't want to store the form
> contents if there is an error on the page, even though Page_Load is
> fired in this case as well.
> So I thought the easy way to to this would be to create a Session
> variable that tells me if the submit button found the form fields to be
> valid. The submit code would look like this:

This is not that difficult really. The validators which operate client side
are your first attempt at ensuring that the data is consistant with what you
would like the user to do with it. The validators on the client side will
stop form submission if there criteria is not met.

When the page loads you can test to see if it is a postback and then further
validate from there.

if Page.IsPostback Then

if MyValidationFunction() Then

'Store My Data

Else

litMessage.Text = "Your form has an error on it . . . Blah Blah Blah "

End If

End If

--
Best Regards

The Inimitable Mr Newbie
"COHENMARVIN" <cohenmarvin@.hotmail.com> wrote in message
news:1131192470.623229.323670@.g49g2000cwa.googlegr oups.com...
>I have a form with a Submit button. The user fills out the form,
> clicks the submit button, and then the page posts back to itself.
> Every time the page posts back to itself, the "Page_Load" event fires.
> So if the user first types the URL of the page in the browser, the
> "Page_Load" event fires. Then he hits submit, and the form contents
> are posted back, so the "Page_Load" event fires again. The form
> appears again (I clear its contents), and he can fill it again, click
> submit, and the "Page_Load" event fires again.
> I also have a validator control that gives an error message if the user
> leaves a field blank. So if the user clicks submit without filling in
> a field, the submit code checks "If Page.IsValid()" and puts an error
> message on the page. I find that the "Page_Load" event is fired for
> this too. In other words, if the form is incorrect and the user clicks
> submit, the "Page_Load" event fires, probably just to put the error
> message on the page.
> Now in the Page_Load event, I want to store the form's contents in a
> database. To do that, I have to know why Page_Load is firing. I don't
> want to store the form contents if Page.IsPostBack is False, because
> that means this is the first time the page is viewed, and the form
> hasn't been filled out yet. I also don't want to store the form
> contents if there is an error on the page, even though Page_Load is
> fired in this case as well.
> So I thought the easy way to to this would be to create a Session
> variable that tells me if the submit button found the form fields to be
> valid. The submit code would look like this:
> ''
> Sub SubmitClick(objSender As Object, objArgs As EventArgs)
> If Page.IsValid() Then
> LabDiagnostic.Text = "IS VALID"
> Session("Valid") = True
> Else
> LabDiagnostic.Text = "NOT VALID"
> Session("Valid") = False
> End If
> If Session("Valid") = True Then
> LabDiagnostic2.Text = "Session('Valid') = True"
> Else
> LabDiagnostic2.Text = "Session('Valid') = False"
> End If
> End Sub
> Notice that I put in Diagnostics just to be sure the code is working.
> The diagnostics do show that it works.
> Then in the "Page_Load" event, I test Session("Valid") again:
> ''''
> Sub Page_Load(Source As Object, E As EventArgs)
> If Page.IsPostBack Then
> If Session("Valid") = True Then
> LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
> Else
> LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
> End If
> End If
> End Sub
> ''
> But this time the diagnostic gives the opposite result than I expect!
> This is so strange that I feel I must be doing something obviously
> wrong, but I can't tell what it is.
> Here is the entire code of the page, in case someone wants to try it
> out:
> -- Thanks
> -- Marvin
> <%@.Page Language="VB" debug="true" %>
> <%@.Import NameSpace="System.Data" %>
> <%@. Import Namespace="System.Data.SqlClient" %>
> <HTML>
> <HEAD>
> <TITLE>Enter Passenger Details</TITLE>
> </HEAD>
> <script language="VB" runat="server">
> ''''
> Sub Page_Load(Source As Object, E As EventArgs)
> If Page.IsPostBack Then
> If Session("Valid") = True Then
> LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
> Else
> LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
> End If
> End If
> End Sub
> ''
> Sub SubmitClick(objSender As Object, objArgs As EventArgs)
> If Page.IsValid() Then
> LabDiagnostic.Text = "IS VALID"
> Session("Valid") = True
> Else
> LabDiagnostic.Text = "NOT VALID"
> Session("Valid") = False
> End If
> If Session("Valid") = True Then
> LabDiagnostic2.Text = "Session('Valid') = True"
> Else
> LabDiagnostic2.Text = "Session('Valid') = False"
> End If
> End Sub
> '' ''
> </script>
> <BODY>
> <form METHOD="POST" name="MyForm" runat="server">
> <ASP:Label id="LabDiagnostic" runat="server" />
> <hr>
> <ASP:Label id="LabDiagnostic2" runat="server" />
> <hr>
> <ASP:Label id="LabDiagnostic3" runat="server" />
> <center>
> <table border="1">
> <tr>
> <td align="right">First Name</td>
> <td><ASP:Textbox id="FirstName" MaxLength="40" Columns="40"
> runat="server" />
> <ASP:RequiredFieldValidator id="rfvFirstName" runat="server"
> ControlToValidate="FirstName"
> ErrorMessage="* You must enter a first name."
> Display="dynamic">
> *
> </ASP:RequiredFieldValidator>
> </td>
> </tr>
> <tr><td align="center" colspan="2">
> <asp:Button id="MyButton" Text="Submit" runat="server"
> OnClick="SubmitClick" />
> </td></tr>
> </table>
> </center>
> <center>
> <table><tr><td>
> <ASP:ValidationSummary id="valSummary" runat="server"
> HeaderText = "<b>The following errors were found:</b>"
> DisplayMode="List" ShowSummary="True" ShowMessageBox="True" />
> <!-- normally I have ShowSummary="False" -->
> </td></tr></table>
> </center>
> </form>
> </BODY>
> </HTML
It seems that my problem was that I assumed the "Submit" button fires
its event before the "Page_Load" event. I had a form with a "Submit"
button, and I thought that when I clicked the "submit" button, the
first thing that would happen was that the code in the "submit" button
event would be executed, and only after that would a "Page_Load" be
executed. But the converse is true, when you click a "submit" button,
the first thing that happens is a "Page_Load", and only after that does
the server execute the code of the "submit" button event.
This is true using the "OnClick" event, the "OnServerClick" event
appears to be radically different, I don't know why.
-- Marvin

strange postback problem

I have a form with a Submit button. The user fills out the form,
clicks the submit button, and then the page posts back to itself.
Every time the page posts back to itself, the "Page_Load" event fires.
So if the user first types the URL of the page in the browser, the
"Page_Load" event fires. Then he hits submit, and the form contents
are posted back, so the "Page_Load" event fires again. The form
appears again (I clear its contents), and he can fill it again, click
submit, and the "Page_Load" event fires again.
I also have a validator control that gives an error message if the user
leaves a field blank. So if the user clicks submit without filling in
a field, the submit code checks "If Page.IsValid()" and puts an error
message on the page. I find that the "Page_Load" event is fired for
this too. In other words, if the form is incorrect and the user clicks
submit, the "Page_Load" event fires, probably just to put the error
message on the page.
Now in the Page_Load event, I want to store the form's contents in a
database. To do that, I have to know why Page_Load is firing. I don't
want to store the form contents if Page.IsPostBack is False, because
that means this is the first time the page is viewed, and the form
hasn't been filled out yet. I also don't want to store the form
contents if there is an error on the page, even though Page_Load is
fired in this case as well.
So I thought the easy way to to this would be to create a Session
variable that tells me if the submit button found the form fields to be
valid. The submit code would look like this:
''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.Text = "IS VALID"
Session("Valid") = True
Else
LabDiagnostic.Text = "NOT VALID"
Session("Valid") = False
End If
If Session("Valid") = True Then
LabDiagnostic2.Text = "Session('Valid') = True"
Else
LabDiagnostic2.Text = "Session('Valid') = False"
End If
End Sub
Notice that I put in Diagnostics just to be sure the code is working.
The diagnostics do show that it works.
Then in the "Page_Load" event, I test Session("Valid") again:
''''
Sub Page_Load(Source As Object, E As EventArgs)
If Page.IsPostBack Then
If Session("Valid") = True Then
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
Else
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
End If
End If
End Sub
''''
'''
But this time the diagnostic gives the opposite result than I expect!
This is so strange that I feel I must be doing something obviously
wrong, but I can't tell what it is.
Here is the entire code of the page, in case someone wants to try it
out:
-- Thanks
-- Marvin
<%@dotnet.itags.org.Page Language="VB" debug="true" %>
<%@dotnet.itags.org.Import NameSpace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SqlClient" %>
<HTML>
<HEAD>
<TITLE>Enter Passenger Details</TITLE>
</HEAD>
<script language="VB" runat="server">
''''
Sub Page_Load(Source As Object, E As EventArgs)
If Page.IsPostBack Then
If Session("Valid") = True Then
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
Else
LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
End If
End If
End Sub
''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
If Page.IsValid() Then
LabDiagnostic.Text = "IS VALID"
Session("Valid") = True
Else
LabDiagnostic.Text = "NOT VALID"
Session("Valid") = False
End If
If Session("Valid") = True Then
LabDiagnostic2.Text = "Session('Valid') = True"
Else
LabDiagnostic2.Text = "Session('Valid') = False"
End If
End Sub
''''
''
</script>
<BODY>
<form METHOD="POST" name="MyForm" runat="server">
<ASP:Label id="LabDiagnostic" runat="server" />
<hr>
<ASP:Label id="LabDiagnostic2" runat="server" />
<hr>
<ASP:Label id="LabDiagnostic3" runat="server" />
<center>
<table border="1">
<tr>
<td>First Name</td>
<td><ASP:Textbox id="FirstName" MaxLength="40" Columns="40"
runat="server" />
<ASP:RequiredFieldValidator id="rfvFirstName" runat="server"
ControlToValidate="FirstName"
ErrorMessage="* You must enter a first name."
Display="dynamic">
*
</ASP:RequiredFieldValidator>
</td>
</tr>
<tr><td align="center" colspan="2">
<asp:Button id="MyButton" Text="Submit" runat="server"
OnClick="SubmitClick" />
</td></tr>
</table>
</center>
<center>
<table><tr><td>
<ASP:ValidationSummary id="valSummary" runat="server"
HeaderText = "<b>The following errors were found:</b>"
DisplayMode="List" ShowSummary="True" ShowMessageBox="True" />
<!-- normally I have ShowSummary="False" -->
</td></tr></table>
</center>
</form>
</BODY>
</HTML>The Page_Load event allways fires. When the page loads off it goes, every
time, no exception.

> Now in the Page_Load event, I want to store the form's contents in a
> database. To do that, I have to know why Page_Load is firing. I don't
> want to store the form contents if Page.IsPostBack is False, because
> that means this is the first time the page is viewed, and the form
> hasn't been filled out yet. I also don't want to store the form
> contents if there is an error on the page, even though Page_Load is
> fired in this case as well.
> So I thought the easy way to to this would be to create a Session
> variable that tells me if the submit button found the form fields to be
> valid. The submit code would look like this:
This is not that difficult really. The validators which operate client side
are your first attempt at ensuring that the data is consistant with what you
would like the user to do with it. The validators on the client side will
stop form submission if there criteria is not met.
When the page loads you can test to see if it is a postback and then further
validate from there.
if Page.IsPostback Then
if MyValidationFunction() Then
'Store My Data
Else
litMessage.Text = "Your form has an error on it . . . Blah Blah Blah "
End If
End If
Best Regards
The Inimitable Mr Newbie
"COHENMARVIN" <cohenmarvin@.hotmail.com> wrote in message
news:1131192470.623229.323670@.g49g2000cwa.googlegroups.com...
>I have a form with a Submit button. The user fills out the form,
> clicks the submit button, and then the page posts back to itself.
> Every time the page posts back to itself, the "Page_Load" event fires.
> So if the user first types the URL of the page in the browser, the
> "Page_Load" event fires. Then he hits submit, and the form contents
> are posted back, so the "Page_Load" event fires again. The form
> appears again (I clear its contents), and he can fill it again, click
> submit, and the "Page_Load" event fires again.
> I also have a validator control that gives an error message if the user
> leaves a field blank. So if the user clicks submit without filling in
> a field, the submit code checks "If Page.IsValid()" and puts an error
> message on the page. I find that the "Page_Load" event is fired for
> this too. In other words, if the form is incorrect and the user clicks
> submit, the "Page_Load" event fires, probably just to put the error
> message on the page.
> Now in the Page_Load event, I want to store the form's contents in a
> database. To do that, I have to know why Page_Load is firing. I don't
> want to store the form contents if Page.IsPostBack is False, because
> that means this is the first time the page is viewed, and the form
> hasn't been filled out yet. I also don't want to store the form
> contents if there is an error on the page, even though Page_Load is
> fired in this case as well.
> So I thought the easy way to to this would be to create a Session
> variable that tells me if the submit button found the form fields to be
> valid. The submit code would look like this:
> ''
> Sub SubmitClick(objSender As Object, objArgs As EventArgs)
> If Page.IsValid() Then
> LabDiagnostic.Text = "IS VALID"
> Session("Valid") = True
> Else
> LabDiagnostic.Text = "NOT VALID"
> Session("Valid") = False
> End If
> If Session("Valid") = True Then
> LabDiagnostic2.Text = "Session('Valid') = True"
> Else
> LabDiagnostic2.Text = "Session('Valid') = False"
> End If
> End Sub
> Notice that I put in Diagnostics just to be sure the code is working.
> The diagnostics do show that it works.
> Then in the "Page_Load" event, I test Session("Valid") again:
> ''''
> Sub Page_Load(Source As Object, E As EventArgs)
> If Page.IsPostBack Then
> If Session("Valid") = True Then
> LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
> Else
> LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
> End If
> End If
> End Sub
> ''''
'''
> But this time the diagnostic gives the opposite result than I expect!
> This is so strange that I feel I must be doing something obviously
> wrong, but I can't tell what it is.
> Here is the entire code of the page, in case someone wants to try it
> out:
> -- Thanks
> -- Marvin
> <%@.Page Language="VB" debug="true" %>
> <%@.Import NameSpace="System.Data" %>
> <%@. Import Namespace="System.Data.SqlClient" %>
> <HTML>
> <HEAD>
> <TITLE>Enter Passenger Details</TITLE>
> </HEAD>
> <script language="VB" runat="server">
> ''''
> Sub Page_Load(Source As Object, E As EventArgs)
> If Page.IsPostBack Then
> If Session("Valid") = True Then
> LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = True"
> Else
> LabDiagnostic3.Text = "PAGE_LOAD: Session('Valid') = False"
> End If
> End If
> End Sub
> ''
> Sub SubmitClick(objSender As Object, objArgs As EventArgs)
> If Page.IsValid() Then
> LabDiagnostic.Text = "IS VALID"
> Session("Valid") = True
> Else
> LabDiagnostic.Text = "NOT VALID"
> Session("Valid") = False
> End If
> If Session("Valid") = True Then
> LabDiagnostic2.Text = "Session('Valid') = True"
> Else
> LabDiagnostic2.Text = "Session('Valid') = False"
> End If
> End Sub
> ''''
''
> </script>
> <BODY>
> <form METHOD="POST" name="MyForm" runat="server">
> <ASP:Label id="LabDiagnostic" runat="server" />
> <hr>
> <ASP:Label id="LabDiagnostic2" runat="server" />
> <hr>
> <ASP:Label id="LabDiagnostic3" runat="server" />
> <center>
> <table border="1">
> <tr>
> <td>First Name</td>
> <td><ASP:Textbox id="FirstName" MaxLength="40" Columns="40"
> runat="server" />
> <ASP:RequiredFieldValidator id="rfvFirstName" runat="server"
> ControlToValidate="FirstName"
> ErrorMessage="* You must enter a first name."
> Display="dynamic">
> *
> </ASP:RequiredFieldValidator>
> </td>
> </tr>
> <tr><td align="center" colspan="2">
> <asp:Button id="MyButton" Text="Submit" runat="server"
> OnClick="SubmitClick" />
> </td></tr>
> </table>
> </center>
> <center>
> <table><tr><td>
> <ASP:ValidationSummary id="valSummary" runat="server"
> HeaderText = "<b>The following errors were found:</b>"
> DisplayMode="List" ShowSummary="True" ShowMessageBox="True" />
> <!-- normally I have ShowSummary="False" -->
> </td></tr></table>
> </center>
> </form>
> </BODY>
> </HTML>
>
It seems that my problem was that I assumed the "Submit" button fires
its event before the "Page_Load" event. I had a form with a "Submit"
button, and I thought that when I clicked the "submit" button, the
first thing that would happen was that the code in the "submit" button
event would be executed, and only after that would a "Page_Load" be
executed. But the converse is true, when you click a "submit" button,
the first thing that happens is a "Page_Load", and only after that does
the server execute the code of the "submit" button event.
This is true using the "OnClick" event, the "OnServerClick" event
appears to be radically different, I don't know why.
-- Marvin