[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

vb.net why I can not set dropdownlist initial value after post back?

Posted on 2011-03-16
7
Medium Priority
?
898 Views
Last Modified: 2012-08-14
Please try my code. UK is selected first time the page is loaded. If you click Currency from left list, then click Country again, why USA is selected not UK?
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="testUK.aspx.vb" Inherits="testUK" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table >
    <tr>
    <td valign="top" >
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack ="true">
            <asp:ListItem Selected ="true">Country</asp:ListItem>
            <asp:ListItem>Currency</asp:ListItem>
        </asp:ListBox>
    </td>
    <td>
        <asp:Panel ID="Panel1" runat="server" Height="130px" Width="195px" EnableViewState ="false">
        </asp:Panel>
    </td>
    </tr>
    </table>
        <asp:Button ID="Button1" runat="server" Text="Save" />
    </div>
    </form>
</body>
</html>

Open in new window

Partial Class testUK
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CreateDropDownList()
    End Sub
    Private Sub CreateDropDownList()
        Dim newDropDownList As New DropDownList
        If ListBox1.Items(0).Selected Then
            newDropDownList.Items.Add("USA")
            newDropDownList.Items.Add("UK")
            newDropDownList.SelectedValue = "UK"
            Panel1.Controls.Add(newDropDownList)
        ElseIf ListBox1.Items(1).Selected Then
            newDropDownList.EnableViewState = False
            newDropDownList.Items.Add("USA$")
            newDropDownList.Items.Add("POUND")
            Panel1.Controls.Add(newDropDownList)
        End If
    End Sub

    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ShowDropDownValue()
    End Sub

    Private Sub ShowDropDownValue()
        If Panel1.Controls.Count = 2 Then
            Response.Write("Selection is " & CType(Panel1.Controls(1), DropDownList).SelectedValue)
        End If
    End Sub
End Class

Open in new window

0
Comment
Question by:Richard_XP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 7

Expert Comment

by:Element1910
ID: 35151239
Try this code...works with my test environment...

Partial Class testUK
    Inherits System.Web.UI.Page

    Dim newDropDownList As New DropDownList

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CreateDropDownList()
    End Sub
    Private Sub CreateDropDownList()
        'Dim newDropDownList As New DropDownList
        If ListBox1.Items(0).Selected Then
            newDropDownList.Items.Add("USA")
            newDropDownList.Items.Add("UK")
            newDropDownList.SelectedValue = "UK"
            Panel1.Controls.Add(newDropDownList)
        ElseIf ListBox1.Items(1).Selected Then
            newDropDownList.EnableViewState = False
            newDropDownList.Items.Add("USA$")
            newDropDownList.Items.Add("POUND")
            Panel1.Controls.Add(newDropDownList)
        End If
    End Sub

    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ShowDropDownValue()
    End Sub

    Private Sub ShowDropDownValue()
        If Panel1.Controls.Count = 2 Then
            Response.Write("Selection is " & CType(Panel1.Controls(1), DropDownList).SelectedValue)
        End If
    End Sub

    Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        newDropDownList.SelectedValue = "UK"
    End Sub
End Class

Open in new window


Basically, all I did was move the Dim newDropDownList declaration to a global and then created an event to handle changing selection to make sure it defaults to UK. You can play around with this if you'd like and make it more dynamic so it will fill in with the default (UK) or previous selection; if there was one...

Hope this helps!
0
 
LVL 4

Author Comment

by:Richard_XP
ID: 35151330
ok, I will play with it. do you know why it happens like this? thanks for your time.
0
 
LVL 4

Author Comment

by:Richard_XP
ID: 35151370
I found a better way to do that. I think MS try to remember dropdownlist value and try to apply it after posting back.

so I add newDropDownList.ID = Guid.NewGuid.ToString to name the id different each time, then it works without any change. interesting.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 7

Expert Comment

by:foobarr
ID: 35151394
0
 
LVL 7

Accepted Solution

by:
Element1910 earned 1500 total points
ID: 35151423
Correct...it's fickle like that sometimes...but it's doing exactly what you are telling it to...sometimes you'll get different results from expected ones because you're not not telling it not to do something :)

It's always good to understand page events for this exact reason because you may need to control data on every event that's fired.

Giving it an ID is a good idea as well, but it's a hack job. If you don't need that specific instance of the control down the line, then that should work as a solution for you.
0
 
LVL 4

Author Comment

by:Richard_XP
ID: 35151491
actually, you only need to assign newdropdownlist a name, any name. strange.
0
 
LVL 4

Author Closing Comment

by:Richard_XP
ID: 35152610
code is fine, only missing ID name
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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