473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Set dropdownlist from static data

I have a dropdownbox object:

<asp:DropDownList ID="ddlQuestionType" runat="server">
<asp:ListItem Value="MS" Text="Multiple Single" />
<asp:ListItem Value="MM" Text="Multiple Multiple" />
<asp:ListItem Value="TF" Text="True/False" />
</asp:DropDownList>

The values and text are static. I am reading from a table:

Select QuestionType from myTable where id = 10

and QuestionType will either be "MS", "MM" or "TF".

How do I bind the data to this object?

With a Label it would be something like:

<asp:label id="Question" Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionUnique") %>' runat="server" />

Thanks,

Tom.
Nov 19 '05 #1
10 11218
Databinding from a SQL table is easy, you should be able to find many
examples out there.

You need to know more about this database you are querying though.

1.) Is it a MS SQL server or is it another type?
2.) Do you have to login to the server, or does it accept inherited
credentials?
3.) Do you have the database name you are connecting to?

Nov 19 '05 #2
"Chad Devine" <su*****@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Databinding from a SQL table is easy, you should be able to find many
examples out there.

You need to know more about this database you are querying though.

1.) Is it a MS SQL server or is it another type?
2.) Do you have to login to the server, or does it accept inherited
credentials?
3.) Do you have the database name you are connecting to?


Actuall, no.

How I get the data is not the issue here.

I can get the data fine. It will always be either MM, MS or TF. I also
have no problem binding to a textbox, label, datagrid etc.

<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %>

I want to do the same type of thing here where my selected item will display
based on the data returned.

Somehow, I need to do something like:

questionType.SelectedItem.value = <%# DataBinder.Eval(Container.DataItem,
"QuestionType") %>

So that the value will show correctly in the dropdownbox.

Thanks,

Tom
Nov 19 '05 #3
Oh so you want the dropdownlist to change the currently selected item
to the one that's in the table? I have tried doing that in the past but
haven't had any luck, I'm only intermediate in skill with asp.net so
hopefully someone else can provide a solution for you.

It may require you to go as far as using a custom control for what you
are trying to do.

Nov 19 '05 #4
"Chad Devine" <su*****@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Oh so you want the dropdownlist to change the currently selected item
to the one that's in the table? I have tried doing that in the past but
haven't had any luck, I'm only intermediate in skill with asp.net so
hopefully someone else can provide a solution for you.

It may require you to go as far as using a custom control for what you
are trying to do.


I hope not.

I assume that other people must do this. The problem with other examples,
is that they assume you want to populate the list with the results from your
table. But if you are looking at data that was already input and want to
allow the user to make a change to that data later, you would set the
dropdown to the data they had already chosen and allow them to choose
something else from the dropdown.

Thanks,

Tom.

Nov 19 '05 #5
Yeah, I completely agree with you and what you say makes sense.

Many suggest that doing a
If not ispostback then
DropDownList.DataBind()
End If

So that the databind doesn't happen unless the person refreshes the
page, that way it keeps whatever the user selected, however in the case
of a table, when the user hits submit it refreshes the whole page so
that method doesn't work in this case. I really wish I could help you
in this, but I can't seem to find a property of the dropdownlist that
allows you to change the current selected item. You only seem to get
that chance right in the beginning when you define what options and
values to go into the list.

Nov 19 '05 #6
Look here, I think this person is doing what you want to do:
http://groups-beta.google.com/group/...57b5a7b406fa52

Nov 19 '05 #7
I just figured out your answer... I did it for myself, but with
autopostback it presents a little problem, when calling it in the
Page_Load sub because it won't let you change the current selected
item, as it keeps trying to select the previous one posted for some
reason.

Anyway, I put it in the sub Page_PreRender and it worked just fine.

Here's the code:
Sub Page_PreRender
dropListOptions.SelectedValue = intPageNumber
End Sub

<asp:DropDownList
ID="dropListOptions"
AutoPostBack="True"
OnSelectedIndexChanged="dropListOptions_SelectedIn dexChanged"
font-size="10"
Runat="Server" >
<asp:ListItem id="Default" Text="Default" Value="25" />
<asp:ListItem Text="50" Value="50" />
<asp:ListItem Text="75" Value="75" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="150" Value="150" />
<asp:ListItem Text="200" Value="200" />
</asp:DropDownList>

Hope this helps!

Nov 19 '05 #8
Another update, I found that only works for Value properties, so if
your databinding this, this would be a problem... and then we're back
to square one.

Nov 19 '05 #9
Ignore the above post, I made an error in my code because I was writing
it too fast.

Anyway, this solution works fine, and it's all in sub code which I
think is better... that way you don't have to scroll between variables
in your html and your subs, it's all in one place. Again, I hope this
helps.

Nov 19 '05 #10
"Chad Devine" <su*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Ignore the above post, I made an error in my code because I was writing
it too fast.

Anyway, this solution works fine, and it's all in sub code which I
think is better... that way you don't have to scroll between variables
in your html and your subs, it's all in one place. Again, I hope this
helps.
I'll look at it and see how it works and let you know.

Thanks for the help,

Tom

Nov 19 '05 #11

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

Similar topics

2
by: rmorvay | last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a grid. I tried to utilize the following code but I am stuck at the point where I bind the dropdownlist to the grid cell. ...
4
by: theo | last post by:
Program flow...load file,then extract the xml text tags from the file,then the number of Xml tags retrieved from the file determines the number of dropdownlist controls instanciated in the...
18
by: Julia Hu | last post by:
Hi, I have a datagrid, and in different rows I need to programmatically bind different type of controls and load data into these controls. For example,in the first row I need to bind data into a...
10
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
10
by: Assimalyst | last post by:
Hi, I'm attempting to use a data reader to load data from a single table, tblCountry, with two columns, countryNo (the Key) and countryName, into a DropDownList box. Here's the code: ...
0
by: pradeepthakar | last post by:
i want to display "select" in my dropdownlist control on my page . this dropdownlist is supposed to retrieve values from database on selection of items in the dropdownlist . in this process, the...
2
by: =?Utf-8?B?VmljdG9yaW91czE=?= | last post by:
When the editcommand is execured for my datagrid the dropdown list appears for the bound item status. However, the item selected by default does not match the unedited data. How do I make the...
1
by: bogdan | last post by:
Hi, I'd like to use a dropdownlist in a formview to map numeric values to 'user-friendly' descriptions. The dropdownlist items' values need to be bound to a column in a db table. For example,...
5
by: nzkks | last post by:
Hi I am using these: ASP.Net 2.0, VB.Net, Visual Studio 2005, SQL Server 2005, Formview controls In a ASP.Net form I have 20 textboxes and 20 dropdownlists(ddl). All ddl(s) are databound and get...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
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,...

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.