DropDownList selected value in a datalist

C

Chris Kettenbach

Good morning all,
I am sure this has been asked but I did not see anything.
I have a datalist control. In the edititemtemplate I have a dropdownlist.
I know on the itemdatabound event is where I can set the dropdownlist
selectedindex. How do I set the correct value?

private void lstDegrees_ItemDataBound(object sender, DataListItemEventArgs
e)

{
DataList dl = (DataList)sender;
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlState");
ddl.DataSource = GetStatesLookup();
ddl.DataTextField = "Abbreviation";
ddl.DataValueField = "StateID";
ddl.DataBind();
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText("AL"));
//how do I get what the correct value from the datalist datasource? The
field is StateID
}

}



Any suggestions? Thanks to everyone who responds.

Regards,

Chris
 
S

Siva M

After setting the SelectedIndex property, SelectedValue should get you the
value (in this case, StateID) for the selected item.

Good morning all,
I am sure this has been asked but I did not see anything.
I have a datalist control. In the edititemtemplate I have a dropdownlist.
I know on the itemdatabound event is where I can set the dropdownlist
selectedindex. How do I set the correct value?

private void lstDegrees_ItemDataBound(object sender, DataListItemEventArgs
e)

{
DataList dl = (DataList)sender;
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlState");
ddl.DataSource = GetStatesLookup();
ddl.DataTextField = "Abbreviation";
ddl.DataValueField = "StateID";
ddl.DataBind();
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText("AL"));
//how do I get what the correct value from the datalist datasource? The
field is StateID
}

}



Any suggestions? Thanks to everyone who responds.

Regards,

Chris
 
S

S. Justin Gengo

Chris,

It's easier than that.

ddl.SelectedValue = "AL"

SelectedValue gets the value of the item in the list control or selects the
item in the list control that contains the selected value.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
C

Chris Kettenbach

I am sorry. "AL" is just an example. I want the dropdownlist selected
value to be what the of the StateID was in the datalist datasource. Is
there a way to do that?

In vb.net you use

ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByText(sender.datasource("StateID")))

how do I accomplish this in C#?
 
S

S. Justin Gengo

Chris,

It's the same thing. Get the name of the selection from the list and use:

string Selection;
Selection = [Selection from datasource here];

ddl.SelectedValue(Selection);

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top