Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting selectedvalue of dropdownlist inside a datagrid edittemplate 1

Status
Not open for further replies.

timtom

Programmer
Jul 12, 2001
78
GB
Hi all,
I've got an editable datagrid with various extra dropdownlist controls which I have added into template columns like so

<asp:TemplateColumn HeaderText="Project">
<EditItemTemplate>
<asp:DropDownList id="ddlProject" DataSource="<%# getProjects() %>" OnPreRender="setddlProjectIndex" DataTextField="ProjectName" DataValueField="ProjectIdKey" runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>


I have no trouble retrieving the value that the user selected when saving their changes which I do in the Datagrid's OnUpdate method


public void OnUpdate(Object source, DataGridCommandEventArgs e)
{
...
int iProjectId = Convert.ToInt32(((DropDownList)e.Item.FindControl("ddlProject")).SelectedValue);
...
}



So that's all fine. My problem is that I don't know how to set the selected value of the drop down list in the first place, as soon as the user clicks the edit button. As you can see I suspect I need to have a method which is called on the drop down's OnPreRenderEvent (I've called it setddlProjectIndex here although I haven't written any code).

Can someone tell me how to set the selected value to the value that's already in the database please?

Thanks for any help you can give.
cheers,
Sarah
 
Wouldn't you in the declaration of the dropdownlist add something like this

SelectedIndex="<%# GetIndex(FieldName) %>"

Where the GetIndex function will return the index of the value passed.

Not real sure, but...
 
Thanks for the help both of you,
However, now I know I can pass the container item from the aspx page I have just added a selectedValue property like this

<asp:DropDownList id="ddlProject" DataSource="<%# getProjects() %>" SelectedValue='<%# (int)DataBinder.Eval(Container.DataItem, "ProjectId") %>' DataTextField="ProjectName" DataValueField="ProjectIdKey" runat="server" />


and that works too.
thanks again,
Sarah
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top