Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Persisting Viewstate in DropDownlist within a Datalist

Posted on 2004-08-08
12
Medium Priority
?
486 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 1500 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

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

604 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