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('RadioLine')[i].value == document.getElementById('" + hiddenControls + "').value) {\r\n";
            js += "                document.getElementsByName('RadioLine')[i].checked = true;\r\n";
            js += "            }\r\n";
            js += "        }\r\n";
            js += "    }\r\n";
            js += "    window.onload = RadioSelectedRow;\r\n";
            js += "</script> \r\n"; 
            return js;
        } 
    }
}

 C#单选按钮选择行

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // 改变背景颜色
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "fn_RadioRowSingle(this,event)");
                e.Row.Attributes.Add("class", e.Row.RowIndex % 2 == 0 ? "rowOdd" : "rowEven");
                e.Row.Attributes.Add("oldClass", e.Row.RowIndex % 2 == 0 ? "rowOdd" : "rowEven");
                e.Row.Attributes.Add("onmouseover", "lastBackgroundColor=this.className;this.className='rowCurrent'");
                e.Row.Attributes.Add("onmouseout", "this.className=lastBackgroundColor;");
                // 绑定按钮
                int CellsCount = e.Row.Cells.Count;
                LinkButton BtnDownItem = e.Row.Cells[CellsCount - 2].Controls[0] as LinkButton;
                LinkButton BtnDelItem = e.Row.Cells[CellsCount - 1].Controls[0] as LinkButton;
                string RowCells = "role_id=" + e.Row.Cells[1].Text.Trim();
                BtnDownItem.Attributes.Add("onclick", "return fn_DownItem('" + RowCells + "');");
                BtnDelItem.Attributes.Add("onclick", "return fn_DelItem('" + RowCells + "');");
            }
        }

 Js中单选按钮选择

// 选中一行:单选按钮
function fn_RadioRowSingle(row, evt) {
    var obj = evt.target || event.srcElement;
    if (obj.tagName && obj.tagName != "INPUT") {
        row.cells[0].children[0].checked = true;
    }
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值