GridView中下拉菜单DropDownList从数据库绑定数据怎么弄? 20
如何在gridview中的dropdownlist绑定的数据默认为数据库中的分类表tb_class中的数据,怎么绑定,当触发编辑事件时,要修改分类时怎么修改?请各位帮助,...
如何在gridview中的dropdownlist 绑定的数据默认为数据库中的分类表tb_class中的数据,怎么绑定,当触发编辑事件时,要修改分类时怎么修改?请各位帮助,
展开
2个回答
展开全部
把数据放到DataSet里,再绑定DataSet里的表就可以了,触发编辑事件时,要修改分类的话直接连接数据库用update改就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
aspx页面:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<script type="text/javascript" language="javascript">
function Onchange()
{
alert("aa");
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server"
onitemdatabound="DataList1_ItemDataBound" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<table border="1">
<tr>
<td>
<%#Eval("OptionCaption") %>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
=================================
aspx。cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
using Custom.Query;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
dataQuery dq = new dataQuery();
DataList1.DataSource = dq.DbExecute();
DataList1.DataKeyField = "ID";
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
dataQuery dq = new dataQuery();
DataTable dt = dq.getOptionById(Convert.ToInt32(this.DataList1.DataKeys[e.Item.ItemIndex]));
DropDownList DropDownList1 = (DropDownList)e.Item.FindControl("DropDownList1");
Label l = (Label)e.Item.FindControl("Label1");
for (int i = 0; i < dt.Rows.Count;i++ )
{
DropDownList1.Items.Add(new ListItem( dt.Rows[i]["OptionalName"].ToString(),dt.Rows[i]["OptionalValue"].ToString()));
}
l.Text = DropDownList1.SelectedValue.ToString() ;
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<script type="text/javascript" language="javascript">
function Onchange()
{
alert("aa");
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server"
onitemdatabound="DataList1_ItemDataBound" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<table border="1">
<tr>
<td>
<%#Eval("OptionCaption") %>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
=================================
aspx。cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
using Custom.Query;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
dataQuery dq = new dataQuery();
DataList1.DataSource = dq.DbExecute();
DataList1.DataKeyField = "ID";
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
dataQuery dq = new dataQuery();
DataTable dt = dq.getOptionById(Convert.ToInt32(this.DataList1.DataKeys[e.Item.ItemIndex]));
DropDownList DropDownList1 = (DropDownList)e.Item.FindControl("DropDownList1");
Label l = (Label)e.Item.FindControl("Label1");
for (int i = 0; i < dt.Rows.Count;i++ )
{
DropDownList1.Items.Add(new ListItem( dt.Rows[i]["OptionalName"].ToString(),dt.Rows[i]["OptionalValue"].ToString()));
}
l.Text = DropDownList1.SelectedValue.ToString() ;
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询