Solved

Persisting Viewstate in DropDownlist within a Datalist

Posted on 2004-08-08
12
476 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:electricstory
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:avidya
Comment Utility
*grin* he is as she...
0
 

Author Comment

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

Accepted Solution

by:
avidya earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
glad that its solved, looks nice.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

744 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

14 Experts available now in Live!

Get 1:1 Help Now