Asp.net Gridview下拉列表绑定
我在获取下拉列表时遇到了很多麻烦,无法将数据库中的数据与适当的部门绑定 这就是我到目前为止所做的: HTML: 当我运行此程序时,它会抛出一个异常,表示: 对象引用未设置为对象的实例 顺便说一句:我使用本教程帮助我完成以下任务:Asp.net Gridview下拉列表绑定,asp.net,vb.net,gridview,Asp.net,Vb.net,Gridview,我在获取下拉列表时遇到了很多麻烦,无法将数据库中的数据与适当的部门绑定 这就是我到目前为止所做的: HTML: 当我运行此程序时,它会抛出一个异常,表示: 对象引用未设置为对象的实例 顺便说一句:我使用本教程帮助我完成以下任务: 任何帮助都将不胜感激!谢谢大家! 您只需检索部门id并将其作为隐藏项存储在gridview中(如果您不想显示它) 希望有帮助。您的问题是您试图在行中查找contorl,但它位于表格单元格中 试试这个 Dim cbo As DropDownList = CType(You
任何帮助都将不胜感激!谢谢大家! 您只需检索部门id并将其作为隐藏项存储在gridview中(如果您不想显示它)
希望有帮助。您的问题是您试图在行中查找contorl,但它位于表格单元格中 试试这个
Dim cbo As DropDownList = CType(YourDGV.Rows(x).Cells(0).FindControl("ddlDepartment"), DropDownList)
您在哪一行收到此错误?
hrGetDepartmentList
VB.Net中是一种静态方法还是共享方法?另外,请检查:它是共享的。我已经用这个方法在我的页面上填写了所有的下拉列表。但在gridview中,我有很多问题需要解决。什么是dept?FindByText(dept)?当我实际将数据绑定到datagrid(未包含在文章中)时,会引发错误,但这是由于gridDepartmentHistory.RowDataBound过程造成的,因为如果我删除该代码,gridview将绑定得很好。@GiusepeMoreno-好的,请检查最后一行。.其余代码看起来很好,最后一行应该是ddlDepartment.Items.FindByText(ddlDepartment).Selected=True
感谢您花时间审阅我的问题。实际上是一些愚蠢的事情导致了这个错误。DropDownList被设置为大写,而我获取文本的标签使用的是ProperCase。我可能会更改它,以便它获得部门ID,而不是名称。
Protected Sub gridDepartmentHistory_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridDepartmentHistory.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim ddlDepartment As DropDownList = CType(e.Row.FindControl("ddlDepartment"), DropDownList)
Dim list As ICollection(Of Department) = Department.hrGetDepartmentList() 'Class method to fill a collection of items with the Department's Name and ID
ddlDepartment.DataSource = list
ddlDepartment.DataTextField = "Name"
ddlDepartment.DataValueField = "ID"
ddlDepartment.DataBind()
Dim dept As String = CType(e.Row.FindControl("lblDepartment"), Label).Text
ddlDepartment.Items.FindByText(dept).Selected = True
End If
End Sub
Dim dept As String = CType(e.Row.FindControl("lblDepartmentId"), Label).Text
ddlDepartment.SelectedValue = dept;
Dim cbo As DropDownList = CType(YourDGV.Rows(x).Cells(0).FindControl("ddlDepartment"), DropDownList)