日期:2014-05-18  浏览次数:21216 次

C#中GRIDVIEW单选按钮使用

page代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" GridLines="Horizontal" Width="100%" 
        onrowdatabound="GridView1_RowDataBound" 
        onrowcommand="GridView1_RowCommand" CssClass="text">
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <Columns>
            <asp:TemplateField HeaderText="选择">
                <ItemTemplate>
                    <input ID="RadioLine" name="RadioLine" type="radio" value="<%#Eval("accessory_id")%>" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:BoundField DataField="accessory_id" HeaderText="附件ID" />
            <asp:BoundField DataField="order_header_id" HeaderText="订单ID" />
            <asp:BoundField DataField="accessory_name" HeaderText="附件名称" />
            <asp:BoundField DataField="accessory_description" HeaderText="附件说明" />
            <asp:BoundField DataField="creation_date" DataFormatString="{0:d}" HeaderText="创建日期" />
            <asp:BoundField DataField="created_by" HeaderText="创建人" />
            <asp:BoundField DataField="accessory_size" HeaderText="文件大小" />
            <asp:BoundField DataField="accessory_type" HeaderText="文件类型" />
            <asp:ButtonField CommandName="DownItem" HeaderText="下载" ShowHeader="True" Text="下载" >
            <ItemStyle HorizontalAlign="Center" />
            </asp:ButtonField>
            <asp:ButtonField CommandName="DelItem" HeaderText="删除" ShowHeader="True" Text="删除" >
            <ItemStyle HorizontalAlign="Center" />
            </asp:ButtonField>
        </Columns>
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
    </asp:GridView>

?C#中单选按钮

protected void Page_Load(object sender, EventArgs e)
        {
            // 单选按钮
            if (!this.IsStartupScriptRegistered("Startup"))
            {
                this.RegisterStartupScript("Startup", RegisterScript.RadioRowSelected(hidden_id.ClientID));
            }
            if(!Page.IsPostBack)
            { 
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.Append("select oa.accessory_id, oa.order_header_id, oa.accessory_name,oa.accessory_description, ");
                sqlBuilder.Append("oa.creation_date, oa.created_by, oa.accessory_size, oa.accessory_type ");
                sqlBuilder.Append("from oe_accessories oa ");
                sqlBuilder.Append("where 1 = 1 ");
                DataSet ds = db.returnds(sqlBuilder.ToString());
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();
            }
        }

?C#单选按钮js注册

using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;

namespace order.Components
{
    public class RegisterScript
    {
        public static string RadioRowSelected(string hiddenControls)
        {
            string js = "";
            js += "<script language='javascript' type='text/javascript'>\r\n";
            js += "    function RadioSelectedRow() {\r\n";
            js += "        for (i = 0; i < document.getElementsByName('RadioLine').length; i++) {\r\n";
            js += "            if (document.getElementsByName('RadioLin