在ASP.Net GridView的OnRowDataBoundevent
中,我试图填充Department DropDownList(ddlDepts),这将在编辑行时显示。
以下代码无效,因为DropdownList为空。
任何想法我做错了什么?
'// GridView标记:
<asp:TemplateField HeaderText="Department" ItemStyle-Width = "150">
<ItemTemplate>
<asp:Label ID = "lblDept" runat="server" Text='<%# Eval("deptName")%>'></asp:Label>
<asp:DropDownList ID="ddlDepts" runat="server" Visible = "false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
'' //的CodeFile
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cmd As New SqlCommand("SELECT DISTINCT(DeptID,DeptName) FROM Departments")
Dim ddlDepts As DropDownList = TryCast(e.Row.FindControl("ddlDepts"), DropDownList)
ddlDepts.DataSource = Me.ExecuteQuery(cmd, "SELECT")
ddlDepts.DataTextField = "DeptID"
ddlDepts.DataValueField = "DeptName"
ddlDepts.DataBind()
Dim DeptName As ListItem = ddlDepts.Items.FindByValue("lblDept")
If DeptName IsNot Nothing Then
ddlDepts.SelectedValue = DeptName.Value
End If
End If
End Sub
答案 0 :(得分:0)
不确定您使用的是哪种SQL,但我认为查询可能不正确。
在MS SQL中,您可以像这样使用它:
now
我不了解MySQL(或其他数据库),但quick search似乎表明它对SELECT DISTINCT DeptID, DeptName FROM Departments
使用了相同的方法。