By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,931 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

DropDownList selected value in a datalist

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


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

"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:ld********************@giganews.com...
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

Nov 19 '05 #2

P: n/a
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
"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:ld********************@giganews.com...
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

Nov 19 '05 #3

P: n/a
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.data source("StateID")))

how do I accomplish this in C#?

"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:ug**************@TK2MSFTNGP11.phx.gbl...
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
"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:ld********************@giganews.com...
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


Nov 19 '05 #4

P: n/a
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
"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:cq******************************@giganews.com ...
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.data source("StateID")))

how do I accomplish this in C#?

"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:ug**************@TK2MSFTNGP11.phx.gbl...
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
"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:ld********************@giganews.com...
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



Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.