Triggering event, listbox

S

Steve Schroeder

For some reason I cannot get the OnSelectedIndexChanged event to fire for a
listbox I have on a page. I'm able to populate the listbox with data from a
stored procedure, but cannot trigger the event. I do have EnableViewState =
True for the listbox. I'm imagining (wanting) to populate the listbox named:
lstNames by using the DataValueField from lstDepartments.

It's quite apparent though that the event is not triggering as I can misname
the stored procedure and no error occurs.

Here is the code, I'm sure it will wrap terribly however::

MGSurvey.aspx:

<FORM id="MGSurvey" runat="server">
<p style="Z-INDEX: 101; LEFT: 15px; POSITION: relative">
<asp:listbox id="lstDepartments" style="Z-INDEX: 102; POSITION: relative;
TOP: 6px" runat="server" EnableViewState="true" OnSelectedIndexChanged =
"lstDepartments_SelectedIndexChanged" Width="258px"
Height="118px"></asp:listbox>
<asp:panel id="pnDepartmentMembers" style="Z-INDEX: 103; POSITION: relative"
runat="server" Width="559px" Height="144px" BackColor="Transparent"
BorderColor="Transparent">
<asp:listbox id="lstNames" style="Z-INDEX: 103; LEFT: 15px; POSITION:
relative; TOP: 20px" runat="server" Width="258px" EnableViewState="True"
BackColor="Transparent"></asp:listbox>
</asp:panel>
</p>
</FORM>

MGSurvey.aspx.vb:

Public Sub lstDepartments_SelectedIndexChanged(ByVal sender As Object, ByVal
e As EventArgs)

If (Not IsPostBack) Then
adoConn.Open()
Dim adoCommDeptMembers As New SqlCommand("dbo.MG_EmployeesByDepartment "
& lstDepartments.SelectedItem.Value, adoConn)

With lstNames
.DataSource =
adoCommDeptMembers.ExecuteReader(CommandBehavior.CloseConnection)
.DataTextField = "Name"
.DataValueField = "tp_ID"
.DataBind()
End With
adoConn.Close()

End If

End Sub
 
M

Marina

How is the user triggering a postback to the server?

What happens when the page posts back to the server? What does it all look
like?
 
S

Steve Schroeder

Well, I have two listboxes.

lstDepartments - Lists Departments
lstNames - Lists Members of Departments

On Page Load lstDepartments is loaded with a list of departments, with the
department 'code' as the DataValueField.

What I want to happen is, when you click on an item in the list of
departments, lstNames gets populated with the members of that department.

Stored procedures worked, tested them, they have the proper permissions,
etc.

Just can't seem to get OnSelectedIndexChanged to fire...I've tried with 'Not
IsPostBack' and without it...not sure how else to answer your
question...thanks! :)
 
M

Marina

You didn't really answer my questions at all, so I'm sorry but I don't have
any ideas on how to help you.
 
S

Steve Schroeder

"How is the user triggering a postback to the server?" - Postback, as I understand it is triggered by reloading the page. In my case, this is not happening. All the user should be doing is selecting with their mouse and item in the listbox, and hopefully triggering the OnSelectedIndexChanged event of the listbox named lstDepartments.

"What happens when the page posts back to the server? What does it all look like?" - Nothing happens, the first listbox flickers when you select an item, and the second listbox remains empty, and no error message occurs.
 
M

Marina

Ok, so the problem is that there is no postback to the server!

A server side event can't run, unless something forces a postback to the server. Your page is sitting in the browser - whatever objects the server used to generate the page are long gone. HTTP is a connectionless protocol - it's just the browser on its own.

So you need something there to trigger an event to the server.

I think the listbox may be one of the objects that has an AutoPostback property. If it does, setting this to True, will trigger a postback to the server whenever the selected item is changed.
If the listbox does not have such a property, you need to trigger a postback. It may be that there is a button the user has to click to proceed, or you may have to write some javascript to trigger the post.
"How is the user triggering a postback to the server?" - Postback, as I understand it is triggered by reloading the page. In my case, this is not happening. All the user should be doing is selecting with their mouse and item in the listbox, and hopefully triggering the OnSelectedIndexChanged event of the listbox named lstDepartments.

"What happens when the page posts back to the server? What does it all look like?" - Nothing happens, the first listbox flickers when you select an item, and the second listbox remains empty, and no error message occurs.
 
S

Steve Schroeder

Well I'll be damned...that worked! Thank you!

For future reference...when would I not want to do a postback, and if 90% of the time people do postback, why isn't it the default behavior? I would have gnashed my teeth over that one indefinately...
Ok, so the problem is that there is no postback to the server!

