Populate DropDown from code

D

David C

I have a DropDownList control that is currently tied to a SqlDataSource
control. However, I need that same type of control in many other pages. I
was thinking of some class function in App_Code folder that retrieved the
Text and Value properties of my DropDownList by simply making the DataSource
a function call. My DropDownList is a simple State Abbreviation and State
Name, e.g. 'AL' and 'Alabama', etc.

Can someone point me to a sample of this that I can use?
p.s. This is an intranet application currently but may go to partly
internet.
Thanks.

David
 
D

David C

Mark Rae said:
Do yourself a *HUGE* favour and forget all about the SqlDataSource and
related "training wheels" controls and build yourself a proper DAL.

Microsoft's DAAB is a good place to start:
http://www.microsoft.com/downloads/...8B-2986-47F7-B529-3E41584B6CE5&displaylang=en

I created some DAL code and it works great. Now I would like to set the
DropDownList dynamically in my code-behind page. Below is my DropDownList
and the hard coded DataSource. I want to be able to set the value that
shows as 27 to any number based on conditions. Thanks for your help.
-David

<asp:DropDownList ID="ddlCaseWorkerID" runat="server" SelectedValue='<%#
Bind("CaseWorkerID") %>'
DataValueField="PeopleLinkID"
DataTextField="LastFirst" DataSource='<%# LookupClass.GetCaseWorkers(27) %>'
AppendDataBoundItems="True">
<asp:ListItem Value="" Text=""></asp:ListItem>
</asp:DropDownList>
 
D

David C

Mark Rae said:
You need to forget *completely* about all of the training wheels stuff and
do everything properly in code. So...

<asp:DropDownList ID="ddlCaseWorkerID" runat="server" />

private void Page_Init(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
ddlCaseWorkerID.DataSource = LookupClass.GetCaseWorkers(27);
ddlCaseWorkerID.DataValueField = "PeopleLinkID";
ddlCaseWorkerID.DataTextField = "LastFirst";
ddlCaseWorkerID.DataBind();
ddlCaseWorkerID.Items.Insert(0, new ListItem("Text", ""));
}
}

I am learning.

I changed my DropDownList and code to below but now I get nothing in the
DropDownList selections.
FYI this is in a FormView and I only use it in Edit mode so I cannot use the
code in initial Page_Load. Any ideas? Thanks.

David

<asp:DropDownList ID="ddlCaseWorkerID" runat="server"
AppendDataBoundItems="True">
</asp:DropDownList>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim tb As TextBox

If Page.IsPostBack = False Then
'1st time in page
Else
If fvPeople.CurrentMode = FormViewMode.Edit Then
Dim row As FormViewRow = fvPeople.Row
tb = Page.Master.FindControl("txtBranch")
Dim intBranch As Integer = Convert.ToInt16(tb.Text)
Dim ddl As DropDownList = row.FindControl("ddlCaseWorkerID")
ddl.DataSource = LookupClass.GetCaseWorkers(intBranch)
ddl.DataValueField = "PeopleLinkID"
ddl.DataTextField = "LastFirst"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("None", ""))
ddl.SelectedValue =
row.FindControl("txtCaseWorkerID").ToString
End If
End If

End Sub
 
D

David C

Nevermind...I figured it out. Thanks for your help. I may never use
DataSourceControls again.

David
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top