Solved

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

Posted on 2011-03-16
7
893 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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