|
-
Nov 10th, 2003, 02:43 PM
#1
Thread Starter
Fanatic Member
[Resolved] Click event for a drop down box on ASP page won't fire
I have autopostback set to true on my drop down box. I have the following code which loads 3 values in my drop down box.
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbPrograms.ClearSelection()
If Not IsPostBack Then
If Request.Cookies("Priveledge").Value = "Administrator" Then
cmbPrograms.Items.Insert(0, New ListItem("PDI Sales", ""))
cmbPrograms.Items.Insert(1, New ListItem("Price Check", ""))
cmbPrograms.Items.Insert(2, New ListItem("RMS Compare", ""))
End If
End If
End Sub
My problem is that I can't seem to get any events to fire when the user selects one of the values in the box. This is the code I'm using.
VB Code:
Private Sub cmbPrograms_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPrograms.SelectedIndexChanged
If cmbPrograms.SelectedItem.Text = "PDI Sales" Then
Response.Redirect("PDI Sales")
End If
End Sub
I placed a break point in here and this event doesn't even fire when I click in the drop down box.
Anyone have any ideas?
Thanks
Last edited by indydavid32; Nov 20th, 2003 at 11:01 AM.
David Wilhelm
-
Nov 10th, 2003, 04:16 PM
#2
PowerPoster
Lets see the HTML side of it.
-
Nov 11th, 2003, 11:50 AM
#3
Thread Starter
Fanatic Member
Here is the HTML code.
VB Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Menu.aspx.vb" Inherits="McClure.Menu"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Menu</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="cmbPrograms" style="Z-INDEX: 108; LEFT: 466px; POSITION: absolute; TOP: 145px" tabIndex="3" runat="server" ForeColor="Black" Font-Bold="True" BackColor="White" AutoPostBack="True"></asp:dropdownlist>
</form>
</body>
</HTML>
Last edited by indydavid32; Nov 11th, 2003 at 11:58 AM.
David Wilhelm
-
Nov 12th, 2003, 09:16 AM
#4
Lively Member
Since PDI Sales is already selected the event doesn't fire, you would need to select a different item and then select the PDI Sales item again.
try this in the page_load event instead
Code:
cmbPrograms.ClearSelection()
If Not IsPostBack Then
If Request.Cookies("Priveledge").Value = "Administrator" Then
cmbPrograms.Items.add(New ListItem("--Select One--", ""))
cmbPrograms.Items.add(New ListItem("PDI Sales", ""))
cmbPrograms.Items.add(New ListItem("Price Check", ""))
cmbPrograms.Items.add(New ListItem("RMS Compare", ""))
End If
End If
now the --Select One-- is the first value to appear, when you select PDI it will change the selection, thus firing the event.
Jason Meckley
Database Analyst
WITF
-
Nov 12th, 2003, 11:37 AM
#5
Lively Member
is force postback set to true in the ddlists properties ?
-
Nov 13th, 2003, 01:13 PM
#6
Thread Starter
Fanatic Member
Since PDI Sales is already selected the event doesn't fire, you would need to select a different item and then select the PDI Sales item again.
No matter which item I select, nothing fires.
is force postback set to true in the ddlists properties ?
What is ddlists properites? The autopostback property is set to True on my drop down box. Is that what you mean?
-
Nov 13th, 2003, 06:21 PM
#7
Aswell as setting autopostback = true you also need to set the OnselectedIndexChanged property of your drop down list.
Code:
<asp:dropdownlist id="cmbPrograms" style="Z-INDEX: 108; LEFT: 466px; POSITION: absolute; TOP: 145px" tabIndex="3"
runat="server" ForeColor="Black" Font-Bold="True" BackColor="White" AutoPostBack="True"
OnSelectedIndexChanged="cmbPrograms_SelectedIndexChanged"></asp:dropdownlist>
-
Nov 14th, 2003, 12:51 AM
#8
PowerPoster
I figured it out after I compared the HTML output. You are supplying a empty string for the value. You have to include something there. If you don't want to supply one, then use the other overloaded method that just accepts the position and text. Then .Net will create the value. Since you were supplying it a non value, it didn't know what to do.
Change these lines to this:
DropDownList1.Items.Insert(0, New ListItem("PDI Sales", "1"))
DropDownList1.Items.Insert(1, New ListItem("Price Check", "2"))
DropDownList1.Items.Insert(2, New ListItem("RMS Compare", "3"))
Or this:
DropDownList1.Items.Insert(0, New ListItem("PDI Sales"))
DropDownList1.Items.Insert(1, New ListItem("Price Check"))
DropDownList1.Items.Insert(2, New ListItem("RMS Compare"))
Or this:
DropDownList1.Items.Add("PDI Sales")
DropDownList1.Items.Add("PriceCheck")
DropDownList1.Items.Add("RMS Compare")
-
Nov 14th, 2003, 12:26 PM
#9
Thread Starter
Fanatic Member
Aswell as setting autopostback = true you also need to set the OnselectedIndexChanged property of your drop down list.
I put that in my line of code and now I get this error message.
Compiler Error Message: BC30456: 'cmbPrograms_SelectedIndexChanged' is not a member of 'ASP.Menu_aspx'.
The name of my web page is Menu.aspx and cmbPrograms is a dropdownbox on this page so I don't get this error message at all.
This the html code
Code:
<asp:dropdownlist id="cmbPrograms" style="Z-INDEX: 107; LEFT: 416px; POSITION: absolute; TOP: 269px" tabIndex="3" runat="server" AutoPostBack="True" onSelectedIndexChanged="cmbPrograms_SelectedIndexChanged"></asp:dropdownlist>
Seems strange that an onclick event for a combo box would be this difficult....
Last edited by indydavid32; Nov 14th, 2003 at 12:30 PM.
David Wilhelm
-
Nov 14th, 2003, 03:55 PM
#10
PowerPoster
Are you still having a problem with this?
What I posted works just fine.
You may be having a little problem with your clear selected method:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cmbPrograms.ClearSelection()
If Not IsPostBack Then
If Request.Cookies("Priveledge").Value = "Administrator" Then
cmbPrograms.Items.Insert(0, New ListItem("PDI Sales", ""))
cmbPrograms.Items.Insert(1, New ListItem("Price Check", ""))
cmbPrograms.Items.Insert(2, New ListItem("RMS Compare", ""))
End If
End If
End Sub
Should be:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
If Request.Cookies("Priveledge").Value = "Administrator" Then
cmbPrograms.ClearSelection()
cmbPrograms.Items.Insert(0, New ListItem("PDI Sales", "1"))
cmbPrograms.Items.Insert(1, New ListItem("Price Check", "2"))
cmbPrograms.Items.Insert(2, New ListItem("RMS Compare", "3"))
End If
End If
End Sub
The pages AutoEventWireUp should be set to false.
Then you should have the AutoPostBack property of the combo set to true.
You shouldn't need to specify the onSelectedIndexChanged event in the html, if you did it from the designer in VS.net.
That works, I have done it. You have something else that is affecting it if you do all that and it still doesn't work.
Try creating a seperate web app project in vs.net, then put a combo box on the form and a label. Set the autopostback to true. Then, put this code in the load event:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
cmbPrograms.Items.Insert(0, New ListItem("PDI Sales", "1"))
cmbPrograms.Items.Insert(1, New ListItem("Price Check", "2"))
cmbPrograms.Items.Insert(2, New ListItem("RMS Compare", "3"))
End If
End Sub
Now, put this in the onSelectedIndexChanged event of the combo box.
Code:
Private Sub cmbPrograms_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPrograms.SelectedIndexChanged
Label1.Text = cmbPrograms.SelectedItem.Text
End Sub
Notice that label1 will change it's text to the selected item.
It does work, you are just doing something wrong.
-
Nov 17th, 2003, 04:17 AM
#11
Compiler Error Message: BC30456: 'cmbPrograms_SelectedIndexChanged' is not a member of 'ASP.Menu_aspx'.
Yeah sorry indyDavid, cmbPrograms_selectedIndexChanged should be replaced by whatever you called your function that you want to call when the selected index changes.
-
Nov 20th, 2003, 10:59 AM
#12
Thread Starter
Fanatic Member
Thanks for all the help everyone. I finally got it to work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|