Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

Persisting Viewstate in DropDownlist within a Datalist

I've got a dropdownlist in a datalist item template. I'm able to populate it without difficulty by doing the following:

1. in the ItemDataBound Event of the DataList, I find the dropdown:

Dim ddl As DropDownList
ddl = CType(e.Item.FindControl("myDropDownlist"), DropDownList)

2. Then I add the childrows of each table row to which the datalist is bound to the dropdownlist

Dim drv As DataRowView
drv = e.Item.DataItem

Dim dr As DataRow

For Each dr In drv.Row.GetChildRows("myDatasetRelation")
Dim li As New ListItem
li.Value = drv(0)
li.Text = dr.Item("myColumn")
ddl.Items.Add(li)

So far, so good, but when I use the dropdownlist, the selectedindex is always the first one, no matter what I select. I've tried accessing the selectedIndex changed event to set some session variables there, but it's never raised, even when I try to put the dropdown in a .ascx control and use that instead of a straight dropdown. Please help!




0
electricstory
Asked:
electricstory
  • 6
  • 4
  • 2
1 Solution
 
avidyaCommented:
here's an example, the index is selected in the body statement
you have to name it *.asp to let it work

================
<%
v_selected=Request("country")
%>
<HTML>
<HEAD>
 <TITLE></TITLE>
<script language="JavaScript">
 function goSelect(obj)
 {
  var SelectedIndex = document.Country.SelCountry.selectedIndex;          
  window.open(obj.options[obj.selectedIndex].value + "country=" + SelectedIndex, "_top" );    
 }
</script>
</HEAD>

<BODY onload="<%If len(trim(v_selected))>0 Then%>document.Country.SelCountry[<%=v_selected%>].selected=true;<%End if%>">
<form name="Country">
<select onchange="goSelect(this)" size="1" name="SelCountry">
     <option>SELECT COUNTRY</option>
     <option value="default.htm?">USA</option>
     <option value="home_Asia.asp?">Afghanistan</option>
     <option value="home_EUframe.asp?lang=2&amp;S=1&">Albania</option>
</select>
</form>
</BODY>
</HTML>
0
 
electricstoryAuthor Commented:
Thanks for the quick response.

I'm using ASP.net. I already solved the problem with a javascript solution, but I'm hoping to use a straight ASP.net/VB.net (or C#) solution.
0
 
alexhoganCommented:
That's because you have;

li.Value = drv() set to 0.  That's the first item on the list.

Define a count var and change the expression to somthing like;

li.Value = drv(i)

This will sequence through the values.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
electricstoryAuthor Commented:
That's not the problem. The dropdown populates fine. I'm just having trouble passing the viewstate. I lose the selectedindex on the postback, even though I repopulate the controls in init before viewstate is supposedly applied. There's got to be someway to bubble the selectedindex change up to the datalist.
0
 
alexhoganCommented:
So you're wanting the selected index to maintain it's selected state when you postback?

Then you're wanting to use __ViewState.  ViewState is a hidden field on your form.  Save the selected value to viewstate and then on your postback send that value to your dropdown as the selected item.

Look at this;
http://www.aspnetresources.com/articles/ViewState.aspx

It explains viewstate.
0
 
electricstoryAuthor Commented:
On each trip to the server, I need to repopulate my datalist and nested controls. I do this during the page init, and then the event handlers for the postback fire. For some reason, the viewstate for the dropdown is not preserved. Try adding a dropdown to a datalist and populate it via a database. How do you access the dropdown contents? Setting EnableViewState to "True" does not work. Right now I'm using a javascript solution similar to avidya's. If no one has a better answer after a couple days, I'll give him the points. Thanks for trying, though.
0
 
avidyaCommented:
*grin* he is as she...
0
 
electricstoryAuthor Commented:
I thought twice about using the pronoun, actually...
0
 
avidyaCommented:
doesn't matter, have humor, hihi

have done some reading and someone had the same problem 'cause his dropdown values woren't unique

this link will explain how the vars get posted, etc.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwebformseventmodel.asp

This is an article about selected values while having a datagrid:
http://www.developer.com/net/vb/article.php/3320941

and here's another one
http://www.dotnetspider.com/Technology/QA/ViewQuestion.aspx?QuestionId=104
0
 
electricstoryAuthor Commented:
I decided to add an html input button to the controls collection of the datalist item in the ondatabound event. Then I added a javascript event handler to it that passes the dropdown in that cell:

        Dim btn As New HtmlInputButton
        btn.Attributes.Add("onClick", "goSelect(document.getElementById('dlCustomerBooks__ctl" & revItemid1 & "_myDropDownList'))")
        btn.Attributes.Add("value", "Download")
        btn.Attributes.Add("text", "Download")
        btn.Attributes.Add("id", "dnLoadBtn" & revItemid1)

In the javascript function GoSelect(obj) that I wrote on the html page, I extracted the dropdownlist values like so:

obj.options[obj.selectedIndex].value
obj.options[obj.selectedIndex].text

I then put them in the querystring, and used that to persist the values. In the pageLoad event handler, I checked for the querystring values and executed code accordingly.
0
 
electricstoryAuthor Commented:
Whoops, let me clean up that code that goes in the OnItemDataBound handler:

        dim intIndex as integer
        Dim btn As New HtmlInputButton
        btn.Attributes.Add("onClick", "goSelect(document.getElementById('dlDataList__ctl" & intIndex & "_myDropDownList'))")
        btn.Attributes.Add("value", "DropDownValue")
        btn.Attributes.Add("text", "DropDownText")
        btn.Attributes.Add("id", "btn" & intIndex)
        intIndex+=1
0
 
avidyaCommented:
glad that its solved, looks nice.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now