开发者

Asp.net Gridview

开发者 https://www.devze.com 2023-01-09 02:35 出处:网络
I\'ve like two gridviews on webpage overlapped on one another I\'ve a button which toggles those gridviews.when the page loads gridview1 loads and gridview2 is hidden and when i hit the button gridvie

I've like two gridviews on webpage overlapped on one another I've a button which toggles those gridviews.when the page loads gridview1 loads and gridview2 is hidden and when i hit the button gridview2 shows up and gridview1 hides(using javascript) both gridviews have paging enabled.the problem is when I want to goto page2 of gridview2 the page gets reloaded and gridview2 disappears and gridview1 shows up.I'm a new bee to asp.net and I want to know is there a way to 开发者_开发知识库stop gridview1 from reloading when I want to view page2 of gridview2

Regards, Ravi


This is not the best solution but the quickest and easiest, put GridView2 in an UpdatePanel:

<form id="Form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>


Actually all the page is reloaded because the gridviews don't mantain state between postbacks.
That is how asp.net controls work.

If you are using javascript to show one or the other gridview you first have to save which one of the gridviews you have currently visible.

You can save it on the Viewstate

Viewstate["visibleGrid"] = visibleGrid1.Enabled ? 1 : 2

Then in your Page_Load you have to check wich on is visible and set it manually yourself

protected void Page_Load(object sender, EventArgs e)
{
        gridview1.Visible = (int)Viewstate["visibleGrid"] == 1;
        gridview2.Visible = (int)Viewstate["visibleGrid"] == 2;
}

finally you have to have a PageIndexChanging for gridview1 and 2 beacuse when the page changes, the gridview have to be bounded again

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.Datasource = ...
    GridView1.Databind();
}

protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView2.PageIndex = e.NewPageIndex;
    GridView2.Datasource = ...
    GridView2.Databind();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消