1 Replies - 831 Views - Last Post: 11 December 2015 - 09:35 AM

#1 artlemaks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-October 15

GridView in Update Panel, bind data on button click

Posted 19 October 2015 - 05:08 AM

Hi all,

I am facing a problem and can't seem to be able to find a solution to it.

I have a GridView inside of an update panel. I have a button that activates the update of the panel, but the grid view does not appear. I just can't seem to get it working. Any suggestions are appreciated!

Here is the code of my update panel:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" updatemode="Conditional">
     <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnAllReport" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="ImageButton3" EventName="Click" />
        </Triggers>
        <ContentTemplate>
            <br />
            Start Date:
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:ImageButton ID="ImageButton1" runat="server" 
            ImageUrl="~/Images/calender.png" onclick="ImageButton1_Click" />
            &nbsp; End Date:
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:ImageButton ID="ImageButton2" runat="server" 
                ImageUrl="~/Images/calender.png" onclick="ImageButton2_Click" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:ImageButton ID="ImageButton3" runat="server" 
            ImageUrl="~/Images/refresh.png" onclick="ImageButton3_Click" />
            <table class="style1">
                <tr>
                    <td class="style2">
                        <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
                            BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" 
                            Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" 
                            onselectionchanged="Calendar1_Selectionchanged1" Visible="False" Width="200px">
                            <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                            <SelectorStyle BackColor="#CCCCCC" />
                            <WeekendDayStyle BackColor="#FFFFCC" />
                            <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                            <OtherMonthDayStyle ForeColor="#808080" />
                            <NextPrevStyle VerticalAlign="Bottom" />
                            <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
                            <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
                        </asp:Calendar>
                    </td>
                    <td>
                        <asp:Calendar ID="Calendar2" runat="server" BackColor="White" 
                            BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" 
                            Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" 
                            onselectionchanged="Calendar2_Selectionchanged" Visible="False" Width="200px">
                            <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                            <SelectorStyle BackColor="#CCCCCC" />
                            <WeekendDayStyle BackColor="#FFFFCC" />
                            <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                            <OtherMonthDayStyle ForeColor="#808080" />
                            <NextPrevStyle VerticalAlign="Bottom" />
                            <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
                            <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
                        </asp:Calendar>
                    </td>
                </tr>
            </table>
            <br />
        <asp:Button ID="btnAllReport" runat="server" 
             Text="All Time Report" onclick="GetData" AutoPostBack="True"/>
            <br />
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                CellPadding="4" ForeColor="#333333" GridLines="None" Visible="False">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID" />
                    <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
                    <asp:BoundField DataField="ProductName" HeaderText="ProductName" />
                    <asp:BoundField DataField="SupplierName" HeaderText="SupplierName" />
                    <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
                    <asp:BoundField DataField="UnitPrice" HeaderText="UnitPirce" />
                    <asp:BoundField DataField="TotalCost" HeaderText="TotalCost" />
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
            <br />
        </ContentTemplate>
       
    </asp:UpdatePanel>
</



And a code for my GetData function:
public void GetData(object sender, EventArgs e)
    {
        GridView1.Visible = true;
        SqlCommand cmd = new SqlCommand("SELECT     o.ProductID AS ProductID, SUM(o.Quantity) AS Quantity, p.ProductName AS ProductName, s.CompanyName AS SupplierName, c.CategoryName AS CategoryName, p.UnitPrice AS UnitPrice, SUM(o.Quantity * p.UnitPrice) AS TotalCost" +
        "FROM [Order Details] AS o INNER JOIN" +
                      "Products AS p ON o.ProductID = p.ProductID INNER JOIN" +
                      "Suppliers AS s ON s.SupplierID = p.SupplierID INNER JOIN" +
                      "Categories AS c ON c.CategoryID = p.CategoryID" +
        "WHERE     (p.SupplierID = '" + suppID + "')" +
        "GROUP BY o.ProductID, p.ProductName, s.CompanyName, c.CategoryName, p.UnitPrice, s.CompanyName", new SqlConnection(ConfigurationManager.ConnectionStrings["databaseConnection1"].ConnectionString));
        cmd.Connection.Open();
        SqlDataReader sdrEmp = cmd.ExecuteReader();
        try
        {
            
            if (sdrEmp.HasRows)
            {
                DataTable dt = new DataTable();
                dt.Load(sdrEmp);
                //Response.Write(dt
                GridView1.DataSource = dt;
                GridView1.DataBind();        // BIND DATABASE TABLE WITH THE GRIDVIEW.
            }

        }

        catch (Exception ex) { }
        finally
        {
            sdrEmp.Close();
            sdrEmp = null;
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: GridView in Update Panel, bind data on button click

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: GridView in Update Panel, bind data on button click

Posted 11 December 2015 - 09:35 AM

I'm not really an ASP.Net person. I've been playing with it, but I'm a long ways from being an expert on it by any means. But since no one else has answered your question yet, I'll take a stab at it.

I had a lot of similar problems. First thing that comes to mind is whether the button needs to have the "AutoPostBack" property set to true in order for the events it uses to fire? That one caused me some grief until I figured it out. In my case, I was trying to use an event of a drop down menu being changed rather than a button press, but I would think it would basically be the same. If your method is never called, it's certainly not going to do anything including update your GridView.

In my code, I use a ListView. I also use a stored procedure rather than allowing direct manipulation of the database with potentially disastrous consequences. (A web-service might be another way to protect the database from direct manipulation.) So, I'm not sure how different that is but here's my code:

     protected void ModelDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SelectedModelID = Convert.ToInt32(ModelDropDown.SelectedItem.Value);
            SqlConnection DBConnection;
            //SqlDataReader PartData;
            SqlDataAdapter PartsData;
            SqlCommand ProcedureCall;
            DataTable PartsForVehicle;


            if (SelectedModelID > 0)
            {
                DBConnection = new SqlConnection(DBConnectionString);
                ProcedureCall = new SqlCommand("usp_MyStoredProcedure", DBConnection);
                PartsData = new SqlDataAdapter();
                PartsForVehicle = new DataTable();
                ProcedureCall.CommandType = CommandType.StoredProcedure;
                ProcedureCall.Parameters.AddWithValue("@ModelID", SelectedModelID);

                try
                {
                    DBConnection.Open();
                    //PartData = ProcedureCall.ExecuteReader();
                    PartsData.SelectCommand = ProcedureCall;
                    PartsData.Fill(PartsForVehicle);
                    ListView1.DataSource = PartsForVehicle;
                    ListView1.DataBind();
                    
                }
                catch
                {

                }
                finally
                {
                    ProcedureCall.Dispose();
                    DBConnection.Close();
                    DBConnection.Dispose();
                }
            }
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1