Code to select item in dropdown populated using TableAdapter

N

Nahom Tijnam

Hi,

In ASP.NET 2.0, where do I write code to select a particular item in a
dropdown control that is populated using SqlDataSource (Table Adapters)?

I am developing a website in ASP.NET 2.0 for a library using Visual Studio
2005 and SQL Server 2000. I have already built a middle tier with objects to
read/write library categories and books to the database. The middleware is
working fine.

I have a webform "newBook.aspx" for creating a new book under a particular
section. The page takes a querystring parameter "sectionId" under which the
book is to be added to.

In the webform, I have a dropdown for listing all sections. This dropdown
was populated using the smarttag option "choose data source", "new
datasource", "object", "the middle tier class for sections" and "method to
get list of sections". This works fine and the webpage displays correctly
with the dropdown listbox listing all sections.

However, I am unable to select the item in the dropdown based on the
sectionId querystring value. In which event do I write the code to make the
dropdown selection?

While Debugging, I find that The DataSet.Designer.Cs file events (in
middleware assembly) to populate the dropdown listbox are executed after the
Page_LoadComplete event of the webpage and hence I am confused as to where I
should put the code to automatically select the ListItem based on the
querystring sectionId.

Any help would be greatly appreciated. Also, is there anyway I can see how
and where the middleware methods get called? Where are these delegates
created?

Thanks,

Nahom Tijnam,
Dubai, UAE
 
S

Steven Cheng[MSFT]

Hi Nahom,

Welcome to ASPNET newsgroup.
As for the use code to select Item in DropDownList which is populated with
certain DataSource control or TableAdapter, I have some question about your
detailed code logic (of the book creation page...)

1. Is the dropdownlist on that page populated with a certain list retrieve
from database and is not changable?

2. Since you mentioned that you'll select the certain section item in the
dropdownlist with the querystring item, then on the page, will you allow
other user to change it or just make it readonly and not selectable?

If you just want to use querystring to set an initial value and can let the
user change it later.... I think you can just retrieve the value from
querystring in Page_load event, then, set the DropDownlist.SelectedValue
property to the querystring's value (it should be the value you'll bind
through the data source....)...

Also, you can manually call DropDownList.DataBind() to redo the databinding
after you set the SelectedValue property.....

Please feel free to post here if you have anything unclear or anything else
I didn't quite get...

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| Thread-Topic: Code to select item in dropdown populated using TableAdapter
| thread-index: AcYNHFh6OvwdSHpkQPSuMlrImVxuNw==
| X-WBNR-Posting-Host: 195.229.241.180
| From: =?Utf-8?B?TmFob20gVGlqbmFt?= <[email protected]>
| Subject: Code to select item in dropdown populated using TableAdapter
| Date: Fri, 30 Dec 2005 00:38:02 -0800
| Lines: 38
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:32127
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi,
|
| In ASP.NET 2.0, where do I write code to select a particular item in a
| dropdown control that is populated using SqlDataSource (Table Adapters)?
|
| I am developing a website in ASP.NET 2.0 for a library using Visual
Studio
| 2005 and SQL Server 2000. I have already built a middle tier with objects
to
| read/write library categories and books to the database. The middleware
is
| working fine.
|
| I have a webform "newBook.aspx" for creating a new book under a
particular
| section. The page takes a querystring parameter "sectionId" under which
the
| book is to be added to.
|
| In the webform, I have a dropdown for listing all sections. This dropdown
| was populated using the smarttag option "choose data source", "new
| datasource", "object", "the middle tier class for sections" and "method
to
| get list of sections". This works fine and the webpage displays correctly
| with the dropdown listbox listing all sections.
|
| However, I am unable to select the item in the dropdown based on the
| sectionId querystring value. In which event do I write the code to make
the
| dropdown selection?
|
| While Debugging, I find that The DataSet.Designer.Cs file events (in
| middleware assembly) to populate the dropdown listbox are executed after
the
| Page_LoadComplete event of the webpage and hence I am confused as to
where I
| should put the code to automatically select the ListItem based on the
| querystring sectionId.
|
| Any help would be greatly appreciated. Also, is there anyway I can see
how
| and where the middleware methods get called? Where are these delegates
| created?
|
| Thanks,
|
| Nahom Tijnam,
| Dubai, UAE
|
 
