473,396 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with code.

I have a session variable populated with a name.
I also have a dropdownlist populated with several names.
I would like to select the name in the dropdown that matches the name in the
session variable.

I am trying :
'DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(Session("Name"))

This does not work.

Any ideas?

Thanks,
Aaron
Nov 21 '05 #1
5 1038
hmmm

does the right hand side return -1?

If so it isn't finding a match.

without knowing the value of everything it would be hard to see the problem
I think.

could you set a break point and check on all the values...

"Aaron" <aa***@jonharvey.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
I have a session variable populated with a name.
I also have a dropdownlist populated with several names.
I would like to select the name in the dropdown that matches the name in the session variable.

I am trying :
'DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(Session("Name"))
This does not work.

Any ideas?

Thanks,
Aaron

Nov 21 '05 #2
DropDownList1.Items.FindByValue(Session("Name").To String).Selected = True

HTH,
Greg
"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:OJ****************@TK2MSFTNGP10.phx.gbl...
hmmm

does the right hand side return -1?

If so it isn't finding a match.

without knowing the value of everything it would be hard to see the problem I think.

could you set a break point and check on all the values...

"Aaron" <aa***@jonharvey.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
I have a session variable populated with a name.
I also have a dropdownlist populated with several names.
I would like to select the name in the dropdown that matches the name in

the
session variable.

I am trying :
'DropDownList1.SelectedIndex =

DropDownList1.Items.IndexOf(Session("Name"))

This does not work.

Any ideas?

Thanks,
Aaron


Nov 21 '05 #3
Guess this works as the other is looking for an object instance I think.

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:Ow****************@TK2MSFTNGP10.phx.gbl...
DropDownList1.Items.FindByValue(Session("Name").To String).Selected = True

HTH,
Greg
"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:OJ****************@TK2MSFTNGP10.phx.gbl...
hmmm

does the right hand side return -1?

If so it isn't finding a match.

without knowing the value of everything it would be hard to see the

problem
I think.

could you set a break point and check on all the values...

"Aaron" <aa***@jonharvey.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
I have a session variable populated with a name.
I also have a dropdownlist populated with several names.
I would like to select the name in the dropdown that matches the name
in the
session variable.

I am trying :
'DropDownList1.SelectedIndex =

DropDownList1.Items.IndexOf(Session("Name"))

This does not work.

Any ideas?

Thanks,
Aaron



Nov 21 '05 #4
Not sure what you are trying to accomplish, but I always found this bit of
code pretty slick. If your dropdown list is in a datagrid, and you are
trying to preset an item from db when put into edit mode, you may find this
slick too. Basically it stores the original value from the db in a custom
attribute named... OriginalValue.

Hope it make sense and is relevant.
Greg
<asp:dropdownlist id=DropDownList1 Runat="server" OriginalValue='<%#
DataBinder.Eval(Container.DataItem, "SomeFieldName") %>'>
<asp:ListItem Value="">--choose--</asp:ListItem>
<asp:ListItem Value="0">Choice 1</asp:ListItem>
<asp:ListItem Value="1">Choice 2</asp:ListItem>
</asp:dropdownlist>
Protected Sub grid_ItemDataBound(ByVal obj As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles grid.ItemDataBound

If e.Item.ItemType = ListItemType.EditItem Then

Dim Myddl As DropDownList =
CType(e.Item.FindControl("DropDownList1"), DropDownList)
Dim OriginalValue As String = Myddl.Attributes("OriginalValue")
Dim MyListItem As ListItem =
Myddl.Items.FindByValue(OriginalValue)
ddlType.SelectedIndex = ddlType.Items.IndexOf(MyListItem)

End If

End Sub
Nov 21 '05 #5
typo is last post...

ddlType.SelectedIndex = ddlType.Items.IndexOf(MyListItem)

should have been

Myddl.SelectedIndex = Myddl.Items.IndexOf(MyListItem)

(forgot I renamed ddlType to Myddl for example)

This also points out the error in your original post...
DropDownList1.SelectedIndex =DropDownList1.Items.IndexOf(Session("Name"))
needs to be changed to

DropDownList1.SelectedIndex
=DropDownList1.Items.IndexOf(DropDownList1.Items.F indByValue(Session("Name")
..ToString)

but my first post should work too

DropDownList1.Items.FindByValue(Session("Name").To String).Selected = True

Confused yet? I am. :^)
Greg

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:eo**************@tk2msftngp13.phx.gbl... Not sure what you are trying to accomplish, but I always found this bit of
code pretty slick. If your dropdown list is in a datagrid, and you are
trying to preset an item from db when put into edit mode, you may find this slick too. Basically it stores the original value from the db in a custom
attribute named... OriginalValue.

Hope it make sense and is relevant.
Greg
<asp:dropdownlist id=DropDownList1 Runat="server" OriginalValue='<%#
DataBinder.Eval(Container.DataItem, "SomeFieldName") %>'>
<asp:ListItem Value="">--choose--</asp:ListItem>
<asp:ListItem Value="0">Choice 1</asp:ListItem>
<asp:ListItem Value="1">Choice 2</asp:ListItem>
</asp:dropdownlist>
Protected Sub grid_ItemDataBound(ByVal obj As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles grid.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then

Dim Myddl As DropDownList =
CType(e.Item.FindControl("DropDownList1"), DropDownList)
Dim OriginalValue As String = Myddl.Attributes("OriginalValue") Dim MyListItem As ListItem =
Myddl.Items.FindByValue(OriginalValue)
ddlType.SelectedIndex = ddlType.Items.IndexOf(MyListItem)

End If

End Sub

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
4
by: Quas.co.ua | last post by:
Hello all. I need your help. I need C compler to make demo of some technologie. This C compiler I need to write program which after run will be located in one segment of memory and it...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.