ObjectDataSource and DropDownList

  • Thread starter Thread starter Bill Gower
  • Start date Start date
B

Bill Gower

I have a asp.net page that has a DDL tied to a ObjectDataSource. I want the
DDL to display a blank in the list when the page is first displayed and let
the user select the entry they want. I have tried

CategoryList.SelectedIndex = -1

and

CategoryList.SelectedValue = ""

Neither clears out the list. How do I display a blank?

Bill
 
* Bill Gower wrote, On 31-7-2007 0:06:
I have a asp.net page that has a DDL tied to a ObjectDataSource. I want the
DDL to display a blank in the list when the page is first displayed and let
the user select the entry they want. I have tried

CategoryList.SelectedIndex = -1

and

CategoryList.SelectedValue = ""

Neither clears out the list. How do I display a blank?

In the designer, add a blank item to the items list of the dropdown. Set
the text to an empty string and the value to -1. Then look for a
property that is called something like "AppendWhenDataBinding"... (The
exact phrasing eludes me at this moment...) and set it true.

Now that you have your blank element both your old statements should be
able to set it to the empty line by default.

Jesse
 
Do you mean in the Page Load event?

Jesse Houwing said:
* Bill Gower wrote, On 31-7-2007 0:06:

In the designer, add a blank item to the items list of the dropdown. Set
the text to an empty string and the value to -1. Then look for a property
that is called something like "AppendWhenDataBinding"... (The exact
phrasing eludes me at this moment...) and set it true.

Now that you have your blank element both your old statements should be
able to set it to the empty line by default.

Jesse
 
* Bill Gower wrote, On 31-7-2007 1:17:
Do you mean in the Page Load event?

No. Open the page with the dropdown in the designer. Look in the
properties. There's an items property. Open the items property with the
little [...] buton and click the add button. Give the thing a valeu of
-1 and clear the text that's automatically put in there. Close the
window and look for the AppendDataboundItems and set it to true.

Now if you load the page, it has an empty item on top. There's no need
to set it to the empty value, because the dropdown will always default
to it's top-most item unless specified otherwise.

Jesse
 
If you can't use the designer (either because you are using nested master
pages or because you just can't stomach the output), you can do it in the
aspx as follows:

<asp:SqlDataSource ID="MyDataSource" ... />
<asp:DropDownList DataSourceID="MyDataSource" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="" />
</asp:DropDownList>


Jesse Houwing said:
* Bill Gower wrote, On 31-7-2007 1:17:
Do you mean in the Page Load event?

No. Open the page with the dropdown in the designer. Look in the
properties. There's an items property. Open the items property with the
little [...] buton and click the add button. Give the thing a valeu of -1
and clear the text that's automatically put in there. Close the window and
look for the AppendDataboundItems and set it to true.

Now if you load the page, it has an empty item on top. There's no need to
set it to the empty value, because the dropdown will always default to
it's top-most item unless specified otherwise.

Jesse

 
Thanks that really helped. Just one problem to solve, even though I have
deleted the Text value in the Items collection field, it still displays -1
in the drop down and I did check, I am displaying the proper field in the
list.

Bill

Jesse Houwing said:
* Bill Gower wrote, On 31-7-2007 1:17:
Do you mean in the Page Load event?

No. Open the page with the dropdown in the designer. Look in the
properties. There's an items property. Open the items property with the
little [...] buton and click the add button. Give the thing a valeu of -1
and clear the text that's automatically put in there. Close the window and
look for the AppendDataboundItems and set it to true.

Now if you load the page, it has an empty item on top. There's no need to
set it to the empty value, because the dropdown will always default to
it's top-most item unless specified otherwise.

Jesse

 
Try to manually add Text="" in the aspx. Clearing the value in the
properties window probably removes the attribute from the control, which
would result in the value being shown by default (-1 in this case). Another
solution might be to use an empty value as well--unless your code
specifically requires -1 for unselected ddl values.


Bill Gower said:
Thanks that really helped. Just one problem to solve, even though I have
deleted the Text value in the Items collection field, it still displays -1
in the drop down and I did check, I am displaying the proper field in the
list.

Bill

Jesse Houwing said:
* Bill Gower wrote, On 31-7-2007 1:17:
Do you mean in the Page Load event?

No. Open the page with the dropdown in the designer. Look in the
properties. There's an items property. Open the items property with the
little [...] buton and click the add button. Give the thing a valeu of -1
and clear the text that's automatically put in there. Close the window
and look for the AppendDataboundItems and set it to true.

Now if you load the page, it has an empty item on top. There's no need to
set it to the empty value, because the dropdown will always default to
it's top-most item unless specified otherwise.

Jesse

* Bill Gower wrote, On 31-7-2007 0:06:
I have a asp.net page that has a DDL tied to a ObjectDataSource. I
want the DDL to display a blank in the list when the page is first
displayed and let the user select the entry they want. I have tried

CategoryList.SelectedIndex = -1

and

CategoryList.SelectedValue = ""

Neither clears out the list. How do I display a blank?
In the designer, add a blank item to the items list of the dropdown.
Set the text to an empty string and the value to -1. Then look for a
property that is called something like "AppendWhenDataBinding"... (The
exact phrasing eludes me at this moment...) and set it true.

