获得答案的一半是知道如何提出问题.我不确定我做得很好,但这是我最好的一击.
我正在尝试将ddl与gridview中的数据绑定,而不是来自gridview本身.这是在EditItemTemplate中.这样做的目的是让用户从查找存储过程开始选择值和一系列其他值.
我在这里提到我之前已成功完成此操作但使用了ObjectDataSource.我这次试图避免这种情况,现在完全从后面的代码中完成,然后将其移到数据层.
这是我到目前为止所拥有的……
<asp:GridView ID="usersGrid" runat="server" DataKeyNames="userID" AutoGenerateColumns="false" Width="580" OnRowUpdating="usersGrid_RowUpdating" OnRowEditing="usersGrid_RowEditing" OnRowCancelingEdit="usersGrid_RowCancelingEdit" OnRowDeleting="usersGrid_RowDeleting" >
…
<EditItemTemplate> <div class="gridName"> <asp:TextBox ID="txtFirstName" Text='<%#Eval("firstName") %>' runat="server" Width="95" /> </div> <div class="gridName"> <asp:TextBox ID="txtLastName" Text='<%#Eval("lastName") %>' runat="server" Width="95" /> </div> <div class="gridEmail"> <asp:TextBox ID="txtEmail" Text='<%#Eval("email") %>' runat="server" Width="245" /> </div> <div class="gridName"> <asp:DropDownList ID="ddl_GetLists" DataSourceID="GetListData()" AppendDataBoundItems="true" DataValueField="listID" DataTextField="listName" SelectedValue='<%#Bind("listID") %>' runat="server" > </asp:DropDownList> </div> </EditItemTemplate>
….
Protected Sub usersGrid_RowEditing(ByVal sender As Object,ByVal e As GridViewEditEventArgs) usersGrid.EditIndex = e.NewEditIndex BindData() End Sub
….
Private Sub BindData() Dim conn As New sqlConnection(connectionString) Dim ad As New sqlDataAdapter("MAINT_DIST_GET_USERS",conn) Dim ds As New DataSet() ad.Fill(ds) GetListData() usersGrid.DataSource = ds usersGrid.DataBind() End Sub
…
Protected Sub usersGrid_RowDataBound(ByVal sender As Object,ByVal e As GridViewRowEventArgs) If e.Row.RowState = DataControlRowState.Edit Then Dim ddl As DropDownList = DirectCast(e.Row.FindControl("ddl_GetLists"),DropDownList) Dim conn As New sqlConnection(connectionString) Dim ad As New sqlDataAdapter("MAINT_DIST_GET_LISTS",conn) Dim ds As New DataSet() ad.Fill(ds) ddl.DataSource = ds ddl.DataBind() End If End Sub Public Function BindDropdown() As DataSet Dim conn As New sqlConnection(connectionString) Dim ad As New sqlDataAdapter("MAINT_DIST_GET_LISTS",conn) Dim ds As New DataSet() ad.Fill(ds) ddl_GetLists.DataSource = ds ddl_GetLists.DataBind() End Function
我还会问,为什么在最终函数中,为什么控件ddl_GetLists也不被识别?在网格内部,它从设计器中消失,但在网格之外再次出现.
感谢大家的帮助.
解决方法
你有几个选择.您可以使用数据源控件,也可以在GridView的RowDataBound事件中绑定代码隐藏中的下拉列表.
我注意到你的代码中也存在一些问题.在您的示例中,您错误地分配了DataSourceID. DataSourceID应指向页面上数据源控件的ID:
<asp:DropDownList ID="ddl_GetLists" DataSourceID="sqlDataSource1" AppendDataBoundItems="true" DataValueField="listID" DataTextField="listName" SelectedValue='<%#Bind("listID") %>' runat="server"> </asp:DropDownList> <asp:sqlDataSource ID="sqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" SelectCommand="SELECT listID,listName FROM SoMetable"> </asp:sqlDataSource>
如果要在代码隐藏中进行绑定,可以通过RowDataBound事件执行此操作:
Protected Sub GridView1_RowDataBound(sender As Object,e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow AndAlso (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then Dim ddl As DropDownList = TryCast(e.Row.FindControl("ddl_GetLists"),DropDownList) If ddl IsNot Nothing Then ddl.DataSource = RetrieveDataSource() ddl.DataBind() End If End If End Sub