You have to handle this in Javascript/Client Side. Use the client side event to check if text box is empty.
If yes, do nothing and if no then call __dopostback. In the page_load try catching which control has done postback and based on this you can do the necessary action.
Best Regards,
Abinash Patra
Please mark the post as answer, if you find this useful.
do everything clientside.. why even go server side to check if the textbox is empty? you will be doing a postback on the button click event if you have code in the btn_click event.
hi, actually the btn click is not to check whether the textbox is empty or not. I used that to simplifed the statement. Sorry for the confusion.
The button is actualli inside a userControl1 and when click, it will trigger a onBubbleEvent on the page and hide this userControl1 and show userControl2.
Inside the if statement, if everything OK, it will write data into the database and show userControl2.
So i need to know the code to stop postback if something is not right so that userControl2 will not show and userControl1 will remains.
Can you post your code for the onBubbleEvent? Also depending on where in the code you are hiding UserControl1 and showing UserControl2 you can stop that in the else code....
Protected Overrides Function OnBubbleEvent(ByVal source As Object, ByVal args As System.EventArgs) As Boolean
If source.GetType.Equals(GetType(Button)) Then
Dim oButton As Button = source
Select Case oButton.ID
''Pls use special button ID for the submit button
''or else others button id with the same id will also trigger this
Case "btn1"
UserControl1.Visible = False
UserControl2.Visible = True
Case Else
' Some other button fired the
' event
End Select
End If
RaiseBubbleEvent(source, args)
End Function
hsienfoo
Member
13 Points
27 Posts
stop button postback with if function
Jul 06, 2010 05:47 PM|LINK
Hi all, i have a button that will check whether textbox1.text is empty or not. I have put a if function inside the btn_click event and look like this
If TextBox1.Text <> Nothing Then ''Will COMMIT if everything OK MsgBox("OK Popup userCon2") Else End IfMay i know what shpuld be the code inside the else statement if i do not want the button to create a postback and just do nothing? Thanks:)
abinashpatra
Participant
984 Points
284 Posts
Re: stop button postback with if function
Jul 06, 2010 05:55 PM|LINK
You have to handle this in Javascript/Client Side. Use the client side event to check if text box is empty.
If yes, do nothing and if no then call __dopostback. In the page_load try catching which control has done postback and based on this you can do the necessary action.
Abinash Patra
Please mark the post as answer, if you find this useful.
Varunrao
Member
371 Points
99 Posts
Re: stop button postback with if function
Jul 06, 2010 05:55 PM|LINK
do everything clientside.. why even go server side to check if the textbox is empty? you will be doing a postback on the button click event if you have code in the btn_click event.
There are 2 ways of doing this
1) Javascript
2) Required field validator.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="AllOk"></asp:RequiredFieldValidator>
Also have this line of code in your Button control
ValidationGroup="AllOk"
AllSector Developer
ketan_al
Contributor
6850 Points
1143 Posts
Re: stop button postback with if function
Jul 06, 2010 05:57 PM|LINK
Hi,
Please add following code in your page load
btn.Attributes.Add("onclick","if(document.getElementById('" + TextBox1.ClientID + "').value == "" { return false }");hope this helps
MCP, MCTS ( ASP.NET 3.5 )
Please mark as answer if it helps :)
hsienfoo
Member
13 Points
27 Posts
Re: stop button postback with if function
Jul 06, 2010 06:03 PM|LINK
hi, actually the btn click is not to check whether the textbox is empty or not. I used that to simplifed the statement. Sorry for the confusion.
The button is actualli inside a userControl1 and when click, it will trigger a onBubbleEvent on the page and hide this userControl1 and show userControl2.
Inside the if statement, if everything OK, it will write data into the database and show userControl2.
So i need to know the code to stop postback if something is not right so that userControl2 will not show and userControl1 will remains.
Varunrao
Member
371 Points
99 Posts
Re: stop button postback with if function
Jul 06, 2010 06:07 PM|LINK
Can you post your code for the onBubbleEvent? Also depending on where in the code you are hiding UserControl1 and showing UserControl2 you can stop that in the else code....
AllSector Developer
hsienfoo
Member
13 Points
27 Posts
Re: stop button postback with if function
Jul 06, 2010 06:11 PM|LINK
Hi here is the code for onBubbleEvent.
Varunrao
Member
371 Points
99 Posts
Re: stop button postback with if function
Jul 06, 2010 06:19 PM|LINK
AllSector Developer
hsienfoo
Member
13 Points
27 Posts
Re: stop button postback with if function
Jul 06, 2010 06:26 PM|LINK
Hi, can u guide on how to put an error message on the first userControl. Sorry, i am quite new to ASP.net. Thanks:)
Varunrao
Member
371 Points
99 Posts
Re: stop button postback with if function
Jul 06, 2010 06:30 PM|LINK
Add a Label Control to the UserConrol1 with visibility set to false.
<asp:Label ID="Label1" runat="Server" Visible = "False"/>
On your Case Else
Label1.Visible = True
Label1.Text = "Your error message."
Also make sure you make the label is not visible if everything is right.
AllSector Developer