Solved

Persisting Viewstate in DropDownlist within a Datalist

Posted on 2004-08-08
12
478 Views
Last Modified: 2012-06-27
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
Comment
Question by:electricstory
  • 6
  • 4
  • 2
12 Comments
 
LVL 10

Expert Comment

by:avidya
ID: 11748033
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
 

Author Comment

by:electricstory
ID: 11748762
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
 
LVL 8

Expert Comment

by:alexhogan
ID: 11749227
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:electricstory
ID: 11749339
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
 
LVL 8

Expert Comment

by:alexhogan
ID: 11749647
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
 

Author Comment

by:electricstory
ID: 11753531
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
 
LVL 10

Expert Comment

by:avidya
ID: 11757953
*grin* he is as she...
0
 

Author Comment

by:electricstory
ID: 11758468
I thought twice about using the pronoun, actually...
0
 
LVL 10

Accepted Solution

by:
avidya earned 500 total points
ID: 11758627
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
 

Author Comment

by:electricstory
ID: 11787983
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
 

Author Comment

by:electricstory
ID: 11788020
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
 
LVL 10

Expert Comment

by:avidya
ID: 11788086
glad that its solved, looks nice.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

813 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

19 Experts available now in Live!

Get 1:1 Help Now