软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP教程 > GridView中CheckBox实现跨页面多选

GridView中CheckBox实现跨页面多选

  烈火网(LieHuo.Net)教程 GridView中CheckBox实现跨页面多选,请看源码。

以下为引用的内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="KSSelectOpen.aspx.cs" Inherits="Admin_KSSelectOpen" %>
<%@ Register Src="UserCheck.ascx" TagName="UserCheck" TagPrefix="uc1" %>
<html>
<head id="id_head" runat="server" >
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <LINK href="./common/css.css" type="text/css" rel="stylesheet">
    <script>
         function AddRemoveValues(oChk)
         {
           //在处理这个地方需要注意的是:你保存的值应该具有唯一性,这样才能不会替换错误的项。          
          
           if(oChk.checked)
           {
               form1.txtId.value += "," + oChk.value;
           }
           else
           {
               form1.txtId.value = form1.txtId.value.replace("," + oChk.value,"");
           }
              
           var n=0;
           if (form1.txtCount.value != "")     
           {                   
               n = parseFloat(form1.txtCount.value); //转为Float类型
           }
           if(oChk.checked)            
           {
               n++;           
           }
           else          
           {
               n--;                       
           }
           if (n>0)
              form1.txtCount.value = n;              
           else
              form1.txtCount.value = "";
          }
    </script>
