Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Theo - 18 Dec 2007 13:20 GMT
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
Cowboy (Gregory A. Beamer) - 18 Dec 2007 17:18 GMT
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.

Signature

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

*************************************************

| Think outside the box!

*************************************************
> Hello,
>
[quoted text clipped - 35 lines]
> myclass.save(ddl1,tb1)
> End Sub
Latish Sehgal - 18 Dec 2007 23:00 GMT
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/
Cowboy (Gregory A. Beamer) - 19 Dec 2007 16:11 GMT
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. :-)

Signature

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

*************************************************

| Think outside the box!

*************************************************
> Why do you have AutoPostBack set to "True" in your textbox?
> That causes the postback to happen everytime the text changes in your
[quoted text clipped - 12 lines]
> -Latish Sehgal
> http://www.dotnetsurfers.com/
Theo - 22 Dec 2007 14:46 GMT
Thanks

>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
[quoted text clipped - 19 lines]
>> -Latish Sehgal
>> http://www.dotnetsurfers.com/
gnewsgroup - 23 Dec 2007 05:43 GMT
On Dec 18, 12:18 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo...@comcast.netNoSpamM> wrote:
> NOTE: Not specific to your problem, but shows an issue with application
> setup.
[quoted text clipped - 59 lines]
> > myclass.save(ddl1,tb1)
> > End Sub

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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.