繁体   English   中英

在分页期间保留复选框状态

Retain checkbox state during paging

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我有一个gridview,其中包含复选框和分页启用。 现在当我检查任何复选框然后更改页面时,那时所有选中的复选框都会松开它们的状态。 我想在分页期间保留复选框的状态。 我怎样才能做到这一点?

//这是.aspx页面的代码

<%@ Page Title="Search candidates based on vacancy" Language="C#" MasterPageFile="~/HR Department/hrmasterpage.master"
AutoEventWireup="true" CodeFile="searcAppForVac.aspx.cs"    Inherits="HR_Department_searcAppForVac" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table width="100%">
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td align="center" class="tdtitle">
            Search Candidates
        </td>
    </tr>
    <tr>
        <td>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <table width="100%">
                        <tr>
                            <td class="RowHeight" width="20%">
                                Select Company
                            </td>
                            <td width="30%">
                                <asp:DropDownList ID="companyList" runat="server" AppendDataBoundItems="true" AutoPostBack="True"
                                    OnSelectedIndexChanged="companyList_SelectedIndexChanged" Width="150px">
                                    <asp:ListItem Text="-Select Company-" Value="-1"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                            <td width="20%">
                                Select Department
                            </td>
                            <td width="30%">
                                <asp:DropDownList ID="deptList" runat="server" AppendDataBoundItems="true" AutoPostBack="True"
                                    onclick="Validate();" OnSelectedIndexChanged="deptList_SelectedIndexChanged"
                                    Width="150px">
                                    <asp:ListItem Value="-1">-Select Department-</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td class="RowHeight" width="20%">
                                Select Vacancy
                            </td>
                            <td colspan="3" width="*">
                                <asp:DropDownList ID="vacanyList" runat="server" AppendDataBoundItems="true" 
                                    Width="200px" AutoPostBack="True" 
                                    onselectedindexchanged="vacanyList_SelectedIndexChanged">
                                    <asp:ListItem Value="-1">-Select Vacancy-</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4" align="center">
                                &nbsp;
                                <asp:Label ID="notifyLbl" runat="server" Font-Size="Large" ForeColor="Red" 
                                    Text="Label"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                <asp:Label ID="titleLbl" runat="server" Font-Size="Large" ForeColor="Red" 
                                    Text="Label"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                &nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                <asp:GridView ID="appForVacGrid" runat="server" AutoGenerateColumns="False" 
                                CellPadding="4" AllowPaging="True" 
                                    onpageindexchanging="appForVacGrid_PageIndexChanging" GridLines="None" 
                                    PageSize="3" CssClass="mGrid">
                                    <RowStyle BackColor="#EFF3FB" />
                                    <Columns>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                App.ID
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="appIdLbl" runat="server" Text='<%# Eval("AppId") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                First Name
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="firstNameLbl" runat="server" Text='<%# Eval("AppFirstName") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Last Name
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="lastNameLbl" runat="server" Text='<%# Eval("AppLastName") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Qualification
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="qualiNameLbl" runat="server" Text='<%# Eval("QualiName") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                &nbsp;Experience
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("TotalExpYear") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                EmailId
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="emailLbl" runat="server" Text='<%# Eval("AppEmailId1") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Send Mail
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="sendMailBox" runat="server" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                    <FooterStyle Font-Bold="True" ForeColor="White" 
                                        HorizontalAlign="Right" />
                                    <PagerStyle ForeColor="White" HorizontalAlign="Center" 
                                        VerticalAlign="Top" CssClass="pgr" />
                                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                    <HeaderStyle Font-Bold="True" ForeColor="White" 
                                        Font-Size="Medium" HorizontalAlign="Left" />
                                    <AlternatingRowStyle CssClass="alt" />
                                </asp:GridView>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4" align="center">
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="4">
                                <asp:Button ID="sendMailBtn" runat="server" Height="40px" Text="SEND MAIL" 
                                    Width="100px" onclick="sendMailBtn_Click" />
                            </td>
                        </tr>
                        <tr>
                            <%--<td>
                                &nbsp;
                            </td>--%>
                        </tr>
                    </table>
                </ContentTemplate>
                <Triggers>
                <asp:AsyncPostBackTrigger ControlID="companyList" EventName="SelectedIndexChanged" />
                <asp:AsyncPostBackTrigger ControlID="deptList" EventName="SelectedIndexChanged" />
                <asp:AsyncPostBackTrigger ControlID="appForVacGrid" EventName="pageindexchanged" />
                </Triggers>
              </asp:UpdatePanel>


        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
