asp.net – 如何在Gridview中绑定DropDownList,而不是从gridview绑定数据
发布时间:2020-05-24 13:21:57 所属栏目:asp.Net 来源:互联网
导读:获得答案的一半是知道如何提出问题.我不确定我做得很好,但这是我最好的一击. 我正在尝试将ddl与gridview中的数据绑定,而不是来自gridview本身.这是在EditItemTemplate中.这样做的目的是让用户从查找存储过程开始选择值和一系列其他值. 我在这里提到我之前已成
|
获得答案的一半是知道如何提出问题.我不确定我做得很好,但这是我最好的一击. 我正在尝试将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 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – AspNet Identity 2.0电子邮件和用户名重复
- asp.net – 全局ASAX中的错误:文件不存在
- asp.net – 无法加载类型’site._Default[已关闭]
- asp.net-mvc – 链接到iPad上的Google Document Viewer上的
- 在IIS托管的asp.net Web应用程序中打开页面时“无法找到资源
- asp.net-mvc – MVC DB首先修复显示名称
- asp.net-mvc – 测量时间调用ASP.NET MVC控制器操作
- 取消选中时,ASP.NET CheckBox不会启动CheckedChanged事件
- asp.net – 基于用户更改主题/ CSS
- asp.net – 生成第二个标题标签的母版页
推荐文章
站长推荐
热点阅读