Now that you have your blank element both your old statements should be
able to set it to the empty line by default.

Jesse
 
* Bill Gower wrote, On 31-7-2007 4:51:
Thanks that really helped. Just one problem to solve, even though I have
deleted the Text value in the Items collection field, it still displays -1
in the drop down and I did check, I am displaying the proper field in the
list.

I the past I've used &nbsp; as the text field. Not completely empty, but
it does the trick from a users perspective.

Jesse

Jesse Houwing said:
* Bill Gower wrote, On 31-7-2007 1:17:
Do you mean in the Page Load event?
No. Open the page with the dropdown in the designer. Look in the
properties. There's an items property. Open the items property with the
little [...] buton and click the add button. Give the thing a valeu of -1
and clear the text that's automatically put in there. Close the window and
look for the AppendDataboundItems and set it to true.

Now if you load the page, it has an empty item on top. There's no need to
set it to the empty value, because the dropdown will always default to
it's top-most item unless specified otherwise.

Jesse

* Bill Gower wrote, On 31-7-2007 0:06:
I have a asp.net page that has a DDL tied to a ObjectDataSource. I
want the DDL to display a blank in the list when the page is first
displayed and let the user select the entry they want. I have tried

CategoryList.SelectedIndex = -1

and

CategoryList.SelectedValue = ""

Neither clears out the list. How do I display a blank?
In the designer, add a blank item to the items list of the dropdown. Set
the text to an empty string and the value to -1. Then look for a
property that is called something like "AppendWhenDataBinding"... (The
exact phrasing eludes me at this moment...) and set it true.

Now that you have your blank element both your old statements should be
able to set it to the empty line by default.

Jesse
 
for example category is an ddl
<asp:DropDownList ID="DdlCategoryName"
'Object data source DataSourceID="OdsCategoryNames"
DataTextField="categoryname"
DataValueField="category"

runat="server">
<asp:ListItem />
</asp:DropDownList>
-------------
<asp:ListItem/> creates one blank, if specify any item in list just specify that with in listitem tag



Bill Gower wrote:

ObjectDataSource and DropDownList
30-Jul-07

I have a asp.net page that has a DDL tied to a ObjectDataSource. I want the
DDL to display a blank in the list when the page is first displayed and let
the user select the entry they want. I have trie

CategoryList.SelectedIndex = -

an

CategoryList.SelectedValue = "

Neither clears out the list. How do I display a blank

Bill

Previous Posts In This Thread:

ObjectDataSource and DropDownList
I have a asp.net page that has a DDL tied to a ObjectDataSource. I want the
DDL to display a blank in the list when the page is first displayed and let
the user select the entry they want. I have trie

CategoryList.SelectedIndex = -

an

CategoryList.SelectedValue = "

Neither clears out the list. How do I display a blank

Bill

Re: ObjectDataSource and DropDownList
* Bill Gower wrote, On 31-7-2007 0:06

In the designer, add a blank item to the items list of the dropdown. Set
the text to an empty string and the value to -1. Then look for a
property that is called something like "AppendWhenDataBinding"... (The
exact phrasing eludes me at this moment...) and set it true

Now that you have your blank element both your old statements should be
able to set it to the empty line by default

Jesse

Do you mean in the Page Load event?
Do you mean in the Page Load event?

Re: ObjectDataSource and DropDownList
* Bill Gower wrote, On 31-7-2007 1:17

No. Open the page with the dropdown in the designer. Look in the
properties. There's an items property. Open the items property with the
little [...] buton and click the add button. Give the thing a valeu of
-1 and clear the text that's automatically put in there. Close the
window and look for the AppendDataboundItems and set it to true

Now if you load the page, it has an empty item on top. There's no need
to set it to the empty value, because the dropdown will always default
to it's top-most item unless specified otherwise

Jesse

If you can't use the designer (either because you are using nested master
If you can't use the designer (either because you are using nested master
pages or because you just can't stomach the output), you can do it in the
aspx as follows

<asp:SqlDataSource ID="MyDataSource" ... /
<asp:DropDownList DataSourceID="MyDataSource" AppendDataBoundItems="true"
<asp:ListItem Value="" Text="" /
</asp:DropDownList


Thanks that really helped.
Thanks that really helped. Just one problem to solve, even though I have
deleted the Text value in the Items collection field, it still displays -1
in the drop down and I did check, I am displaying the proper field in the
list

Bil


Try to manually add Text="" in the aspx.
Try to manually add Text="" in the aspx. Clearing the value in the
properties window probably removes the attribute from the control, which
would result in the value being shown by default (-1 in this case). Another
solution might be to use an empty value as well--unless your code
specifically requires -1 for unselected ddl values.



Re: ObjectDataSource and DropDownList
* Bill Gower wrote, On 31-7-2007 4:51:

I the past I have used &nbsp; as the text field. Not completely empty, but
it does the trick from a users perspective.

Jesse


Submitted via EggHeadCafe - Software Developer Portal of Choice
Make Your Apps Talk to Each Other: Asynchronous Named Pipes Library
http://www.eggheadcafe.com/tutorial...0-5acdaeb00162/make-your-apps-talk-to-ea.aspx
 
Back
Top