Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 10th, 2004, 05:31 AM
Registered User
 
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to set selectedindex

how to set selectedindex in dropdownlist with optional value when i use from datalist in run time (in html code)

 
Old August 10th, 2004, 09:11 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

hi azita,
I didnt get what you mean exactly....
but I think you want to change selectedindex of a dropdownlist according to the
DataList item selected by user..am I right?

--------------------------------------------
Mehdi.:)
 
Old August 10th, 2004, 09:48 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

or you have a DropDownList in your DataList(ItemTemplate) and want to change it's selectedindex ?

--------------------------------------------
Mehdi.:)
 
Old August 10th, 2004, 11:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

In HTML, you have to use javascript to do so by looping through each option and find the right one.

var list = document.getElementById('DropDownList1');
for (var intI = 0; intI < list.options.count -1; intI++) {
if (list.options[intI].value == 'some value') {
    list.options[intI].selected = true;
}
}

Something like that (may be a little off). At the server level, you can do:

DropDownList1.SelectedIndex = <value>

Brian
 
Old August 10th, 2004, 11:40 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

its better not to use JavaScript when you deal with datalist object(work in server-side not in client-side using HTML & javascript).

--------------------------------------------
Mehdi.:)
 
Old August 11th, 2004, 07:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Well, although the server side code is easier, it is still possible to use JavaScript. Because the datalist is similar to the datagrid, where when you select an item and it posts back, it is possible to use javascript, by changing the above code to:

var list = document.getElementById('DropDownList1');
for (var intI = 0; intI < list.options.count -1; intI++) {
  if (list.options[intI].value == '<%=DataList1.<whatever control or attribute returns the value>') {
    list.options[intI].selected = true;
  }
}

But server-side would be the easiest, especially because you are posting back anyway due to the select.

Brian
 
Old August 14th, 2004, 02:54 AM
Registered User
 
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mehdi62b
 or you have a DropDownList in your DataList(ItemTemplate) and want to change it's selectedindex ?

--------------------------------------------
Mehdi.:)
i have a dropdown list in my datalist(edittemplate) and iwant to change dropdown list value with my value means(i want pass selectedindex value in edit template )
please help me
thanks.

 
Old August 14th, 2004, 03:07 AM
Registered User
 
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mehdi62b
 or you have a DropDownList in your DataList(ItemTemplate) and want to change it's selectedindex ?

--------------------------------------------
Mehdi.:)
my dear mehdi
hi
i have a dropdown list in my datalist(edittemplate) and iwant to change dropdown list value with my value means(i want pass selectedindex value in edit template from itemtemplate )
please help me
thanks

 
Old August 14th, 2004, 10:44 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think I got it what you mean
somewhere like ItemCommand event write below code
Code:
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
DataList1.EditItemIndex=e.Item.ItemIndex;//make your cell like EditTemplate 
((DropDownList)e.Item.FindControl("DropDownList1")).SelectedIndex=newvalue;
//find your dropdownlist in your edititemtemplate and assign new value to it's SelectedIndex 
}
--------------------------------------------
Mehdi.:)
 
Old August 16th, 2004, 02:17 AM
Registered User
 
Join Date: Aug 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mehdi62b
 I think I got it what you mean
somewhere like ItemCommand event write below code
Code:
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
DataList1.EditItemIndex=e.Item.ItemIndex;//make your cell like EditTemplate 
((DropDownList)e.Item.FindControl("DropDownList1")).SelectedIndex=newvalue;
//find your dropdownlist in your edititemtemplate and assign new value to it's SelectedIndex 
}
--------------------------------------------
Mehdi.:)
i performed this way but dropdownlist return null value and i dont khow what do i do






Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedIndex in datalist dhoward VB.NET 2002/2003 Basics 0 August 28th, 2008 10:15 AM
GridView.SelectedIndex Prop SteveP55419 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 October 8th, 2006 09:08 PM
comboBox selectedindex problem ColdFusion C# 4 May 12th, 2006 05:37 AM
how can get selectedindex in dropdown list azitanosrati General .NET 0 September 8th, 2004 05:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.