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 16th, 2004, 07:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

I think you have to reference the specific cell, as such:

((DropDownList)e.Item.Cells[<cellindex>].FindControl("DropDownList1")).SelectedIndex=newval ue;

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

Brian,I dont think it makes any sense
dear Azita, if you tell us more about your scenario we could help you much more
(I think your problem is due to binding your DropDownList and DataList)
can u tell me when you bind your DropDownList(it should be after binding DataList)?
also before Binding your DropDownList you should determine or change
your SelectedIndex(through an integer static variable
could be changed everywhere u need like ItemCommand event )
Hope this helps.

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

Hello,

You are right, that is for a DataGrid. Sorry for any confusion.

Brian
 
Old September 15th, 2004, 03:56 PM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am new to .NET and I am still having trouble with this the topic in this post. I am trying to set the value of the dropdownlist to match the database value when I move to edit mode in the datagrid. Some of this code may be unecessary but I'm not sure what would be. I have been trying everything to get this too work with a lot of different approaches being recommended by many different forums. Sorry if it is ugly. I am getting a message that says, "Object reference not set to an instance of an object."

Here is the code I have in the DataGrid1_EditCommand method.
this.sqlConnection1.Open();
this.sqlDataAdapter1.Fill(recordValuesDataSet11);
this.sqlConnection1.Close();
DataGrid1.EditItemIndex = e.Item.ItemIndex;
this.DataGrid1.DataBind();
((DropDownList)e.Item.Cells[6].FindControl ("statusddl")).SelectedIndex = e.Item.Cells[6].Text;

Thanks in advance for any help you can give.
 
Old September 16th, 2004, 02:17 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Which line is that error occuring on?

I see the call to bind the datagrid, but I don't see anything that is setting the grid's datasource. I assume that you have some code somewhere that is setting up the grid's datasource that gets executed on page load. You need to call this code again when you re-bind the grid. After the page postback to change a row into edit mode, the only data that still exists is the data in the grid's viewstate and this is not enough to rebind the grid.
 
Old September 16th, 2004, 06:21 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello,
if you dont have any problem with your Connection object your problem is due to
binding your grid,for preventing this error you should bind your DropDownList and DataGrid
like below order,
1-first bind your datagrid(before it,set its datasource)
2-then bind again your DropDownList(before it,set its datasource)
for example somewhere like ItemDataBound event could be a good place for binding DropDownList
meanwhile,its better to use this line in your code,
Code:
if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.SelectedItem ||
                e.Item.ItemType == ListItemType.EditItem)
{   
((DropDownList)e.Item.FindControl ("statusddl")).SelectedIndex = anIntegerVariable;
}
you should put your find method in if-statement for avoiding that error.
HtH.

--------------------------------------------
Mehdi.:)
 
Old December 1st, 2006, 02:25 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You guys are adding an extra step.

Use the .length property and you dont need to subtract one, because it knows, that an array starts at zero.

        for(var i=0;i<avail_options.length;i++){
            if(avail_options.options[i].value == current_season){
                avail_options.options[i].selected = true;
            }
        }






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.