Solved

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

Posted on 2011-03-16
7
887 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:foobarr
Comment Utility
0
 
LVL 7

Accepted Solution

by:
Element1910 earned 500 total points
Comment Utility
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
Comment Utility
actually, you only need to assign newdropdownlist a name, any name. strange.
0
 
LVL 4

Author Closing Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

12 Experts available now in Live!

Get 1:1 Help Now