</table>

<script type="text/javascript">

    function alertOnBadSelection() {
        var select = document.getElementById('companyList');
        if (select.options[select.selectedIndex].value == "-Select Company-") {
            alert('Please Select Company!');
            return false;
        }
    }

</script>

// .aspx.cs页面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class HR_Department_searcAppForVac : System.Web.UI.Page
{
DataOperation oDo = new DataOperation();
AppVacancyDetailClass objAppVacDetail = new AppVacancyDetailClass();
protected void Page_Load(object sender, EventArgs e)
{
    notifyLbl.Visible = false;
    titleLbl.Visible = false;
    sendMailBtn.Visible = false;
    //mailSentLbl.Visible = false;
    try
    {
        if (!IsPostBack)
        {
            //Disable department dropdown list and vacancy dropdown list till company is not selected.
            deptList.Enabled = false;
            vacanyList.Enabled = false;
            //Fill Company dropdown list.
            DataTable objCmpnyTable = oDo.DropDownList("select * from tblCompanyMaster");
            if (objCmpnyTable.Rows.Count > 0)
            {
                companyList.DataSource = objCmpnyTable;
                companyList.DataValueField = "CompId";
                companyList.DataTextField = "CompName";
                companyList.DataBind();
            }
            else
            {
                notifyLbl.Visible = true;
                notifyLbl.Text = "There is not any company in the list.";
            }
        }
        else
        {
            if (companyList.SelectedIndex <= 0)
            {
                //Disable department dropdown list and vacancy dropdown list till company is not selected.
                deptList.Enabled = false;
                vacanyList.Enabled = false;
            }
        }
     }
     catch (Exception)
     {
          throw;
     }
 }
protected void companyList_SelectedIndexChanged(object sender, EventArgs e)
{
    //Disable vacancy dropdown list till depratment list is not selected
    vacanyList.Enabled = false;
    appForVacGrid.DataSource = null;
    appForVacGrid.DataBind();
    try
    {
        if (companyList.SelectedIndex > 0)
        {
            deptList.Enabled = true;
            deptList.Items.Clear();
            string str = "select * from vwCompWiseList where CompId=" + companyList.SelectedValue;
            DataTable objDeptTable = oDo.DropDownList("select DeptId,DeptName from vwCompWiseDept where CompId= "+companyList.SelectedValue);
            if (objDeptTable.Rows.Count > 0)
            {
                deptList.DataSource = objDeptTable;
                deptList.DataTextField = "DeptName";
                deptList.DataValueField = "deptId";
                deptList.DataBind();
                deptList.Items.Insert(0, new ListItem("--Select Department--", "-1"));
            }
            else 
            {
                deptList.Items.Insert(0, new ListItem("--No Departments--", "-1"));
                notifyLbl.Visible = true;
                notifyLbl.Text = "No Departments Available in " + companyList.SelectedItem.Text;
            }
        }
        else
        {
            notifyLbl.Visible = true;
            notifyLbl.Text = "Select Company....";
            appForVacGrid.DataSource = null;
            appForVacGrid.DataBind();
        }
    }
    catch (Exception)
    {

        throw;
    }    
}
protected void deptList_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        if (deptList.SelectedIndex > 0)
        {
            vacanyList.Enabled = true;
            vacanyList.Items.Clear();
            DataTable objVacancytbl = oDo.DropDownList("select VacId,VacTitle from tblVacancyMaster where DeptId =" + deptList.SelectedValue + " and CompId=" + companyList.SelectedValue);
            if (objVacancytbl.Rows.Count > 0)
            {
                vacanyList.DataSource = objVacancytbl;
                vacanyList.DataValueField = "VacId";
                vacanyList.DataTextField = "VacTitle";
                vacanyList.DataBind();
                vacanyList.Items.Insert(0, new ListItem("--Select Vacancy--", "-1"));
                appForVacGrid.DataSource = null;
                appForVacGrid.DataBind();
            }
            else
            {
                notifyLbl.Visible = true;
                notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN "+" "+deptList.SelectedItem.Text.ToUpper();
                vacanyList.Enabled = false;
                appForVacGrid.DataSource = null;
                appForVacGrid.DataBind();
            }
        }
        else
        {
            notifyLbl.Visible = true;
            notifyLbl.Text = "Select Department...";
            appForVacGrid.DataSource = null;
            appForVacGrid.DataBind();
            vacanyList.Enabled = false;
        }
    }
    catch (Exception)
    {

        throw;
    }
}
protected void vacanyList_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        DataTable AppListTbl = objAppVacDetail.GetValue("CompId=" + companyList.SelectedValue + " and DeptId=" + deptList.SelectedValue + " and VacId=" + vacanyList.SelectedValue);
        if (AppListTbl.Rows.Count > 0)
        {
            appForVacGrid.DataSource = AppListTbl;
            appForVacGrid.DataBind();
            appForVacGrid.Columns[5].Visible = false;
            Session.Add("snAppListTbl", AppListTbl);
            titleLbl.Visible = true;
            titleLbl.Text = AppListTbl.Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + ".";
            sendMailBtn.Visible = true;
        }
        else
        {
            notifyLbl.Visible = true;
            notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN " + " " + deptList.SelectedItem.Text.ToUpper();
            appForVacGrid.DataSource = null;
            appForVacGrid.DataBind();
        }
    }
    catch (Exception)
    {

        throw;
    }
}
protected void appForVacGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
        //DataTable AppListTbl = (DataTable)Session["snAppListTbl"];
        titleLbl.Visible = true;
        titleLbl.Text = ((DataTable)Session["snAppListTbl"]).Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + ".";
        appForVacGrid.PageIndex = e.NewPageIndex;
        appForVacGrid.DataSource = (DataTable)Session["snAppListTbl"];
        appForVacGrid.DataBind();
        sendMailBtn.Visible = true;
}
protected void sendMailBtn_Click(object sender, EventArgs e)
{
    DataTable AppListTable = ((DataTable)Session["snAppListTbl"]);
    int intTotalRows = AppListTable.Rows.Count;
    string strFromId="chetan.patel@sahmed.com",strToId="",strCcId="",strBccId="";
    string strVacTitle="",strCompName="",strMailBody="";
    string strSubject = "Rgarding Selection of Your Resume";
    Label EmailLbl;
    bool isMailSent=false;
    //for (int intRow = 0; intRow < intTotalRows; intRow++)
    foreach (GridViewRow Row in appForVacGrid.Rows)
    {
        CheckBox objSendMail = (CheckBox)Row.FindControl("sendMailBox");
        //CheckBox objSendMail = (CheckBox)appForVacGrid.Rows[Row].FindControl("sendMailBox");
        if (objSendMail.Checked)
        {
            if (strToId == "")
            {
                //strToId = AppListTable.Rows[Row]["AppEmailId1"].ToString();
                EmailLbl = (Label)Row.FindControl("emailLbl");
                strToId = EmailLbl.Text;
            }
            else
            {
                //strToId += "," + AppListTable.Rows[Row]["AppEmailId1"].ToString();
                EmailLbl = (Label)Row.FindControl("emailLbl");
                strToId +=","+ EmailLbl.Text;
            }
        }
    }
    strVacTitle = vacanyList.SelectedItem.Text;
    strCompName = companyList.SelectedItem.Text;
    ////appForVacGrid.AllowPaging = false;
    ////appForVacGrid.DataBind();
     strMailBody = CommonProcedures.GetMailBody(strVacTitle, strCompName);
    isMailSent=CommonProcedures.SendMail(strFromId, strToId, strCcId, strBccId, strSubject, null, strMailBody, false);
    //appForVacGrid.AllowPaging = true;
    //appForVacGrid.DataBind();
     if (!isMailSent)
     {
         titleLbl.Visible = true;
         titleLbl.Text = "MAIL HAS BEEN SENT TO THE SELECTED APPLICANTS";
         sendMailBtn.Visible = true;
     }
     else
     {
         titleLbl.Visible = true;
         titleLbl.Text = "MAIL SENDING FAIL.... TRY AGAIN LATER..";
         sendMailBtn.Visible = true;
     }
     appForVacGrid.DataSource = AppListTable;
     appForVacGrid.DataBind();
}

}

