Welcome to WebmasterWorld Guest from

Forum Moderators: ocean10000

Message Too Old, No Replies

Drop Down box prefilled in

6:49 pm on Jun 24, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
votes: 0

I am trying to prepopulate a dropdown box with a value from the database, but I get this error -->

Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Code blocks are not supported in this context.

Source Error:

Line 61: <asp:DropDownList id="ddlSTATE" runat="server">
Line 62: <asp:ListItem>Please Select</asp:ListItem>
Line 63: <asp:ListItem Value=""><%=strstate %></asp:ListItem>
Line 64:
Line 65: <asp:ListItem Value="AL">Alabama</asp:ListItem

This is what my code looks like. I want <%=strstate%> filled in so it is the first choice. If there is an easier, way I am open to that too.

<asp:DropDownList id="ddlSTATE" runat="server">
<asp:ListItem>Please Select</asp:ListItem>
<asp:ListItem Value=""><%=strstate %></asp:ListItem>

<asp:ListItem Value="AL">Alabama</asp:ListItem>

<asp:ListItem Value="AK">Alaska</asp:ListItem>

<asp:ListItem Value="AZ">Arizona</asp:ListItem>

<asp:ListItem Value="AR">Arkansas</asp:ListItem>

7:11 pm on June 24, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
votes: 0

One possibility is to select the correct list item from code behind:

Dim li As ListItem = ddlSTATE.Items.FindByText(strstate)
If Not li Is Nothing Then
li.Selected = True
End If
7:12 pm on June 24, 2010 (gmt 0)


WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
votes: 3

This is some code I have used since version 1.0 of dot.net Its purpose is to select one item in the dropdown and if the item doesn't exist it will add it to the list.

You can just call the following then in your databinding code.

FillValue(ddlSTATE,strstate );

/// <summary>
/// Takes already populated dropdown and selects the item in the list that matches the value
/// you passed in
/// </summary>
/// <param name="DropDown">dropdown which you want to select an item in</param>
/// <param name="Value">the value you wish to have selected in the dropdown</param>
static public void FillValue(System.Web.UI.WebControls.DropDownList DropDown, string Value)
//Deselects anything that is currently selected.
if (DropDown.SelectedIndex >=0)
DropDown.SelectedItem.Selected =false;
if (Value==null)
Value =string.Empty;
Value = Value.Trim();
//this findbyvalue is case sensitive
if (DropDown.Items.FindByValue(Value) != null)
DropDown.Items.FindByValue(Value).Selected = true;
if (Value != string.Empty)
//doing this to save a few cpu cycles. because it will be used in a loop
string lValue = Value.ToLower();
//this for loop will look for the value no mater the upper /lower case
for (int Count =0;Count <= DropDown.Items.Count -1;Count ++)
if (DropDown.Items[Count].Value.ToLower().Equals(lValue) ==true)
DropDown.Items[Count].Selected =true;
//this will exit us out of the function gracefully
//Creates the Item in the List if it doesn't already
DropDown.Items.FindByValue(Value).Selected = true;
8:23 pm on June 24, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
votes: 0

Thank you both. I will work on this first thing tomorrow morning.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members