I am trying to do this as asked earlier. The only difference that I found is additional List item that was included in above code.
I tried to use AppendDataBoundItems=true
but it is still not working. I also want to set the its default value to the value that was being displayed in label of itemtemplate i.e. DropDownList's SelectedValue='<%# Eval("DepartmentName") %>'
but thie property is not available to me in dropdownlist.
What could be the reason. ??
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartment_Edit" runat="server"
DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName"
DataValueField="PK_DepartmentId">
</asp:DropDownList>
<asp:SqlDataSource ID="dsDepartment_Edit" runat="server"
ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"
ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown"
SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' >
</asp:Label>
</ItemTemplate>
I am using GridView
This is the best i have found....
The use of the
GridView_DataBound
event handler solves the problem.In your case you will need to add a
HiddenField
to store thePK_DepartmentId
value:Why are you guys suggesting to use loops, when there is a
GridView
method specifically made for when a row's condition changes - theRowDataBound()
?On your grid there is an event called
ItemCommand
. Create a method for it:Now simply create a case statement that will recognize when the user has clicked the edit button on the grid:
Now you have a member variable set to the string you want to be selected before the dropdown is even loaded/prerendered. Use the event
OnPrerender
orOnLoad
for thedropdownbox
and set the selected item to this String.DataValueField
seems to be wrong - shouldn't it beDepartmentId
? Similarly, you need to haveSelectedValue='<%# Eval("**DepartmentId**") %>'
-DepartmentName
would be theSeletectText
.