Solved

Persisting Viewstate in DropDownlist within a Datalist

Posted on 2004-08-08
12
481 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
[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
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

732 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