?
Solved

DropDownList problem  SelectIndexChanged event not being fired.

Posted on 2005-05-12
11
Medium Priority
?
394 Views
Last Modified: 2010-04-07
------------------------------------------------------------------------------------------------------------------------------
.aspx file.

                        <asp:TableRow>
                              <asp:TableCell BorderWidth="1" ColumnSpan="10">
                                    <asp:DropDownList id="DropDownList1" runat="server" Width="856px"></asp:DropDownList>
                              </asp:TableCell>
                        </asp:TableRow>

------------------------------------------------------------------------------------------------------------------------------
.aspx.vb file:

    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList

------------------------------------------------------------------------------------------------------------------------------
Comment: This is my function that add items to the dropdown list
    Private Sub btnSaveSPSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveSPSQL.Click

        If txtSP_SQL.Text.Trim.Length > 0 Then
            If txtDBNumber.Text.Trim.Length > 0 Then
                DropDownList1.Items.Add(txtDBNumber.Text & " | " & txtSP_SQL.Text)
            Else
                MsgBox("Select Database from Master list.", MsgBoxStyle.Exclamation, "Saving SP/SQL Query")
            End If
        Else
            MsgBox("No SP/SQL Query found", MsgBoxStyle.Exclamation, "Saving SP/SQL Query")
        End If

    End Sub
------------------------------------------------------------------------------------------------------------------------------

Comment: This is my function that is supposed to react when I select one to the items in the list and populate a couple of textboxes.  Right now, nothing happen.

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

        Dim s As String = DropDownList1.Items.Item(DropDownList1.SelectedIndex).Text
        Dim p As Integer = s.IndexOf("|")

        txtDBNumber.Text = s.Substring(0, p - 1).Trim
        txtSP_SQL.Text = s.Substring(p + 1).Trim

    End Sub

------------------------------------------------------------------------------------------------------------------------------

Any suggestions on why this event is not being fired.  
I really appreciate your input.


0
Comment
Question by:EdCariello
  • 6
  • 3
  • 2
11 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 13990304
<asp:DropDownList id="DropDownList1" runat="server" Width="856px" AutoPostBack="True"></asp:DropDownList>

You must set the AutoPostBack to true
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13990307
By default it is False.
0
 
LVL 6

Expert Comment

by:dharmesh_amity
ID: 13990315
I think the event must be firing. If you don't have autopostback=true for a dropdown box than what happens is that the page is not posted back immediately, but rather the events are placed in the queue. So when the page is postback the ASP.NET processes all the events in the queue one by one and at that time this event handler will execute.

So the bottom line is if you want a postback immediately when the selected index is changed, then do this

<asp:DropDownList id="DropDownList1" runat="server" Width="856px" AutoPostBack="True"></asp:DropDownList>

I added AutoPostBack="True".
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:EdCariello
ID: 13990649
Now the event is being fired, but the whole page is being reload it.  Is there a way to add to the dropdownlist box without involving the server?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13990727
yes thru javascript (client-side) but this gets sent down on the first time. After that a postback/reload is needed.
0
 

Author Comment

by:EdCariello
ID: 13990809
Can a postback be omitted for a particular control while getting the benefits of a page event.?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13990846
firstly you can avoid re-populating controls in your Load event

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  If Not IsPostBack Then
    'initial population of data
    'upon first load
  End If

End Sub
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13990864
secondly, the pageevent will still fire (it's at the page level and not the control level), Postback is needed if you want to make use of the WebForm features. If there is no <form runat="server">...</form> then you may wish to dispense with the postback since it can;t occur now.
0
 

Author Comment

by:EdCariello
ID: 13990983
Every time something is clicked, the whole page is reloaded and the position on the scroll bar is lost. It seems that something big happen when it only a selection/click was done within the page with no server side effect.  
I am still confused about ASP.NET or the approach I am taking.  
I do not intent the page to jump up and down at events.
I am going home now but we will continue tomorrow.  

>>>>> Thank you for your input <<<<<<<
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 2000 total points
ID: 13991014
no problems =)
0
 
LVL 6

Expert Comment

by:dharmesh_amity
ID: 13995313
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

        Dim s As String = DropDownList1.Items.Item(DropDownList1.SelectedIndex).Text
        Dim p As Integer = s.IndexOf("|")

        txtDBNumber.Text = s.Substring(0, p - 1).Trim
        txtSP_SQL.Text = s.Substring(p + 1).Trim

    End Sub

You can do this in javascript without going to server side scripting. As I see in your code here you do not need to get any information from server when dropdown selection is changed. I would suggest you to do this operation on client side script with javascript.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question