Reahreic
Reahreic

Reputation: 629

asp:ListBox SelectedValue not setting (dynamically or when clicked)

I have an asp:ListBox that is populated dynamically from js based on the selected value of another asp:List box. The problem is that the second list box always returns a SelectedValue of "" regardless of whether i set lstBox.selectedIndex = 0 or actually select an item in the list.

.js to add to list then set default selected item

var Option = document.createElement("option");
lstId = document.getElementById(lstId);
Option.text = lstItem;
lstId.add(Option);    
lstId.selectedIndex = 0;

.vb to get selected value

Dim selSchedule As String = lstCRMSched.SelectedValue

Now as this list is populated by javascript i had to set my @page EnableEventValidation = "false" otherwise the postback that came later would fail.

Side note: I'm noticing that asp.net doesn't like it when you use hidden divs as overlays that are unhidden based on menu selections as everything it does requires a postback, which wipes out the state of the other divs. Should i just have 10 .aspx files one for each div and just switch locations from the codebehind using sessions to transfer things like selected values and data that is to be shown in another div?

Upvotes: 0

Views: 1180

Answers (2)

Pilgerstorfer Franz
Pilgerstorfer Franz

Reputation: 8359

If you add items to the dropdownlist on the client these items are not persisted on the server!

But you may try saving dynamically added items (text-value-pairs) within some hidden input fields and parse them out on the server. See this link for a working example. For your example you will also have to save your selectedIndex within another hidden field to be able to access it on the server.

EDIT

demo.aspx

<script type="text/javascript">
function saveValue() {
    var hiddenField1 = document.getElementById("hiddenField1");
    hiddenField1.value = "hello world";
}
</script>
<form id="Form1" method="post" runat="server">
    <input type="hidden" id="hiddenField1" name="hiddenField1" value="" />
    <asp:Button ID="btnPostBack" runat="server" Text="PostBack" 
        OnClientClick="saveValue()" onclick="btnPostBack_Click" />
</form>

demo.aspx.cs

protected void btnPostBack_Click(object sender, EventArgs e)
{
    Debug.WriteLine("Value of hiddenField1: " + Request["hiddenField1"]);
    Debugger.Break();
}

This one worked for me. I got "hello world" on the server.

EDIT 2

Icarus pointed out that you can always access any submitted element to the server by referring to the Request object and of course he is absolutelly right! According to your question I thought you'd like to have access to all dynamically created items - and that is - with solution shown below - not possible.

aspxPage

<script type="text/javascript">
    function saveToList() {
        var ListBox1 = document.getElementById("ListBox1");
        var ListBox2 = document.getElementById("ListBox2");
        var Option = document.createElement("option");
        Option.text = ListBox1.options[ListBox1.selectedIndex].text;
        ListBox2.add(Option);
        ListBox2.selectedIndex = 0;
    }
</script>
<form id="Form1" method="post" runat="server">
<asp:ListBox ID="ListBox1" runat="server">
    <asp:ListItem Text="entry1" Value="1" />
    <asp:ListItem Text="entry2" Value="2" />
    <asp:ListItem Text="entry3" Value="3" />
    <asp:ListItem Text="entry4" Value="4" />
</asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" Width="100"></asp:ListBox>
<input id="Button1" type="button" value="Save to List" onclick="saveToList()" />

<asp:Button ID="btnPostBack" runat="server" Text="PostBack" OnClick="btnPostBack_Click" />

codeBehind

protected void btnPostBack_Click(object sender, EventArgs e)
{
    Debug.WriteLine("SelectedValue of ListBox2: " + Request["ListBox2"]);
    // no access to all other clientSide created items in ListBox2
    Debugger.Break();
}

Upvotes: 1

Icarus
Icarus

Reputation: 63956

You can access the SelectedValue of the drop down list through the Request object since every element in the form that has a name is submitted in the request.

You simply need to do this:

Dim selSchedule As String = Request[lstCRMSched.UniqueID]

Now, this will work just because you disabled EventValidation on the page. The error you were getting is completely normal. ASP.NET is just making sure that no one sends data that wasn't rendered by the server initially to prevent attacks. If you were to keep the EventValidation enabled on the page, you'd need to register the list for Validation via ClientScriptManager.RegisterForEventValidation

Upvotes: 2

Related Questions