2 个回复

您正在页面PageIndexChanging事件上绑定网格,因此您无法使用代码首先将必须更新的复选框值更新为会话中的数据表。 所以下次它会显示选中的复选框..

1 保留复选框状态

我有一个问题,在菜单栏上单击时,选中的复选框丢失了。 如果选择零食 零食-&gt;吹气---&gt;蔬菜---&gt;并选中所有复选框 最后,如果我们单击“饮料”。 问题在于,“零食”下选中的复选框不会被重新绑定。 请在这里查看jsfiddle http://fi ...

2014-06-04 09:44:46 1 56   jquery
2 分页时保留选定的复选框

任何想知道如何在分页过程中以及在使用下面的代码在selectlist中选择另一个值的过程中保留所选复选框的人。 谢谢! :) 这是我使用的扩展名。 它包括表格,选择值和分页。 这就是我使用的VisualForce。 它还包括表,选择列表和分页。 ...

3 复选框不保留其状态

我是ASP MVC 3的初学者,我开发了一个应用程序,我首先要描述我的应用程序,因此我在数据库中拥有一个表帐户和一个表角色,并且帐户和角色之间的关系非常多,因此我有一个关联表account_role,我首先使用Entity Framework数据库,并使用EF DbContext Generat ...

4 如何在Struts中的分页过程中保留复选框状态

