一分心灵的宁静

在滚滚红尘,繁杂人世里,能够保持一分心灵的宁静,随时回到自己的内心深处,细细品味生命的奥妙,无疑是一种修身养性的人生境界

导航

DataGrid加单选框

Posted on 2006-08-23 10:20  有缘无份  阅读(216)  评论(0编辑  收藏  举报
  1<%@ Page language="c#" Codebehind="DataGrid加单选框.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_8_16.Samples.DataGrid加单选框" %>
  2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
  3<HTML>
  4    <body>
  5        <form runat="server" id="MM">
  6            <input type="hidden" id="rd" runat="server" NAME="rd">
  7            <asp:DataGrid id="ItemsGrid" runat="server" BorderColor="black" BorderWidth="1" CellPadding="3"
  8                AllowPaging="true" AutoGenerateColumns="false">
  9                <HeaderStyle BackColor="#00aaaa"></HeaderStyle>
 10                <PagerStyle Mode="NumericPages"></PagerStyle>
 11                <Columns>
 12                    <asp:TemplateColumn>
 13                        <ItemTemplate>
 14                            <input type=radio name="RadioName" value='<%# DataBinder.Eval(Container.DataItem, "IntegerValue")%>'/>
 15                        </ItemTemplate>
 16                    </asp:TemplateColumn>
 17                    <asp:BoundColumn HeaderText="数字列" DataField="IntegerValue" />
 18                    <asp:BoundColumn HeaderText="字符串列" DataField="StringValue" />
 19                    <asp:BoundColumn HeaderText="货币列" DataField="CurrencyValue" DataFormatString="{0:c}">
 20                        <ItemStyle HorizontalAlign="right"></ItemStyle>
 21                    </asp:BoundColumn>
 22                </Columns>
 23            </asp:DataGrid>
 24            <br>
 25            <asp:Button id="Btn" Text="看你选择的" runat="server" />
 26            <asp:Label id="Label1" Text="" runat="server" />
 27        </form>
 28    </body>
 29</HTML>
 30*********************************************8
 31using System;
 32using System.Collections;
 33using System.ComponentModel;
 34using System.Data;
 35using System.Drawing;
 36using System.Web;
 37using System.Web.SessionState;
 38using System.Web.UI;
 39using System.Web.UI.WebControls;
 40using System.Web.UI.HtmlControls;
 41
 42
 43namespace bsTest2005_8_16.Samples
 44{
 45    /// <summary>
 46    /// DataGrid加单选框 的摘要说明。
 47    /// </summary>

 48    public class DataGrid加单选框 : System.Web.UI.Page
 49    {
 50        protected System.Web.UI.WebControls.DataGrid ItemsGrid;
 51        protected System.Web.UI.WebControls.Button Btn;
 52        protected System.Web.UI.WebControls.Label Label1;
 53        protected System.Web.UI.HtmlControls.HtmlInputHidden rd;
 54
 55        
 56
 57        private void Page_Load(object sender, System.EventArgs e)
 58        {
 59            // 在此处放置用户代码以初始化页面
 60            System.Text.StringBuilder js = new System.Text.StringBuilder();
 61            js.Append("<script>\r\n");
 62            js.Append("function ld(){\r\n");
 63            js.Append("for(i=0;i<document.getElementsByName('RadioName').length;i++)\r\n");
 64            js.Append("if(document.getElementsByName('RadioName')[i].value==");
 65            js.Append("document.getElementById('" + rd.ClientID + "').value) ");
 66            js.Append("document.getElementsByName('RadioName')[i].checked=true\r\n");
 67            js.Append("}\r\n");
 68            js.Append("window.onload=ld\r\n");
 69            js.Append("</"+"script>\r\n");
 70            this.RegisterStartupScript("js",js.ToString());
 71            if (!IsPostBack) 
 72            {
 73                ItemsGrid.DataSource = CreateDataSource();
 74                ItemsGrid.DataBind();
 75            }
    
 76        }

 77        
 78        private DataView CreateDataSource() 
 79        {
 80            DataTable dt = new DataTable();
 81            DataRow dr;
 82        
 83            dt.Columns.Add(new DataColumn("IntegerValue"typeof(Int32)));
 84            dt.Columns.Add(new DataColumn("StringValue"typeof(string)));
 85            dt.Columns.Add(new DataColumn("CurrencyValue"typeof(double)));
 86        
 87            for (int i = 0; i < 101; i++
 88            {
 89                dr = dt.NewRow();        
 90                dr[0= i;
 91                dr[1= "Item " + i.ToString();
 92                dr[2= 1.23 * (i+1); 
 93                dt.Rows.Add(dr);
 94            }

 95        
 96            DataView dv = new DataView(dt);
 97            return dv;
 98        }

 99 
100        private void Btn_Click(object sender, System.EventArgs e)
101        {
102            if(Request.Form["RadioName"!= null)
103            {
104                rd.Value = Request.Form["RadioName"].ToString();
105                Label1.Text = "您所选择的是:<font color=red>" + Request.Form["RadioName"].ToString() +"</font>";
106            }

107        }

108
109        private void ItemsGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
110        {
111            ItemsGrid.CurrentPageIndex = e.NewPageIndex;
112            ItemsGrid.DataSource = CreateDataSource();
113            ItemsGrid.DataBind();
114        }

115
116        Web 窗体设计器生成的代码
138
139       
140    }

141}

142
143