locked
How to restraint gridview data and get checked value on rows of gridview after postback RRS feed

  • Question

  • User-236416143 posted

    I have a gridview and data will be populated into the gridview after a Search button is clicked. Inside the gridview there is a checkbox column, then onclick of a Download button I will loop through the checked rows and get the fileName before download all the checked. The problem is when I clicked this Download button a postback occured and I think this caused the gridview to be empty thus the looping checked rows not working.

    However this worked locally but when published as real website with https it is not working.

    Any help is greatly appreciated.

    <%@ 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>

    my code behind

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

    Thursday, October 25, 2018 5:10 AM

All replies

  • User-271186128 posted

    Hi xchaax,

    I have a gridview and data will be populated into the gridview after a Search button is clicked. Inside the gridview there is a checkbox column, then onclick of a Download button I will loop through the checked rows and get the fileName before download all the checked. The problem is when I clicked this Download button a postback occured and I think this caused the gridview to be empty thus the looping checked rows not working.

    However this worked locally but when published as real website with https it is not working.

    According to your code, I create a sample using the following code, it seems that everything works well on my side, I could get the selected row values.

            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!=null && cb.Checked)
                    {
                        string FileID = GridView1.DataKeys[gr.RowIndex]["FileID"].ToString();
                        string FileName =  gr.Cells[0].Text;
                        if (FileName != "")
                        {
                            //count++;
                            Filename.Add(FileID +"---"+ FileName);
                        }
    
                    }
                    //count++;
                }
    
                lblresult.Text = "Selected file:" + string.Join(";", Filename);
               // Download(Filename);
            }

    The screenshot as below:

    So, I suggest you could re-test your application, if still not working, you could share the whole code (include the paging and checkbox click event), so that we could test it on our side.

    Best regards,
    Dillion

    Friday, October 26, 2018 6:05 AM
  • User-1716253493 posted

    If it worked locally, i guess the problem is related to accessing the file, such as wrong path or security issue

    Friday, October 26, 2018 6:58 AM