GridView中添加一个Radio列

 1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_RadioColumn.aspx.cs" Inherits="GridSamples_GridView_RadioColumn" %>
 2
 3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4
 5<html xmlns="http://www.w3.org/1999/xhtml" >
 6<head runat="server">
 7    <title>无标题页</title>
 8    <script language="javascript" type="text/javascript">
 9    function ld()
10    {
11    
12     for(i=0;i<document.getElementsByName('RadioName').length;i++)
13       if(document.getElementsByName('RadioName')[i].value == document.getElementById('rd').value)
14            document.getElementsByName('RadioName')[i].checked=true;
15    }

16     
17    
</script>
18</head>
19<body onload="ld();">
20     <form id="form1" runat="server" >
21    <input type="hidden" id="rd" runat="server" name="rd" value="" />
22        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
23            DataKeyNames="id" DataSourceID="AccessDataSource1" AllowSorting="True">
24            <Columns>
25                <asp:TemplateField>
26                    <ItemTemplate>
27                       <input type="radio" name="RadioName" value='<%# DataBinder.Eval(Container.DataItem, "id")%>'/>
28                    </ItemTemplate>
29                </asp:TemplateField>
30                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
31                    SortExpression="id" />
32                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
33                <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
34                <asp:BoundField DataField="deptid" HeaderText="deptid" SortExpression="deptid" />
35            </Columns>
36        </asp:GridView>
37        &nbsp;
38      
39        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/test.mdb"
40            SelectCommand="SELECT [id], [name], [sex], [deptid] FROM [employees]"></asp:AccessDataSource>
41        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ShowSelectedItem" />
42        <asp:TextBox ID="TextBox1" runat="server" Width="200px" ReadOnly="True"></asp:TextBox>
43    </form>
44</body>
45</html>
46

 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class GridSamples_GridView_RadioColumn : System.Web.UI.Page
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16
17    }

18    protected void Button1_Click(object sender, EventArgs e)
19    {
20        if (Request.Form["RadioName"!= null)
21        {
22            rd.Value = Request.Form["RadioName"].ToString();
23            this.TextBox1.Text = "您所选择的是:" + Request.Form["RadioName"].ToString() ;
24        }

25    }

26}

27

posted on 2006-07-04 22:21  №.零零伍  阅读(1565)  评论(0编辑  收藏  举报