A server side event can't run, unless something forces a postback to the server. Your page is sitting in the browser - whatever objects the server used to generate the page are long gone. HTTP is a connectionless protocol - it's just the browser on its own.

So you need something there to trigger an event to the server.

I think the listbox may be one of the objects that has an AutoPostback property. If it does, setting this to True, will trigger a postback to the server whenever the selected item is changed.
If the listbox does not have such a property, you need to trigger a postback. It may be that there is a button the user has to click to proceed, or you may have to write some javascript to trigger the post.
"How is the user triggering a postback to the server?" - Postback, as I understand it is triggered by reloading the page. In my case, this is not happening. All the user should be doing is selecting with their mouse and item in the listbox, and hopefully triggering the OnSelectedIndexChanged event of the listbox named lstDepartments.

"What happens when the page posts back to the server? What does it all look like?" - Nothing happens, the first listbox flickers when you select an item, and the second listbox remains empty, and no error message occurs.
 
M

Marina

Because, who is to say that you always want a postback?

A lot of times, a user is filling out a form, and has many fields to fill out. Then they hit 'Submit', and go to the next page. In this case you would not want to postback automatically. Not only because there is nothing to do until the form is filled out, but because it is a waste of resources, and looks ugly with the screen flashing all the time.

So I would say, that it is not the case that people want to postback most of the time. It really just depends on the type of application and what the UI needs to do.

I think most people would assume that nothing happens by default, because that is what HTML equivalents of these controls would do. And then they add code to change the default behavior.
Well I'll be damned...that worked! Thank you!

For future reference...when would I not want to do a postback, and if 90% of the time people do postback, why isn't it the default behavior? I would have gnashed my teeth over that one indefinately...
Ok, so the problem is that there is no postback to the server!

A server side event can't run, unless something forces a postback to the server. Your page is sitting in the browser - whatever objects the server used to generate the page are long gone. HTTP is a connectionless protocol - it's just the browser on its own.

So you need something there to trigger an event to the server.

I think the listbox may be one of the objects that has an AutoPostback property. If it does, setting this to True, will trigger a postback to the server whenever the selected item is changed.
If the listbox does not have such a property, you need to trigger a postback. It may be that there is a button the user has to click to proceed, or you may have to write some javascript to trigger the post.
"How is the user triggering a postback to the server?" - Postback, as I understand it is triggered by reloading the page. In my case, this is not happening. All the user should be doing is selecting with their mouse and item in the listbox, and hopefully triggering the OnSelectedIndexChanged event of the listbox named lstDepartments.

"What happens when the page posts back to the server? What does it all look like?" - Nothing happens, the first listbox flickers when you select an item, and the second listbox remains empty, and no error message occurs.
 
S

Steve Schroeder

I appreciate the great explanation. Thank you.
Because, who is to say that you always want a postback?

A lot of times, a user is filling out a form, and has many fields to fill out. Then they hit 'Submit', and go to the next page. In this case you would not want to postback automatically. Not only because there is nothing to do until the form is filled out, but because it is a waste of resources, and looks ugly with the screen flashing all the time.

So I would say, that it is not the case that people want to postback most of the time. It really just depends on the type of application and what the UI needs to do.

I think most people would assume that nothing happens by default, because that is what HTML equivalents of these controls would do. And then they add code to change the default behavior.
Well I'll be damned...that worked! Thank you!

For future reference...when would I not want to do a postback, and if 90% of the time people do postback, why isn't it the default behavior? I would have gnashed my teeth over that one indefinately...
Ok, so the problem is that there is no postback to the server!

A server side event can't run, unless something forces a postback to the server. Your page is sitting in the browser - whatever objects the server used to generate the page are long gone. HTTP is a connectionless protocol - it's just the browser on its own.

So you need something there to trigger an event to the server.

I think the listbox may be one of the objects that has an AutoPostback property. If it does, setting this to True, will trigger a postback to the server whenever the selected item is changed.
If the listbox does not have such a property, you need to trigger a postback. It may be that there is a button the user has to click to proceed, or you may have to write some javascript to trigger the post.
"How is the user triggering a postback to the server?" - Postback, as I understand it is triggered by reloading the page. In my case, this is not happening. All the user should be doing is selecting with their mouse and item in the listbox, and hopefully triggering the OnSelectedIndexChanged event of the listbox named lstDepartments.

"What happens when the page posts back to the server? What does it all look like?" - Nothing happens, the first listbox flickers when you select an item, and the second listbox remains empty, and no error message occurs.
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top