Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cant retrieve dropdownlist value....

Status
Not open for further replies.

magmo

Programmer
May 26, 2004
291
SE
Hi


I have added a drop downlist on the aspx page and filled it with items. It looks like this....

And in the codebehind I have this code...

Code:
<asp:dropdownlist id="ddl" runat="server" Width="144px" AutoPostBack="true" SelectedIndexChanged="Selection_Change">
<asp:ListItem Value="0">choose:ListItem>
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
<asp:ListItem Value="6">Item 6</asp:ListItem>
<asp:ListItem Value="7">Item 7</asp:ListItem>
</asp:dropdownlist>

Code:
    Sub Selection_Change(ByVal sender As Object, ByVal e As EventArgs) Handles ddl.SelectedIndexChanged
        Response.Write(ddl.SelectedItem.Value & " - Text = " & ddl.SelectedItem.Text)
    End Sub


But it never change what it write out, it always write the same..... What am I doing wrong here?
 
You are most likely populating the DDL on the page load. You should check for a postback on the page load and if it is a postback, don't populate the DDL.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi



Here's what I use...

Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load       
 If Page.IsPostBack Then
        Else
            FillDDL()
        End If

End Sub

    Sub FillDDL()
        ddl.Items.Add("Choose")
        ddl.Items.Add("Item 1")
        ddl.Items.Add("Item 2")
        ddl.Items.Add("Item 3")
        ddl.Items.Add("Item 4")
        ddl.Items.Add("Item 5")
        ddl.Items.Add("Item 6")
        ddl.Items.Add("Item 7")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If ddl.SelectedItem.Text <> "" Then
            Response.Write(ddl.SelectedItem.Value & " - Text = " & ddl.SelectedItem.Text)
            'ddl.SelectedIndex = ddl.SelectedItem.Value
        End If
        Response.Write(Request.Form("ddl"))
        'HideTextItems()
    End Sub


Whatever I choose, Item 1 is always written out....

Am I still missing something here, this is very frustrating to spend this amount of time on a task that should be simple...


Regards


 
I'm not really sure what you are doing wrong (although it may be the way you have set up the if..Else statement in the page load). It should be as simple as:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            FillDDL()
        End If
    End Sub

    Sub FillDDL()
        ddl.Items.Add("Choose")
        ddl.Items.Add("Item 1")
        ddl.Items.Add("Item 2")
        ddl.Items.Add("Item 3")
        ddl.Items.Add("Item 4")
        ddl.Items.Add("Item 5")
        ddl.Items.Add("Item 6")
        ddl.Items.Add("Item 7")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If ddl.SelectedItem.Text <> "" Then
            Response.Write("Value: " & ddl.SelectedItem.Value & " Text: " & ddl.SelectedItem.Text)
        End If
    End Sub
which works perfectly well.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi


Changed the page load and now it always displays the "Item 6" whatever I choose. This is just to weird...
 
And you have copied and pasted the code exactly as I wrote above?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
OK, can you copy the full html and code behind files and paste them both here.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi


I found my problem... Finaly...

The page I was using where a page I had copied inside my project, and on that page I used the <%@ OutputCache Duration="300" VaryByParam="none" %>. And that caused all my problems.


Thanks for your help and time!


Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top