N

Nahom Tijnam

Hi Steven,

Thank you for your response.

The dropdown values are coming from the database through the middletier
assembly.
My requirement is to just get the querystring value selected in the dropdown
which the user might change later while working in the form.

Following your guidance, in the Page_Load event, I used the DataBind method
of the lstCategory dropdown to redo the binding to the database and followed
with the item selection code as follows:

lstCategory.DataBind();
try
{
int catId = Convert.ToInt32(Request.QueryString["catId"].ToString());
if (catId > 0)
{
lstCategory.SelectedIndex =
lstCategory.Items.IndexOf(lstCategory.Items.FindByValue(catId));
}
}
catch{}

Thanks to you, this is now working perfectly fine with the item getting
automatically selected. But now I have another question related to this as
follows:

In the webpage, without adding any lstCategory.DataBind(), the page was
showing all the sections from the database. how was this happening? Visual
Studio must have called lstCategory.DataBind() somewhere which it is hiding,
isnt it? But now, since I have explicitly called DataBind, will VS call it
again? Where can I see this "Hidden" code of Visual Studio?

Thanks a lot once again.

Cheers,

Nahom Tijnam
Dubai, UAE
 
S

Steven Cheng[MSFT]

Thanks for your Nahom,

yes, since you're binding the DropDownList to a datasource control, the
asp.net DropDownlist control will call certain method to bind data
retrieved from the Datasourcecontrol... This is the feature of using the
DataSource Control in asp.net 2.0 :). And generally this happend during
the page's PreRender event..... (if the certain databound control
associated with a DataSourceControl is hasn't been bound with data, the
control's PreRender code will retreive data from DataSource control and
perform the databinding...)

Also, as I mentioned in previous thread, if we explicitly call
DropDownlist.DataBind(), the internal status of DropDownlist indicate that
it has been bound with data, then the PreRender event won't rebind it
again.....

You can also turn on the Page's Trace and add some code in the
DropDownList's "DataBinding" or "DataBound" event handler to see when it
happend (if we explicitly call DataBind or not....):

protected void DropDownList1_DataBinding(object sender, EventArgs e)
{
Trace.Write("<br>DropDownList1_DataBinding");
}


Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




