why do i have to click two times on the button?

T

Theo

Hello,

I want to insert values from a dropdownlist and textbox into a table. The
textbox may be visible only if the selectedvalue of the ddl is "2".
My problem is that i need to click two times on the button before the values
are inserted in the table. I put a response.write to check the value: after
the first click, nothing appears, after the second, they appear and are
inserted.

Why is that?
Thanks
Theo

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack=true >
<asp:ListItem Text="x" Value="1"></asp:ListItem>
<asp:ListItem Text="y" Value="2"></asp:ListItem>
</asp:DropDownList>
<br /> <br />
<asp:TextBox ID="TextBox1" runat="server" Visible="false"
AutoPostBack=true></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="save" />
----------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Page.IsPostBack Then
If DropDownList1.SelectedValue = "2" Then
Textbox1.visible = True
End If
End if
End sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ddl1, tb1 As String
ddl1 = DropDownList1.SelectedValue
tb1 = TextBox1.Text
Response.Write(ddl1 & " " & tb1)
myclass.save(ddl1,tb1)
End Sub
 
C

Cowboy \(Gregory A. Beamer\)

NOTE: Not specific to your problem, but shows an issue with application
setup.

If Page.IsPostBack Then

You should handle postback events in your control event handlers. Unless
every possible action you can ever add to a page does the same thing
(extremely rare), there is no need for the IsPostBack = true branch in the
Page_Load.

I would refactor this test into the button click event. If that is not
possible, then rethink the intent. If the textbox has to be visible to save
to the database, boom, there is your trouble. It is not visible until
someone does something on the page. ANd, if that is the intent, you need to
go through the process and add some validation when someone attempts to
submit via button without the box showing.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
L

Latish Sehgal

Why do you have AutoPostBack set to "True" in your textbox?
That causes the postback to happen everytime the text changes in your
textbox and it loses focus. So what's actually happening here is that
when you press the button, instead of the button handler being fired,
its the autopostback event from textbox.
And when you press the button again (if you do not change text), then
the button handler executes and saves your data.
Removing AutoPostBack=true from your textbox should fix this.

If however, you do want Autopostback to be true for your textbox, you
should create another textbox or some control there, so that when you
click on the new textbox, the autopostback event will fire then and
not block your button event handler.

-Latish Sehgal
http://www.dotnetsurfers.com/
 
C

Cowboy \(Gregory A. Beamer\)

I did not notice that in the OPs code, but that is a good point. It also
causes the If PostBack = True Then to fire every single time you update a
textbox, which kicks you into an interesting loop, albeit one that requires
user interaction. :)

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
T

Theo

Thanks

Cowboy (Gregory A. Beamer) said:
I did not notice that in the OPs code, but that is a good point. It also
causes the If PostBack = True Then to fire every single time you update a
textbox, which kicks you into an interesting loop, albeit one that requires
user interaction. :)

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box! |
*************************************************
 
G

gnewsgroup

NOTE: Not specific to your problem, but shows an issue with application
setup.

If Page.IsPostBack Then

You should handle postback events in your control event handlers. Unless
every possible action you can ever add to a page does the same thing
(extremely rare), there is no need for the IsPostBack = true branch in the
Page_Load.

I would refactor this test into the button click event. If that is not
possible, then rethink the intent. If the textbox has to be visible to save
to the database, boom, there is your trouble. It is not visible until
someone does something on the page. ANd, if that is the intent, you need to
go through the process and add some validation when someone attempts to
submit via button without the box showing.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|

I have always been puzzled by the same kind of problem. It's been 1
year since I noticed that if I use the UpdatePanel of the MS ajax
framework, quite often a button has to be clicked twice to cause a
postback.

I have this problem with one page in my current web application. The
button itself is not inside an UpdatePanel, I do have a few controls
with AutoPostBack=true on the page.

I haven't figured out what is causing this problem. Any idea if this
indeed has to do with the ajax framework?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top