J
Jenna Alten
I have a datagrid with a template column that contains a dropdown list.
I currently fill and display the dropdown list on the page load. This is
working correctly. I am NOT using an Edit Column. I am receiving errors
when trying to set the selected value of the dropdown within the
ItemDataBound() subroutine. Below is the code I am using. I hope someone
can tell me if this is possible.
'''HTML'''
<asp:datagrid id="dgAssignments" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn SortExpression="PermAssignment"
HeaderText="Permanent <br> Assignment"
HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<aspropDownList Runat="server" ID="ddlPermanentAssignment"
DataTextField="DisplayName"
DataValueField="EmployeeID"></aspropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</asp:datagrid>
'''Code Behind'''
Private Sub dgAssignments_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgAssignments.ItemDataBound
Dim ddlPermanentAssignment As DropDownList =
CType(e.Item.FindControl("ddlPermanentAssignment"), DropDownList)
Dim ddlTemporaryAssignment As DropDownList =
CType(e.Item.FindControl("ddlTemporaryAssignment"), DropDownList)
If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType
= ListItemType.Item Then
'Retrieve Employees for Dropdown Lists
Dim GetEmployees As SqlClient.SqlCommand = New
SqlClient.SqlCommand("spvGetEmployees", Me.DataConnection)
Dim DaEmployees As New SqlClient.SqlDataAdapter
Dim dsEmp As New DataSet
GetEmployees.CommandType = CommandType.StoredProcedure
GetEmployees.Parameters.Add("RETURN_VALUE", SqlDbType.Int).Direction =
ParameterDirection.ReturnValue
DaEmployees.SelectCommand = GetEmployees
DaEmployees.Fill(dsEmp)
'Load Permanent Assignment Dropdown List
ddlPermanentAssignment.DataSource = dsEmp
ddlPermanentAssignment.DataBind()
'''ERROR OCCURS WITHIN CODE BLOCK BELOW''''
'Set Current Permanent Employee
Dim CurrentPermanent As String = DataBinder.Eval(e.Item.DataItem,
"EmployeeID")
Dim liPermanent As ListItem =
ddlPermanentAssignment.Items.FindByValue(CurrentPermanent.ToString())
If Not (liPermanent Is Nothing) Then
liPermanent.Selected = True
End If
End If
End Sub
I currently fill and display the dropdown list on the page load. This is
working correctly. I am NOT using an Edit Column. I am receiving errors
when trying to set the selected value of the dropdown within the
ItemDataBound() subroutine. Below is the code I am using. I hope someone
can tell me if this is possible.
'''HTML'''
<asp:datagrid id="dgAssignments" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn SortExpression="PermAssignment"
HeaderText="Permanent <br> Assignment"
HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<aspropDownList Runat="server" ID="ddlPermanentAssignment"
DataTextField="DisplayName"
DataValueField="EmployeeID"></aspropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</asp:datagrid>
'''Code Behind'''
Private Sub dgAssignments_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgAssignments.ItemDataBound
Dim ddlPermanentAssignment As DropDownList =
CType(e.Item.FindControl("ddlPermanentAssignment"), DropDownList)
Dim ddlTemporaryAssignment As DropDownList =
CType(e.Item.FindControl("ddlTemporaryAssignment"), DropDownList)
If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType
= ListItemType.Item Then
'Retrieve Employees for Dropdown Lists
Dim GetEmployees As SqlClient.SqlCommand = New
SqlClient.SqlCommand("spvGetEmployees", Me.DataConnection)
Dim DaEmployees As New SqlClient.SqlDataAdapter
Dim dsEmp As New DataSet
GetEmployees.CommandType = CommandType.StoredProcedure
GetEmployees.Parameters.Add("RETURN_VALUE", SqlDbType.Int).Direction =
ParameterDirection.ReturnValue
DaEmployees.SelectCommand = GetEmployees
DaEmployees.Fill(dsEmp)
'Load Permanent Assignment Dropdown List
ddlPermanentAssignment.DataSource = dsEmp
ddlPermanentAssignment.DataBind()
'''ERROR OCCURS WITHIN CODE BLOCK BELOW''''
'Set Current Permanent Employee
Dim CurrentPermanent As String = DataBinder.Eval(e.Item.DataItem,
"EmployeeID")
Dim liPermanent As ListItem =
ddlPermanentAssignment.Items.FindByValue(CurrentPermanent.ToString())
If Not (liPermanent Is Nothing) Then
liPermanent.Selected = True
End If
End If
End Sub