0
在ASP.Net的GridView的OnRowDataBoundevent
我试图填充部的DropDownList一行时,编辑将要显示(ddlDepts)。为什么在Dropdownlist OnRowDataBound事件中没有选择一个项目?
下面的代码是不是因为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
你在哪里初始化到包含部门表的数据库的连接? –
可能愚蠢的问题,但你肯定你的OnRowDataBound事件实际上是发射呢? –
@inquisitive_mindm很好的问题。我拥有了它,并开始修改代码并将其删除。它下面是: gvInPerson.DataSource =的GetData(CMD)和连接到DB在的getData()子定义。它没有帮助。 大卫W,没有这样的事情愚蠢的问题,但不知道为什么它会虽然射击。 – Tairoc