`

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;
    }
}
 

 

分享到:
评论

相关推荐

    GridView 单选和全选功能以及全选操作和取消全选

    在很多场景下,我们可能需要为GridView添加单选、多选以及全选和取消全选的功能,比如在图片选择器或者列表设置中。下面我们将详细探讨如何实现这些功能。 1. **GridView的基本使用** GridView继承自AbsListView,...

    Devexpress 添加单选框

    确保每个RadioButton的GroupName属性相同,这样它们才能形成一组单选按钮。 ```xml 选择"&gt; ``` 2. **事件处理**:为GridView的`SelectedIndexChanged`事件添加处理程序,当用户选择一行时,取消其他行...

    简单的GridView单选按钮行选择器

    "简单的GridView单选按钮行选择器"是一个自定义Web控件,它的目标是在GridView每一行中集成单选按钮,允许用户仅能选择一行,而不是多选。这个控件通常适用于那些只需要单个数据项被选中的场景,如用户投票或数据的...

    在GridView中实现单选或多选功能

    这篇博客“在GridView中实现单选或多选功能”主要讲解了如何在GridView中添加单选和多选功能,这对于提升用户体验和增加应用交互性至关重要。 1. GridView基础: GridView继承自AbsListView,通过Adapter来填充...

    asp.net 中的Gridview控件的使用

    4. 文件“c#GridView72招”可能涵盖的高级技巧: - 72招可能包括72种不同的方法或技巧,比如自定义按钮事件处理、AJAX无刷新操作、使用AjaxToolKit扩展GridView功能、数据验证、异步更新等。 - 每一招可能是一个...

    ASP.NET中GridView控件的用法实例

    ASP.NET中的GridView控件是Web开发中非常常用的数据展示组件,尤其在数据绑定和操作方面具有强大的功能。这篇教程将详细介绍GridView控件的基本用法,包括编辑、删除和选择等功能。 首先,GridView控件用于显示从...

    Asp.net下Gridview的72种用法

    在ASP.NET web应用开发中,GridView控件是一个非常重要的组件,它用于显示数据源中的数据,如数据库表、XML文件或任何可以绑定的数据集。在本文档“Asp.net下Gridview的72种用法”中,我们将深入探讨如何充分利用这...

    C_精髓_GridView_72般绝技

    在“C#精髓_GridView_72般绝技”这个主题中,我们将深入探讨GridView控件的各种高级技巧和实用功能,这些技巧涵盖了72个不同的方面,帮助开发者充分利用这个强大的工具。 1. GridView的基础使用:首先,你需要了解...

    GridView选中编辑取消删除数据项

    在.NET框架中,GridView控件是ASP.NET网页开发中常用的一种数据展示组件,它能够方便地展示数据库或其他数据源中的信息,并提供多种交互功能,如排序、分页、选择、编辑和删除数据项。本教程将围绕“GridView选中...

    Developer Express .NET v7.2 - ASPxGridView Demo

    ASPxGridView Demo是Developer Express提供的一系列示例代码和资源,旨在帮助开发者快速理解和掌握ASPxGridView的使用方法。这个Demo包含了VB.NET和C#两种语言的源代码,这使得无论你偏好哪种.NET语言,都能找到相应...

    C# Winform DatagridView 分页及 全选/ 取消全选 功能

    在C# Winform开发中,`DataGridView`是一个非常重要的控件,用于展示表格数据。它提供了丰富的功能,包括数据编辑、排序、筛选等。在这个场景中,我们将关注两个特定的功能:分页和全选/取消全选。这些功能在处理...

    C#web控件使用方法(全部)

    常见的Web服务器控件包括按钮(Button)、文本框(TextBox)、复选框(CheckBox)、单选按钮(RadioButton)、下拉列表(DropDownList)等。这些控件在HTML中以特定的标签形式存在,但其行为和属性由C#代码控制。 第10章 "Web...

    c#控件介绍

    6. RadioButton:单选按钮用于提供互斥的选项,同一组内的单选按钮只能选择一个。 7. ListBox和ComboBox:两者都是列表控件,ListBox显示不折叠的列表,ComboBox则提供折叠式下拉列表。 8. PictureBox:用于显示图片...

    c#网上调查(Sql2005)

    开发者会创建各种表单控件(如文本框、单选按钮、多选按钮和下拉列表)来模拟调查问题,并利用事件驱动模型处理用户交互。 2. **数据库交互**:使用ADO.NET(数据库访问对象)库与SQL Server 2005进行连接,执行SQL...

    radio的应用ver_radio_datagrid

    在IT行业中,"radio"通常指的是单选按钮(Radio Button),它是用户界面中常见的控件类型,用于在多个选项中让用户选择一个。"Ver_radio_datagrid"可能是一个特定的项目或者功能版本,它结合了“radio”和数据网格...

    C#命名规则

    在 C# 中,变量的命名规则是使用 camelCase 风格,即第一个单词的首字母小写,后续单词的首字母大写。例如:`array`、`shoppingList`。 同时,C# 也提供了一些常用的前缀来表示变量的数据类型,例如: * `arr`:...

    ASP.net很好的代码 控件 C#

    在ASP.NET中,C#代码通常写在后台代码文件(.cs文件)中,与HTML标记结合使用,实现页面逻辑。 在ASP.NET中,控件与C#代码的交互主要通过事件驱动模型。当用户在网页上执行操作,如点击按钮,对应的控件会引发一个...

    ASP.NET程序开发范例宝典(C#)

    介绍如何使用单选按钮组实现单项选择功能。 - **示例081**:多选框组的选择项统计。通过示例展示如何统计多选框组中被选中的项。 - **5.6 下拉菜单控件响应** - **示例082**:下拉菜单选择。介绍如何使用下拉菜单...

Global site tag (gtag.js) - Google Analytics