回发后如何限制gridview数据并在gridview的行上获取检查值

时间:2018-10-26 06:39:17

标签: c# asp.net gridview webforms

我有一个gridview,单击“搜索”按钮后,数据将被填充到gridview中,因此页面加载我的griview为空。在gridview的内部有一个复选框列,然后单击“下载”按钮,我将遍历选中的行并获得fileName,然后再下载所有选中的内容。问题是,当我单击此下载按钮时,发生了回发,并且我认为这导致gridview为空,因此循环检查的行不起作用。

但这在本地有效,但是当使用https作为真实网站发布时,则无效。

任何帮助将不胜感激。

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Portfolio.aspx.cs" 
    Inherits="Portfolio_CustomPage.Portfolio" %>
    <!DOCTYPE html>
     <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <br />

    <div style="margin-left:auto; margin-right:auto; width:1000px;">

        <div style="text-align: left; height:200px; width:100%;">
           <asp:TextBox ID="TextBox1" runat="server" placeholder="SearchBox" Width="245px" Font-Size="12px">
                        </asp:TextBox>
            <br />
            <asp:Label ID="Label4" runat="server" Text="OR" Font-Names="Segoe UI" Font-Size="11px"></asp:Label>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Search" Font-Size="12px" OnClick="searchBtn" 
                        style="background-color: #008CBA; padding: 5px 10px; cursor: pointer; " Width="112px" ForeColor="White" BorderStyle="None"/>
               </div>
        <br />
        <asp:Label ID="Label5" runat="server" Text="GrandParent" Font-Names="Segoe UI"></asp:Label>
            <asp:GridView ID="GridView1" runat="server" Width="1000px" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="15" BackColor="#CCCCCC" Font-Size="12px" ShowHeaderWhenEmpty="True" OnRowDataBound="GridView_RowDataBound" DataKeyNames="FileID">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                    <asp:BoundField DataField="FileName" HeaderText="FileName" HeaderStyle-Width="200px"/>
                    <asp:BoundField DataField="FileID" HeaderText="FileID" Visible="False" />
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckboxSelectAll" onclick="HeaderCheckBoxClick(this);" runat="server" />
                    </HeaderTemplate>
                    <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" onclick="ChildCheckBoxClick(this);" />
                        </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
       <br />
            <div style="text-align:right;">
                <asp:Button ID="Button4" runat="server" OnClick="Download_Button" style="background-color: #008CBA; padding: 5px 10px; cursor: pointer; " Text="DownloadSelected" Width="112px" ForeColor="White" BorderStyle="None" Font-Size="12px" />
            </div>
    </div>

后面的代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadData();

        }
    }

    void loadData()
    {
        string Search = TextBox1.Text;

        if (Search != "" )
        {
            LoadDataIntoGridView();
        }
        else
        {
            BindGridviewWithEmptyData();
        }

    }

protected void searchBtn(object sender, EventArgs e)
    {
        loadData();
    }

protected void Download_Button(object sender, EventArgs e)
    {

        var Filename = new List<string>();
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gr.FindControl("CheckBox1");
            if (cb.Checked)
            {
                string FileName = (string)GridView1.DataKeys[gr.RowIndex]["FileID"];
                if (FileName != "")
                {
                    //count++;
                    Filename.Add(FileName);
                }

            }
            //count++;
        }
        Download(Filename);
    }

0 个答案:

没有答案