--------------------
| Thread-Topic: Code to select item in dropdown populated using TableAdapter
| thread-index: AcYNVr4MHI97uXz/QUySMvMQbLEIWA==
| X-WBNR-Posting-Host: 213.42.2.11
| From: =?Utf-8?B?TmFob20gVGlqbmFt?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Code to select item in dropdown populated using TableAdapter
| Date: Fri, 30 Dec 2005 07:36:03 -0800
| Lines: 158
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:32136
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi Steven,
|
| Thank you for your response.
|
| The dropdown values are coming from the database through the middletier
| assembly.
| My requirement is to just get the querystring value selected in the
dropdown
| which the user might change later while working in the form.
|
| Following your guidance, in the Page_Load event, I used the DataBind
method
| of the lstCategory dropdown to redo the binding to the database and
followed
| with the item selection code as follows:
|
| lstCategory.DataBind();
| try
| {
| int catId = Convert.ToInt32(Request.QueryString["catId"].ToString());
| if (catId > 0)
| {
| lstCategory.SelectedIndex =
| lstCategory.Items.IndexOf(lstCategory.Items.FindByValue(catId));
| }
| }
| catch{}
|
| Thanks to you, this is now working perfectly fine with the item getting
| automatically selected. But now I have another question related to this
as
| follows:
|
| In the webpage, without adding any lstCategory.DataBind(), the page was
| showing all the sections from the database. how was this happening?
Visual
| Studio must have called lstCategory.DataBind() somewhere which it is
hiding,
| isnt it? But now, since I have explicitly called DataBind, will VS call
it
| again? Where can I see this "Hidden" code of Visual Studio?
|
| Thanks a lot once again.
|
| Cheers,
|
| Nahom Tijnam
| Dubai, UAE
|
|
| "Steven Cheng[MSFT]" wrote:
|
| > Hi Nahom,
| >
| > Welcome to ASPNET newsgroup.
| > As for the use code to select Item in DropDownList which is populated
with
| > certain DataSource control or TableAdapter, I have some question about
your
| > detailed code logic (of the book creation page...)
| >
| > 1. Is the dropdownlist on that page populated with a certain list
retrieve
| > from database and is not changable?
| >
| > 2. Since you mentioned that you'll select the certain section item in
the
| > dropdownlist with the querystring item, then on the page, will you
allow
| > other user to change it or just make it readonly and not selectable?
| >
| > If you just want to use querystring to set an initial value and can let
the
| > user change it later.... I think you can just retrieve the value from
| > querystring in Page_load event, then, set the
DropDownlist.SelectedValue
| > property to the querystring's value (it should be the value you'll bind
| > through the data source....)...
| >
| > Also, you can manually call DropDownList.DataBind() to redo the
databinding
| > after you set the SelectedValue property.....
| >
| > Please feel free to post here if you have anything unclear or anything
else
| > I didn't quite get...
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| > --------------------
| > | Thread-Topic: Code to select item in dropdown populated using
TableAdapter
| > | thread-index: AcYNHFh6OvwdSHpkQPSuMlrImVxuNw==
| > | X-WBNR-Posting-Host: 195.229.241.180
| > | From: =?Utf-8?B?TmFob20gVGlqbmFt?= <[email protected]>
| > | Subject: Code to select item in dropdown populated using TableAdapter
| > | Date: Fri, 30 Dec 2005 00:38:02 -0800
| > | Lines: 38
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:32127
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Hi,
| > |
| > | In ASP.NET 2.0, where do I write code to select a particular item in
a
| > | dropdown control that is populated using SqlDataSource (Table
Adapters)?
| > |
| > | I am developing a website in ASP.NET 2.0 for a library using Visual
| > Studio
| > | 2005 and SQL Server 2000. I have already built a middle tier with
objects
| > to
| > | read/write library categories and books to the database. The
middleware
| > is
| > | working fine.
| > |
| > | I have a webform "newBook.aspx" for creating a new book under a
| > particular
| > | section. The page takes a querystring parameter "sectionId" under
which
| > the
| > | book is to be added to.
| > |
| > | In the webform, I have a dropdown for listing all sections. This
dropdown
| > | was populated using the smarttag option "choose data source", "new
| > | datasource", "object", "the middle tier class for sections" and
"method
| > to
| > | get list of sections". This works fine and the webpage displays
correctly
| > | with the dropdown listbox listing all sections.
| > |
| > | However, I am unable to select the item in the dropdown based on the
| > | sectionId querystring value. In which event do I write the code to
make
| > the
| > | dropdown selection?
| > |
| > | While Debugging, I find that The DataSet.Designer.Cs file events (in
| > | middleware assembly) to populate the dropdown listbox are executed
after
| > the
| > | Page_LoadComplete event of the webpage and hence I am confused as to
| > where I
| > | should put the code to automatically select the ListItem based on the
| > | querystring sectionId.
| > |
| > | Any help would be greatly appreciated. Also, is there anyway I can
see
| > how
| > | and where the middleware methods get called? Where are these
delegates
| > | created?
| > |
| > | Thanks,
| > |
| > | Nahom Tijnam,
| > | Dubai, UAE
| > |
| >
| >
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top