views:

665

answers:

5

Here is what I am doing. I create two buttons in a header row of a gridview that i created. The buttons are +page and -page. When -page is hit I remove the paging and all the data goes down the page and -page disappears and +page is there.

Here comes my issue When I hit +page I must be double postbacking because one row of my data disappears. I will provide the code below. What can i do to fix this??

Dim bttnrempag As New Button
    bttnrempag.ID = "bttnrempag"
    bttnrempag.Text = "      Paging"
    bttnrempag.CssClass = "bttnMinEG"
    bttnrempag.ValidationGroup = "alone"
    bttnrempag.Attributes.Add("onclick", "return Paging('1')")

    Dim bttnallowpag As New Button
    bttnallowpag.ID = "bttnallowpag"
    bttnallowpag.Text = "      Paging"
    bttnallowpag.ValidationGroup = "alone"
    bttnallowpag.CssClass = "bttnAddEG"
    bttnallowpag.Attributes.Add("onclick", "return Paging('0')")

---------------------How the buttons are added------------------------------------------

    Dim row As New GridViewRow(-1, -1, DataControlRowType.Separator, DataControlRowState.Normal)
    row.Cells.Add(cell)
    cell.Controls.Add(bttnrempag) '36
    cell.Controls.Add(bttnallowpag)

----------------------------------------------------------------------------------------