我正在Struts 1.3中开发Web应用程序,正在使用显示标签显示数据,在表display.i中有复选框。我是显示标签中的新功能,分页是我想要保持页面之间复选框状态的所有功能。 发生的情况是,当我选中一个复选框,然后转到第二页,然后在此处选中一个复选框时,当我返回上一页时,再次取消选中该复选 ...

5 在分页时维护复选框状态时出错

我访问了许多站点,找到了很多答案,但是似乎没有一个可以解决我的错误。 我想维护复选框的状态,以便回到第二页之前进入第二页之前选中的复选框数保持不变。 我的代码如下:Default.aspx文件是: CS文件是: 我添加了两个按钮“保存并检查”。 因此,当我将页面从2移到1后 ...

6 复选框状态应保留状态以隐藏/取消隐藏页面加载中的文本

作为此问题的后续措施,虽然复选框值存储在页面加载中,但是即使未选中该复选框,文本也会显示在页面加载中。 仅当用户在整个会话过程中或在关闭浏览器之前未选中/选中该复选框时,该文本才应隐藏/显示。 存储复选框状态的js代码: 该^代码应该可以正常工作,但是它保留了复选框的状态,但不适 ...

7 分页过程中如何保留用户打勾的复选框值

我正在使用struts框架创建一个在线测试应用程序。 在jsp页面中,我正在使用显示标签浏览问题。 我可以每页显示4个选项的一个问题。 这是我的代码。 我的问题是,在页面1中,如果用户选择一个复选框选项并转到下一页,则当他返回该页面时该复选框未选中,反之亦然。 我怎么解决这个问题 ...

9 Ext JS 4.2.1-带分页的网格-复选框状态丢失

我有一个ExtJS网格,该网格具有用于(远程)分页的PagingToolbar和一个定义为的复选框列: 但是,如果我选中一个复选框,然后向前和向后翻页,则不会保存复选框状态-所有复选框均未选中。 我猜因为存储只包含当前页面(来自服务器)的数据,所以我需要某种方式来存储不在当前数据页 ...

10 分页显示错误时,GridView复选框保持状态

我的gridview遇到问题 从一页移动到另一页时出现错误 它不保留分页上的复选框状态 在KeepChecks上出现错误 错误消息索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引ERROR MESSAGE 下面是代码... 您的帮助 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM