set selected value declaratively to dropdownlist

P

podi

Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?

I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode

<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />

</EditItemTemplate>

I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any prompts
 
G

Guest

Hi,
You can set the selectedvalue property on the dropdownlist as follows
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddl"
DataSourceID="SqlDataSource2"
DataValueField="ProductID"
DataTextField="ProductName" SelectedValue='<%# Bind("ProductID")
%>'></asp:DropDownList>
</EditItemTemplate>

Regards,
Mohamed Mosalem
 
T

Tim_Mac

hi podi,
unfortunately the SelectedIndex cannot be set declaratively, it is a compile
error. i'm just wondering how you could know in advance with a databound
list which one to select?
an alternative would be to sort the datasource so that the one you want to
select is the first record, then it will be selected by default when the
list is databound.

i think the databound event is the correct (and best) place to achieve your
task.
i hope this helps
tim
 
P

podi

Thank you for responses, it helped

Tim_Mac said:
hi podi,
unfortunately the SelectedIndex cannot be set declaratively, it is a compile
error. i'm just wondering how you could know in advance with a databound
list which one to select?
an alternative would be to sort the datasource so that the one you want to
select is the first record, then it will be selected by default when the
list is databound.

i think the databound event is the correct (and best) place to achieve your
task.
i hope this helps
tim
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top