?
Solved

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

Posted on 2011-03-16
7
Medium Priority
?
897 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 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