-
项模板直接拉一个RadioButton,然后写JS代码,如下:<script language="javascript">
function check(radio) {
var radioButtons = document.getElementsByTagName("input");
for (var i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].type == "radio") {
if (radioButtons[i] == radio) {
continue;
}
else {
radioButtons[i].checked = false;
}
}
}
}
</script>源文件:
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" οnclick="check(this)" runat="server" />
</ItemTemplate>
</asp:DataList>
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListNesting.aspx.cs" Inherits="DataListNesting" %>
- <!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>DataListNesting</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Eval("OrderID") %>'></asp:Label>
- <asp:Label ID="Label2" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label>
- <asp:DataList ID="DataList2" runat="server">
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
- <asp:Label ID="Label2" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>
- <asp:Label ID="Label3" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
- </ItemTemplate>
- </asp:DataList>
- </ItemTemplate>
- </asp:DataList>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- public partial class DataListNesting : System.Web.UI.Page
- {
- private void BindList()
- {
- SqlConnection cn = new SqlConnection(@"server=.\sqlexpress;uid=sa;pwd=;database=northwind");
- SqlDataAdapter da = new SqlDataAdapter("select OrderID, CustomerID from Orders", cn);
- DataSet ds = new DataSet();
- cn.Open();
- da.Fill(ds);
- cn.Close();
- DataList1.DataSource = ds.Tables[0].DefaultView;
- DataList1.DataKeyField = "orderID";
- DataList1.DataBind();
- }
- private void Page_Load(object sender, System.EventArgs e)
- {
- if (!IsPostBack)
- {
- BindList();
- }
- }
- protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
- {
- DataList datalist2;
- if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
- {
- datalist2 = e.Item.FindControl("DataList2") as DataList;
- if (datalist2 != null)
- {
- SqlConnection cn = new SqlConnection(@"server=.\sqlexpress;uid=sa;pwd=;database=northwind;");
- SqlDataAdapter da = new SqlDataAdapter("select ProductID, UnitPrice, Quantity from [Order Details] where orderID = @orderID", cn);
- da.SelectCommand.Parameters.AddWithValue("@orderID", (e.Item.DataItem as DataRowView)["orderID"]);
- DataSet ds = new DataSet();
- cn.Open();
- da.Fill(ds);
- cn.Close();
- datalist2.DataSource = ds.Tables[0].DefaultView;
- datalist2.DataBind();
- }
- }
- }
- }