为了账号安全,请及时绑定邮箱和手机立即绑定

GridView实现单选(Single Selection)

标签:
架构

一直很想写一个Gridview内实现单选功能,但是一直没有太多时间,近段时间多个网友问及此功能,只好抽出些少时间来实现,下面为实现过程。

效果如下,把选择的值使用javascript Alert:

数据源,在可用的数据库内创建一个表[Member] 并插入三笔测试记录:

View Code SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID ('[dbo].[Member]') IS NOT NULL
DROP TABLE [dbo].[Member]

GO

CREATE TABLE [dbo].[Member](
    [MemberId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL,
    [Email] [nvarchar](100) NULL,
 CONSTRAINT [PK__Member__0CF04B187F60ED59] PRIMARY KEY CLUSTERED 
(
    [MemberId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
INSERT INTO [dbo].[Member] ([Name],[Email]) VALUES ('John','john@it.com')
INSERT INTO [dbo].[Member] ([Name],[Email]) VALUES ('Jeny','Jeny@it.com')
INSERT INTO [dbo].[Member] ([Name],[Email]) VALUES ('Mike','Mike@it.com')

 

存过程:

View Code SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID ('[dbo].[usp_GetMember]') IS NOT NULL
DROP PROCEDURE [dbo].[usp_GetMember]
GO

CREATE PROCEDURE [dbo].[usp_GetMember]
AS
SELECT [MemberId],[Name],[Email] FROM [dbo].[Member]
GO

 

Member.cs 类别:

View Code sing System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Member
/// </summary>
namespace Insus.NET
{
    public class Member
    {
        BusinessBase objBusinessBase = new BusinessBase();

        public Member()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public DataTable GetMember()
        {
            return objBusinessBase.GetDataToDataSet("usp_GetMember").Tables[0];
        }
    }
}

 

GridviewSingleSelection.aspx:

View Code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewSingleSelection.aspx.cs"
    Inherits="GridviewSingleSelection" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div>
                <asp:Button ID="ButtonSingleSelect" runat="server" Text="GetGridViewRasioSelectedValue" OnClick="ButtonSingleSelect_Click" />
                <p />
                <!--Insus.NET:GridView有一个事件OnRowDataBound -->
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="选择">
                            <HeaderStyle BorderWidth="1" BorderColor="#c0c0c0" />
                            <ItemStyle BorderWidth="1" BorderColor="#c0c0c0" />
                            <ItemTemplate>
                               <!--Insus.NET:这里是重点部分,使用Literal控件装载HtmlInputRadioButton -->
                                <asp:Literal ID="Literal1" runat="server"></asp:Literal>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="ID">
                            <HeaderStyle BorderWidth="1" BorderColor="#c0c0c0" />
                            <ItemStyle BorderWidth="1" BorderColor="#c0c0c0" />
                            <ItemTemplate>
                                <%# Eval("MemberId") %>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                            <HeaderStyle BorderWidth="1" BorderColor="#c0c0c0" />
                            <ItemStyle BorderWidth="1" BorderColor="#c0c0c0" />
                            <ItemTemplate>
                                <%# Eval("Name") %>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Email">
                            <HeaderStyle BorderWidth="1" BorderColor="#c0c0c0" />
                            <ItemStyle BorderWidth="1" BorderColor="#c0c0c0" />
                            <ItemTemplate>
                                <%# Eval("Email") %>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

 

GridviewSingleSelection.aspx.cs:

View Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;  //Insus.NET:要引用名称空间

public partial class GridviewSingleSelection : System.Web.UI.Page
{
    //Insus.NET: new Member一个实例
    Member objMember = new Member();

    protected void Page_Load(object sender, EventArgs e)
    {
        Data_Binding();
    }

    private void Data_Binding()
    {
        this.GridView1.DataSource = objMember.GetMember();
        this.GridView1.DataBind();

    }
    protected void ButtonSingleSelect_Click(object sender, EventArgs e)
    {
       //Insus.NET: 判断用户是否有选择,如果没有选择return
        if (Request.Form["InsusRadio1"] == null) return;

        //Insus.NET: 把选择的值存到ViewState,你可以把它存任何可以保留的变量中,呵呵
        ViewState["SelectedValue"]= Request.Form["InsusRadio1"].ToString();    
        //Insus.NET: 下面类别,你可以从Insus.NET博客中下载
        InsusJsUtility objJs = new InsusJsUtility();

        objJs.JsAlert(ViewState["SelectedValue"].ToString());
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           //Insus.NET: 判断Literal控件是否存在,如果没有写此判断,程式有可能在找不到时抛出异常
            if (e.Row.FindControl("Literal1") != null)
            {
                //Insus.NET:找到了,把对象转为控件。
                Literal literal = (Literal)e.Row.FindControl("Literal1");

                //Insus.NET: 下面这个是把表主键的值赋给变量value。如果你想取GridView的索引,可以使用e.Row.RowIndex.ToString()
                string value = DataBinder.Eval(e.Row.DataItem, "MemberId").ToString(); //e.Row.RowIndex.ToString();

                //Insus.NET: 把HtmlInputRadioButton绑定至Literal控件上。
                literal.Text = "<input id=\"InsusRadio1\" type=\"radio\" name=\"InsusRadio1\" value=\"" + value + "\" />";
            }
        }
    }
}

 

好了,时间问题不再作过多讲解,如还有问题,请联系Insus.NET,程序重点部分有详细的注解。

 

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消