Solved

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

Posted on 2011-03-16
7
890 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Expert Comment

by:foobarr
ID: 35151394
0
 
LVL 7

Accepted Solution

by:
Element1910 earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now