<p>//DataGridRadio.aspx<br></p>
<%@ Page language="c#" Codebehind="DataGridRadio.aspx.cs" AutoEventWireup="false" Inherits="Sample.DataGridRadio" %>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
<form id=Form1 method=post runat="server">
<input id=rd type=hidden name=rd runat="server"><asp:datagrid id=ItemsGrid runat="server" OnPageIndexChanged="Grid_Change" AutoGenerateColumns="false" AllowPaging="true" CellPadding="3" BorderWidth="1" BorderColor="black">
<headerstyle BackColor="#00aaaa">
</headerstyle>
<pagerstyle Mode="NumericPages"></pagerstyle>
<columns><asp:templatecolumn><itemtemplate><input type=radio value='<%# DataBinder.Eval(Container.DataItem, "IntegerValue")%>' name=RadioName> </itemtemplate></asp:templatecolumn><asp:boundcolumn DataField="IntegerValue" HeaderText="数字列"></asp:boundcolumn><asp:boundcolumn DataField="StringValue" HeaderText="字符串列"></asp:boundcolumn><asp:boundcolumn DataField="CurrencyValue" HeaderText="货币列" DataFormatString="{0:c}"><itemstyle HorizontalAlign="right"></itemstyle></asp:boundcolumn></columns></asp:datagrid><br><asp:button id=Btn onclick=btnClick runat="server" Text="看你选择的"></asp:button><asp:label id=Label1 runat="server" Text=""></asp:label>
</form>
// DataGridRadio.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Sample
{
/// <summary>
/// DataGridRadio 的摘要说明。
/// </summary>
public class DataGridRadio : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid ItemsGrid;
protected System.Web.UI.WebControls.Button Btn;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlInputHidden rd;
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for ( int i = 0 ; i < 101 ; i++ )
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * ( i + 1 );
dt.Rows.Add(dr);
}
DataView dv = new DataView( dt );
return dv;
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string js = "";
js += "<script>\r\n";
js += "function ld(){\r\n";
js += "for(i=0;i<document.getElementsByName('RadioName').length;i++)\r\n";
js += "if(document.getElementsByName('RadioName')[i].value==";
js += "document.getElementById('" + rd.ClientID + "').value) ";
js += "document.getElementsByName('RadioName')[i].checked=true\r\n";
js += "}\r\n"; js+="window.onload=ld\r\n";
js += "</"+"script>\r\n";
this.RegisterStartupScript( "js" , js );
if (!IsPostBack)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
}
public void Grid_Change(Object sender, DataGridPageChangedEventArgs e)
{
ItemsGrid.CurrentPageIndex = e.NewPageIndex;
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
}
public void btnClick(Object sender, EventArgs e)
{
if(Request.Form["RadioName"] != null)
{
rd.Value = Request.Form["RadioName"].ToString();
Label1.Text = "您所选择的是:<font color=red>" + Request.Form["RadioName"].ToString() +"</font>";
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}