473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.net 2.0 DropdownList bound to SQLDataSource and setting SelectedIndex

I have two dropdownlists that are bound to the same datasource and I
have a couple of questions:

1) Because they are bound to the same datasource, I am assuming that
they only make 1 round trip to the DB server -- is that correct?

2) On a user's click of a button, one of the actions in the button
OnClick event is to set the selectedindex of the 2nd DDL to that of the
1st. I get a runtime error here that says that the given index is not
in the list of items. Is this because the button event is firing
before the listitems are populated? If so how do I get the list items
to populate 1st? Also the list items will probably not change in the
term of a user session, is there a way to prevent the page from
retreiving the listitems on each postback?

Here is my relevant code:

<asp:SqlDataSou rce ID="dsICD9" runat="server"
ConnectionStrin g="<%$...%>"
SelectCommand=" ...">
</asp:SqlDataSour ce>

<asp:DropDownLi st AppendDataBound Items="true" ID="ddl1"
runat="server" DataSourceID="d sICD9"
onkeypress="ret urn KeySortDropDown List_onkeypress (this,false)"
DataTextField=" description" DataValueField= "ICD9">

<asp:ListItem Selected="True" ></asp:ListItem>
</asp:DropDownLis t>

<asp:DropDownLi st AppendDataBound Items="true" ID="ddl2"
runat="server" DataSourceID="d sICD9"
onkeypress="ret urn KeySortDropDown List_onkeypress (this,false)"
DataTextField=" description" DataValueField= "ICD9">

<asp:ListItem Selected="True" ></asp:ListItem>
</asp:DropDownLis t>

Protected Sub AddCPT_Click(By Val sender As Object, ByVal e As
System.EventArg s)
...
ddl2.SelectedIn dex = ddl1.SelectedIn dex <-----Here's where I get
the runtime error
...
End Sub

Sep 13 '06 #1
1 7232
Hi Jason,

Your code seems to work against my database. Here's what I used:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

<script runat="server">

Protected Sub AddCPT_Click _
(ByVal sender As Object, _
ByVal e As System.EventArg s)
ddl2.SelectedIn dex = ddl1.SelectedIn dex
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:sqldatasou rce id="dsICD9" runat="server" connectionstrin g="<%$
ConnectionStrin gs:authorsConne ctionString %>" selectcommand=" SELECT
[FirstName], [AuthorID] FROM [Authors]"></asp:sqldatasour ce>
<asp:dropdownli st id="ddl1" runat="server"
appenddatabound items="true" datasourceid="d sICD9" datatextfield=" FirstName"
datavaluefield= "AuthorID" >
<asp:listitem selected="True" ></asp:listitem>
</asp:dropdownlis t>
<asp:dropdownli st id="ddl2" runat="server"
appenddatabound items="true" datasourceid="d sICD9" datatextfield=" FirstName"
datavaluefield= "AuthorID" >
<asp:listitem selected="True" ></asp:listitem>
</asp:dropdownlis t>
<asp:button id="AddCPT" runat="server" text="Button"
onclick="AddCPT _Click" />
</div>
</form>
</body>
</html>
"Jason Wilson" <wi*****@ausrad .comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>I have two dropdownlists that are bound to the same datasource and I
have a couple of questions:

1) Because they are bound to the same datasource, I am assuming that
they only make 1 round trip to the DB server -- is that correct?

2) On a user's click of a button, one of the actions in the button
OnClick event is to set the selectedindex of the 2nd DDL to that of the
1st. I get a runtime error here that says that the given index is not
in the list of items. Is this because the button event is firing
before the listitems are populated? If so how do I get the list items
to populate 1st? Also the list items will probably not change in the
term of a user session, is there a way to prevent the page from
retreiving the listitems on each postback?

Here is my relevant code:

<asp:SqlDataSou rce ID="dsICD9" runat="server"
ConnectionStrin g="<%$...%>"
SelectCommand=" ...">
</asp:SqlDataSour ce>

<asp:DropDownLi st AppendDataBound Items="true" ID="ddl1"
runat="server" DataSourceID="d sICD9"
onkeypress="ret urn KeySortDropDown List_onkeypress (this,false)"
DataTextField=" description" DataValueField= "ICD9">

<asp:ListItem Selected="True" ></asp:ListItem>
</asp:DropDownLis t>

<asp:DropDownLi st AppendDataBound Items="true" ID="ddl2"
runat="server" DataSourceID="d sICD9"
onkeypress="ret urn KeySortDropDown List_onkeypress (this,false)"
DataTextField=" description" DataValueField= "ICD9">

<asp:ListItem Selected="True" ></asp:ListItem>
</asp:DropDownLis t>

Protected Sub AddCPT_Click(By Val sender As Object, ByVal e As
System.EventArg s)
...
ddl2.SelectedIn dex = ddl1.SelectedIn dex <-----Here's where I get
the runtime error
...
End Sub

Sep 14 '06 #2

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

Similar topics

3
4375
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would be really grateful for any assistance! Thanks
10
10763
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected = true; Where endTime is a string containing "15".
4
4491
by: P. Yanzick | last post by:
Hello, I've been playing with master/detail views as well as editing in the gridview, and I ran across a strange problem that I am not exactly sure where to go to try to solve. I have 2 tables, a table of cars (pretty basic, an ID, a description, and a Color ID) and a table of colors (Color ID, and a color description). I've added a gridview and a detailsview as I'm playing with both and how to get editing features to work the way I...
6
8269
by: Dabbler | last post by:
I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to an SqlDataSource, then have a value from a boundfield in the row be passed as the keyfield for select where clause. Im trying to load the ddl with a list of dates from another table keyed on GridView row field that only apply to this row. Any suggestions would be appreciated.
2
7914
by: Dabbler | last post by:
In my Registrant FormView I have a DropDownList which loads data from a secondary SqlDataSource "sdsOfficeParks". I need the user to select an office park but save the selected value in the FormView's ObjectDataSource "odsRegistrant" . The following solution can't work because I need the ddl selected value bound to my odsRegistrant but both tables have the same column name for primary key. <asp:DropDownList ID="ddlOfficePark"...
1
5405
by: clickon | last post by:
Forget about the controlParameter for the moment, for testing purposes i have created the following Markup: <asp:Table ID="tblSelectRoute" runat="server" CssClass="asp-table"> <asp:TableRow> <asp:TableCell CssClass="asp-table-header">Select Route<asp:Label ID="lblTest" runat="server"></asp:Label> </asp:TableCell>
3
2729
by: gsauns | last post by:
Hello, I have an ASP.NET app with a DropDownList on the page, which is bound to values from a table. I have a Repeater control on the page whose displayed data is dependent on the DropDownList value selected by the user. (The DataTextField is a Datetime concatenated with a string). On the Page_Load event, if it's not a postback, I would like the DropDownList to programmatically be set to the next occurring day in the DropDownList (the...
1
2289
by: ken_wilsonii | last post by:
I ran into this problem it ended up being a combination of this that needed to be done. I believe this will work for a bound list to. The whole problem stems from the two-way binding which has caused me several other headaches. My example is derived from binding a DETAILSVIEW to a SQLDATASOURCE. The field is then converted to a template field for which I modify the EditTemplate for that
1
4933
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over time. The problem is that when an item has been removed from the lookup table, and a user wants to retrieve a record that used the deleted item, the following error occurs: 'ddlAssignedTo' has a SelectedValue which is invalid because it...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.