读书人

C#中GRIDVIEW单选按钮施用

发布时间: 2012-09-01 09:33:02 作者: rapoo

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" CssForeColor="#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;    }}
?

?

读书人网 >C#

热点推荐