获得答案的一半是知道如何提出问题。我不确定我做得很好,但这是我最好的拍摄。
我正在尝试将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也不被识别?在网格内部,它从设计器中消失,但在网格外部再次出现。
谢谢大家的帮助。
答案 0 :(得分:11)
你有几个选择。您可以使用数据源控件,也可以在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
答案 1 :(得分:1)
您可以简单地创建所需类型的全局列表,并将其设置为null 如果您正在使用linq,那么只需将该源放在DropDownListObject.DataSource
中DropDownListObject.DataSource=ObjListSource;
DropDownListObject.DataBind;
希望它会有所帮助。
答案 2 :(得分:0)
我看到了几个问题。
Protected Sub usersGrid_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
usersGrid.EditIndex = e.NewEditIndex
BindData()
End Sub
你为什么要这样做?为什么要在已经填充的网格上的事件上再次绑定网格?
答案 3 :(得分:0)
你解决了这个问题:
在代码页中,您定义了一个GetListData()公共函数(我想您已经这样做了)。
使用DataSource属性(如果要调用函数):
<asp:DropDownList ID="ddl_GetLists"
DataSource='<%# GetListData() %>'
AppendDataBoundItems="true"
DataValueField="listID"
DataTextField="listName"
SelectedValue='<%#Bind("listID") %>'
runat="server" >
</asp:DropDownList>