</head>
<body style="overflow:scroll">
    <form id="form1" runat="server">     
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>         
          <td width="240" height="24" class="borleft1 bortop1 borbottom MainTitle_bg_2 MainTitle_Font_1">
              <img src="./img/c20.gif" hspace="8"/>当前位置:题库试题列表
          </td>
          <td class="bor1 MainTitle_bg_1">&nbsp;
             ·关键字:                       
               <asp:TextBox ID="TextBox1" runat="server" widht="200"/>
               <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="查询" />
               <asp:Label ID="lblCount" runat="server"/></td>
        </tr>
   </table>   
      <table width="100%" height="450" border="0" cellspacing="0" cellpadding="0">      
        <tr>
          <td width="100%" valign="top">
             <table width="100%" border="0" cellspacing="0" cellpadding="0" class="bor2" >      
                 <tr>                    
                     <td class="First_bg_1 First_font5" height="24">                      
                         共选了:<INPUT id="txtCount" name="txtCount" style="width=30;" class="font_orange_2_XH MainTitle_bg_2" readonly="readonly" runat="server">个
                         <INPUT id="txtId" name="txtId" style="width=600;" readonly="readonly" class="font_blue_common_1 MainTitle_bg_2" runat="server">
                     </td>
                     <td class="First_bg_1 First_font5" align="right">
                         <ASP:BUTTON id="btnOK" runat="server" text=" 完 成 " OnClick="btnOK_Click"/>
                     </td>
                 </tr>   
                 <tr>
                      <td class ="font_orange_1" colspan="2" align="right"><asp:Label ID="lblMsg" runat="server" ForeColor="Red"/>提示:可以跨行多选,选完后点击完成返回.</td>
                 </tr>             
                 <tr>
                     <td width="99%" valign="top" colspan="2">
                         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" width="100%" border="0" cellspacing="1" cellpadding="2" class=" MainTitle_bg_3" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="60" DataKeyNames="ID" AllowSorting="True" OnSorting="GridView1_Sorting">                   
                               <Columns>                                    
                                  <asp:BoundField DataField="id" HeaderText="ID" SortExpression="ID">
                                      <HeaderStyle HorizontalAlign="Center"/>
                                      <ItemStyle HorizontalAlign="Center" Width="60px" />
                                  </asp:BoundField>  
                                  <asp:TemplateField HeaderText="试题内容"><!--代码来自烈火LiEhuo.NeT--->
                                     <HeaderStyle HorizontalAlign="Center" />                                    
                                     <ItemTemplate>                                       
                                         <asp:Literal Text='<%#GetClientVisitInfoShowAnchorStr(DataBinder.Eval(Container.DataItem, "id"))%>' runat="server" ID="TitleShow" />
                                     </ItemTemplate>
                                  </asp:TemplateField>      
                                  <asp:TemplateField>                                   
                                      <HeaderStyle HorizontalAlign="center"/>
                                      <ItemStyle Width="60" HorizontalAlign="Center"/>
                                      <HeaderTemplate>
                                          请选择
                                      </HeaderTemplate>
                                      <ItemTemplate>                                         
                                          <input type="checkbox" id="cbSelect" name="cbSelect" value='<%#Eval("ID")%>' runat="server" onclick="AddRemoveValues(this)"/>
                                      </ItemTemplate>
                                  </asp:TemplateField>                                
                              </Columns>  
                              <PagerSettings Mode="NumericFirstLast" />
                              <PagerStyle Height="17px" HorizontalAlign="Right" Font-Size="12pt" />                    
                          </asp:GridView>                            
                     </td>
                 </tr>                      
              </table>              
          </td>                     
        </tr>         
      </table>
      <uc1:UserCheck ID="UserCheck1" runat="server" />
    </form>  
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_KSSelectOpen : System.Web.UI.Page
{
    string _sBookID, _TypeID;
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.MaintainScrollPositionOnPostBack = true;
       
        _sBookID = Request.QueryString["BookID"];
        _TypeID = Request.QueryString["TypeID"];               
       
        if (!IsPostBack)
        {       
            ViewState["SortOrder"] = "id";
            ViewState["OrderDire"] = "asc";
            GridBind();
            lblMsg.Text = "";
            //lblMsg.Text = "<script>window.opener.document.Form1.all.txtAccepterId.value = window.document.Form1.all.txtId.value ;window.close();</script>";
        }
    }
    protected void GridBind()
    {
        DataSet ds = classFun.Fun_QusetionsList_Select(_sBookID, _TypeID, TextBox1.Text);
        DataView view = ds.Tables[0].DefaultView;

        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        view.Sort = sort;

        GridView1.DataSource = view;
        GridView1.DataBind();
        lblCount.Text = "共有 " + ds.Tables[0].Rows.Count.ToString() + " 条记录.";

        ds.Dispose();
        view.Dispose();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExp = e.SortExpression.ToString();
        if (ViewState["SortOrder"].ToString() == sortExp)
        {
            if ((string)ViewState["OrderDire"] == "DESC")
            {
                ViewState["OrderDire"] = "ASC";
            }
            else
            {
                ViewState["OrderDire"] = "DESC";
            }
        }
        else
        {
            ViewState["SortOrder"] = e.SortExpression;
        }
        GridView1.PageIndex = 0;
        GridBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) //普通行 liehuo.net
        {
            ClassFunCommon.Fun_GVLineChangeColor(e, "#DEEAFC"); //鼠标颜色
            //if (txtId.Value.IndexOf(((Literal)e.Row.Cells[1].FindControl("TitleShow")).Text) >= 0)
            //{
            //    HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Row.Cells[2].FindControl("cbSelect"));
            //    ChkSelected.Checked = true;
            //}           
            string sBookID = txtId.Attributes["value"];
            if (sBookID != "" || sBookID != null)
            {
                sBookID = sBookID.Substring(1, sBookID.Length - 1);
                string[] strtemp = sBookID.Split(',');
                foreach (string str in strtemp)
                {
                    HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Row.Cells[2].FindControl("cbSelect"));
                    if (str == ChkSelected.Value)
                    {
                        ChkSelected.Checked = true;
                    }
                }
            }           
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header) //标题       
            e.Row.Attributes.Add("style", "background:#F3F9FF;color:#336799");
        else if (e.Row.RowType == DataControlRowType.Pager) //分页       
            e.Row.Attributes.Add("style", "background:#F3F9FF;color:#336799");
        else if (e.Row.RowType == DataControlRowType.DataRow) //普通行
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowState == DataControlRowState.Alternate) //交替行       
                e.Row.Attributes.Add("style", "background:#F7F9FD");
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridBind();
    }
    public string GetClientVisitInfoShowAnchorStr(object ID)
    {
        string strUrl = "QuestionsDetails.aspx?ID=" + ID + "";
        string AnchorStr = "<a href=" + strUrl + " target=_blank>" + HttpUtility.HtmlDecode(Eval("Questions").ToString()) + "</a>";
        return AnchorStr;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridBind();
    }   
    protected void ddlBook_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridBind();
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridBind();
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        //为隐藏控件txtID赋值 liehuo.net
        string sBookID = txtId.Attributes["value"];
        if (sBookID != "" || sBookID != null)
        {
            try
            {
                txtId.Attributes["value"] = sBookID.Substring(1, sBookID.Length - 1);
            }
            catch
            {
            }
        }
        //txtId.Attributes["value"] = GetBookID();
        //下面部分给打开本窗口的窗口中控件赋值
        lblMsg.Text = "<script>";
        lblMsg.Text += "window.opener.document.form1.all." + Request["getUser"] + "txtAccepterId" + _TypeID + ".value = window.document.form1.all.txtId.value ;";
        lblMsg.Text += "window.close();";
        lblMsg.Text += "</script>";
    }
}

    相关阅读
    网友评论