stop gridview posting back on clicking anywhere

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aleact
    New Member
    • Oct 2011
    • 6

    stop gridview posting back on clicking anywhere

    I have a gridview bound to some sql data and I have some textboxes that let me search this data. The problem is that when the client enters a search term, they can click anywhere on the page and the search happens. But they only want it to happen when the "search" button is pressed. (I am surprised this is a 'problem' but what can I do?)

    Can I somehow stop the postback from happening until I call it? I tried an Update panel around the gridview, but it doesn't appear to have any effect.

    Update:
    Let me clarify just a little bit... when the search term is entered in the text box and any click event occurs, the gridview does it's thing and returns only the relevant items (done in a SQL stored procedure). I don't want this to happen until a button is pressed.


    Thanks,

    Alea
    Last edited by Niheel; Oct 10 '11, 08:06 PM. Reason: part of question, merged
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    #2
    It sounds as if your search textbox has "AutoPostback=T rue". Set this property to False.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      It sounds like you are calling the "search" functionality in the Page Load event...but what you really want to do is only call this functionality in Search Button Click event.

      -Frinny

      Comment

      • aleact
        New Member
        • Oct 2011
        • 6

        #4
        Thanks! I set the autopostback on the textboxes to false, and it solved the problem.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          If your search code is in the Page Load event then you're still going to search whenever a postback occurs.

          This means that you may have solved to problem for your TextBoxes causing the page to post back...but the problem will still exist when any other control causes a page postback.

          So, if there's another button (not the search button), and you click it, the search will still happen.

          Or say there's a DropDownList on the page that posts back during a SelectedIndexCh anged event...the search will happen when this occurs too.

          In other words, if your search code is in the Page Load event, this code will execute whenever any control on the page posts back to the server.

          -Frinny
          Last edited by Frinavale; Oct 11 '11, 05:56 PM.

          Comment

          • aleact
            New Member
            • Oct 2011
            • 6

            #6
            yes, I see what you mean. But I don't call the gridview load explicitly. It loads automatically on Page Load. Is the code for that hidden somewhere? I do want the gridview to load once, but I'd like to put an if(!postback) in there.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I don't know what you're doing without seeing any code.
              Post the code that is responsible for populating the GridView so that I can see what you're doing.

              Comment

              • aleact
                New Member
                • Oct 2011
                • 6

                #8
                Code:
                 <asp:GridView ID="gvSearch" runat="server" AllowSorting="True" 
                         AutoGenerateColumns="False"
                        DataKeyNames="PO_Number" DataSourceID="SearchSource" 
                        onselectedindexchanged="gvSearch_SelectedIndexChanged"
                        CssClass="mGrid"
                        AlternatingRowStyle-CssClass="alt"
                        FooterStyle-CssClass="ftr" 
                        HeaderStyle-CssClass="hdr"
                        PagerStyle-CssClass="pgr" 
                        RowStyle-CssClass="row" 
                        SelectedRowStyle-CssClass="selrow">
                        <Columns>
                            <asp:CommandField ShowSelectButton="True" />
                            <asp:BoundField DataField="PODisplay" HeaderText="PO Number" 
                                SortExpression="PODisplay" />
                            <asp:BoundField DataField="Equip_SerialNO" HeaderText="Serial Number" 
                                SortExpression="Equip_SerialNO" />
                            <asp:BoundField DataField="Equip_Model_Name" HeaderText="Model" 
                                SortExpression="Equip_Model_Name" />
                            <asp:BoundField DataField="UserName" HeaderText="User Name" 
                                SortExpression="UserName" />
                            <asp:BoundField DataField="Emp_Dept" HeaderText="Department" 
                                SortExpression="Emp_Dept" />
                            <asp:BoundField DataField="Building" HeaderText="Building" 
                                SortExpression="Building" />
                            <asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" />
                            <asp:BoundField DataField="PO_CostCenter" HeaderText="Cost Center" 
                                SortExpression="PO_CostCenter" />
                            <asp:BoundField DataField="Equip_LeaseEndDate" DataFormatString="{0:d}" 
                                HeaderText="Lease End Date" HtmlEncode="False" 
                                SortExpression="Equip_LeaseEndDate" />
                            <asp:BoundField DataField="Equip_DeployedDate" DataFormatString="{0:d}" 
                                HeaderText="Deployed Date" HtmlEncode="False" 
                                SortExpression="Equip_DeployedDate" />
                            <asp:BoundField DataField="Equip_DeployedBy" HeaderText="Deployed By" 
                                SortExpression="Equip_DeployedBy" />
                        </Columns>
                    </asp:GridView>
                    <asp:SqlDataSource ID="SearchSource" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:PCLeaseConnectionString %>" 
                        SelectCommand="SearchPOs" SelectCommandType="StoredProcedure">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="txtPO" DefaultValue="EMPTY" Name="PO" 
                                PropertyName="Text" Type="String" />
                            <asp:ControlParameter ControlID="txtFirstName" DefaultValue="EMPTY" 
                                Name="UserName" PropertyName="Text" Type="String" />
                            <asp:ControlParameter ControlID="txtDept" DefaultValue="EMPTY" Name="Dept" 
                                PropertyName="Text" Type="String" />
                            <asp:ControlParameter ControlID="txtSN" DefaultValue="EMPTY" Name="SN" 
                                PropertyName="Text" Type="String" />
                            <asp:ControlParameter ControlID="txtLeaseExpiration" DefaultValue="1-1-2000" 
                                Name="LeaseExpiration" PropertyName="Text" Type="String" />
                            <asp:ControlParameter ControlID="txtLeaseExpirationUpperEnd" 
                                DefaultValue="12-12-5000" Name="LeaseExpirationUpper" PropertyName="Text" 
                                Type="String" />
                            <asp:ControlParameter ControlID="txtModel" DefaultValue="EMPTY" Name="Model" 
                                PropertyName="Text" Type="String" />
                            <asp:ControlParameter ControlID="txtCostCenter" DefaultValue="EMPTY" 
                                Name="CostCenter" PropertyName="Text" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  I'm so sorry but I've never used a SqlDataSource. I find them restricting.

                  I could try to help you based on what you currently have or we could remove the this component completely and populate the GridView in your C# or VB.NET code.


                  The SqlDataSource is automatically providing the SQL command with parameters based on the values provided by the user. It uses ControlParamete rs to do this.

                  We don't want it to always use these values so we somehow need to prevent this from happening. We'll have to remove the ControlParamete rs from your asp.net code and manually provide these values in your C# or VB.NET server-side code.

                  If we continue to use the SqlDataSource then we will have to figure out when the Select is executed during the ASP.NET page life cycle... If it's near the end of the life cycle (after the other page events [like the button click event] occur) then it might be easy to filter the select manually in the button click event. If not, then we're going to have some issues working around this.

                  Honestly, if you want this type of control, I recommend doing away with the SqlDataSource. It just produces a DataView for the GridView to bind to and you can easily create this DataView in your C# or VB.NET code yourself... like I always do.

                  -Frinny
                  Last edited by Frinavale; Oct 11 '11, 06:41 PM.

                  Comment

                  • yarbrough40
                    Contributor
                    • Jun 2009
                    • 320

                    #10
                    If I may just add my two cents... Frinny is being very kind about his suggestions - for what it's worth, Once you learn to not use these SqlDataSources (as he is suggesting) and instead make your own deliberate database calls, you will find that it opens a whole new world for you. It actually becomes easier to manage and gives you much more control of your applications. Listen to him - he is speaking the good truth : )

                    Comment

                    • aleact
                      New Member
                      • Oct 2011
                      • 6

                      #11
                      Thanks for all the guidance guys.

                      I'm looking at DataView examples and it looks pretty straightforward . I use SQL calls in the code, but haven't used the DataView - wasn't sure how else to bind a gridview other than SQLDataSource.

                      I'll let you know how it goes.

                      Comment

                      • yarbrough40
                        Contributor
                        • Jun 2009
                        • 320

                        #12
                        DataView is just used for sorting data once you have it and before binding it to your UI object. Look into System.Data.Ole db (this is what I use)
                        Using that you will open a connection, fire sql at your server, put the result into a DataTable, then bind that DataTable to your gridview.

                        good luck!

                        Comment

                        • aleact
                          New Member
                          • Oct 2011
                          • 6

                          #13
                          Ok, all is working well now, no problems with extra postbacks, and using DataView to bind to gridview manually. I now populate the gridview when the search button is pressed and the first time the page is loaded.

                          Thanks again!

                          Alea

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Awesome :)
                            I'm glad you got it to work!

                            The one thing that I should probably tell you about now is that if you are using your GridView to allow the user to edit the data you should not call the DataBind method until the PreRender event.

                            If you call the DataBind method in the Page Load event you will over write the user's input with "nothing" and so you will never be able to retrieve what they supplied.

                            The PreRender event is the last event that you have access to the .NET controls before they are rendered. All the other page events occur before this (like button clicks etc) so it's a good time to do things like DataBinding.

                            -Frinny

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              Also, you can store the DataView in Cache or Session if you want to so that you don't always have to call your database to populate the GridView.

                              The nice thing about the DataView is that it has paging and filtering functionality :)

                              -Frinny

                              Comment

                              Working...