function Paging(remove) {
    var gridView = $get('<%=Gridview1.ClientID %>');
    var txt2 = $get('<%=TextBox2.ClientID %>');
    if (remove == '1') {
        txt2.value = '1';
        __doPostBack('<%=UpdatePanel1.ClientID %>', '');
        return false;
    }
    else {
        txt2.value = '0';
        __doPostBack('<%=UpdatePanel1.ClientID %>', '');
        return false;
    }

Edited

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate >
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            DataSourceID="SqlDataSource1" Font-Names="Comic Sans MS" Font-Size="XX-Small"
            Caption = '<table border="" width="100%" cellpadding="3" cellspacing="0" bgcolor="#4A3C8C"><tr><td style = "font-size:X-large;font-family:Arial CE;color:White"><b>Ordering/Receiving Log</u></td></tr></table>' 
            Font-Bold="True" PageSize="15" >
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />

            <Columns>
                      Bound Data
            </Columns>
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <EmptyDataTemplate>
            <head>
                <meta http-equiv="refresh" content="5;URL=/Corporate_newpo/ReceivingLog.aspx?">
            </head>
                <script type="text/javascript" >
                    var count = 6;
                    var counter = setInterval("timer()", 1000); //1000 will  run it every 1 second

                    function timer() {
                        count = count - 1;
                        if (count <= 0) {
                            clearInterval(counter);
                            //counter ended, do something here
                            return;
                        }
                        $get("timer").innerHTML = "The Table will Reload in " + count + " secs";
                    }
                </script>
                <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Large" 
                    ForeColor="#993333" Text="No Data was Found for the Selected Filter"></asp:Label><br /><br />
               <span id="timer" style="font-size:medium;color:blue"></span>
            </EmptyDataTemplate>
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <AlternatingRowStyle BackColor="#F7F7F7" />
            <PagerTemplate>
                <small 12px""="" style="font-size:xx-small; padding-right">Go To Page</small>
                <asp:DropDownList ID="ddlPageSelector" runat="server" AutoPostBack="true" 
                    Font-Size="XX-Small" Height="19px" Width="36px">
                </asp:DropDownList>
                <asp:ImageButton ID="btnFirst" runat="server" CommandArgument="First" 
                    CommandName="Page" SkinID="pagefirst" />
                <asp:ImageButton ID="btnPrevious" runat="server" CommandArgument="Prev" 
                    CommandName="Page" SkinID="pageprev" />
                <asp:ImageButton ID="btnNext" runat="server" CommandArgument="Next" 
                    CommandName="Page" SkinID="pagenext" />
                <asp:ImageButton ID="btnLast" runat="server" CommandArgument="Last" 
                    CommandName="Page" SkinID="pagelast" />
            </PagerTemplate>
            </asp:GridView>
        </ContentTemplate> 
    </asp:UpdatePanel>

So as you can see, when one or the other is hit I set a textbox to some value then i do a partial postback to my Gridview which is in that Updatepanel.

+1  A: 

I suspect it's because you called the "__doPostBack" method. The buttons' normal default click behaviour will perform the Async postback, you probably made it perform another time. Try removing it and see how it goes.

o.k.w
But by `return false` he should have prevented the default behavior.
Joel Potter
No. I get the same result.
Eric
Yeah. Joel is right.
Eric
Hmm... how about showing more of your codes? E.g. how you structure your controls in the UpdatePanel and how the databindings are done. The client-side "Paging(remove)" function isn't really necessary other than giving you a visual clue with the textbox value.
o.k.w
ok. I added code for you, I'm listening - you may have a point here.
Eric
I don't think that in this case return false will prevent the post back. The __doPostBack method is getting called explicitly which in turn calls theForm.submit(). The page has already been submitted, returning false will not cancel this behavior.
Phaedrus
@Phaedrus you're correct. But it only does a partial postback. not a full postback. But yes - the return false is pointless.
Eric
What makes you think that having a data row less is a result of double postback. In any case, if you remove the "Paging" script call completely, you are getting the same result?
o.k.w
@Eric I can see that you are adding the Page+/- Button dynamically. Just curious, at which stage of the page lifecycle to you perform that?
o.k.w
Pageload if it is postback and GridviewDatabound. Would that effect anything? I have no issues at all with my added header row w/these two buttons.
Eric
Well, controls added at PageLoad will not have their 'states' maintained. Give this a try: have the 2 buttons coded in the aspx file outside the gridview but within the updatepanel. See if it works.
o.k.w
Oh i know that will work just fine. But the button unfortunately should be inside the header of the gridview as it is. I know it will work because I have a button outside of it that removes paging to insert into an Excel page. Any other ideas?
Eric
Think i'll take your advice and mine. I'll make a work around with an invisible button and fire it's click event off with the page buttons.
Eric
ACtually. It didn;t make a difference. I still lose a row each submit.
Eric
Then perhaps the problem lies with the databinding and nothing to do with the buttons? Do you have any custom event handling during the databind?
o.k.w
+1  A: 

Hey Eric,

Can you try to put the Click Attribute on the buttons in RowDataBound event declaration??

Cheers.

Zinx
A: 

Try this, (I don't know why you were using javascript):

bttnrempag.onclick += bttnrempag_Click; 

protected void bttnrempag_Click(object sender, EventArgs e)
{
    //handle the removal of paging or whatever.
}
Chris
+1  A: 

Try changing your <asp:ImageButtons> to be regular HTML <img> tags.

I've run into this sort of thing before which was hard to diagnose and even harder to explain. See here and here for some buggy behavior that might help. If you have <img> tags with blank src attributes, then the hyperlinks I provided likely explain the problem. If you do not have any blank <img> tags then keep reading for my work around.

In my specific case, it was only happening on some servers (at some client sites) but not others, so I couldn't find any concrete issues with the code. My workaround was to change my <asp:ImageButtons> to regular <img> tags with javascript click events that would call a function to perform the postback, much like your Paging() function.

For some reason, I have found that an <asp:ImageButton> with a client side onclick event of return:confirm('are you sure?'); will stop a postback if the confirm() returns false, however returning false the way you do in your Paging() function after manually calling a __doPostBack does not consistently stop the page from posting back a second time.

wweicker
The links you gave point out the reason it was happening (but I don't know why it would only be on certain servers ... unless the path to the image was invalid or ASP.NET didn't have access to the image ... the path being invalid for images happens all the time if you develop in a virtual directory but deploy to a web application root.
Richard Hein
A: 

You have to provide an image for your ImageButtons. Otherwise the HTML rendered will produce an <img src=""> tag and when this is rendered, it will cause a postback. Try it.

Richard Hein