Monday, March 26, 2012

Setting selected item in dropdownlist

Hi, I know this has been covered a number of times and believe me I've tried but I can't make it work for the life of me. What I want is for, when the edit button in a datagrid is clicked and the dropdownlist appears, that the ddl is set to the same item as the field rather than the first in the list. so on and so forth.
here is my code. Please help

Dim TRDDLds As DataSet = New DataSet()
Function TrainerList() As DataSet
Dim sqlStr As String = "SELECT left(namelast,8)+' ,'+ left(namefirst,3) as contname FROM tbltrainers"
Dim objTRAdapter As SqlDataAdapter = New SqlDataAdapter(sqlStr, objConn)
TRDDLds.Clear()
objTRAdapter.Fill(TRDDLds, "tbltrainers")
Return TRDDLds
End Function

Then in the html for datagrid


<EditItemTemplate>
<TABLE border="0">
<TR>
<TD align="left"><B style="FONT-SIZE: 10pt; OVERFLOW: hidden">Trainer</B></TD>
<TD>
<asp:dropdownlist id="trainers1" Width ="85" runat="server" Font-Size="10pt" DataSource="<%#trainerlist%>" DataTextField="contname" datavaluefield="contname">
</asp:dropdownlist></TD>
</TR>
<TR>
<TD align="left"><B style="FONT-SIZE: 10pt">Client</B></TD>
<TD>
<asp:DropDownList id=clients1 Font-Size="10pt" Width ="85" DataSource="<%#clientlist%>" DataTextField="contname" DataValueField="contname" Runat="server">
</asp:DropDownList></TD>
</TR>
</TABLE>
</EditItemTemplate>

Thanks
RaifWhen you click edit in your grid you must be able to get somthing the is equal to the value or text of you ddl you want to set. Example, you hit edit, you then need to get a value from the row selected. Lets say you setup a hidden field in the dg that contains the ddl value. You get that value then loop through the ddl and when you find the Item[i].value that is equal to your ddl value you set your selected index to i.

if this is of no help, please post the funciton you have assigned to your edit button.
This article does a lot with dropdownlists in a datagrid.

0 comments:

Post a Comment