简体   繁体   English

页面刷新后保留GridView的可见属性(ASP.NET,C#)

[英]Retain Visible Attribute for GridView After Page Refresh (ASP.NET, C#)

  • OK so I assume I need to use SessionData but I can't seem to figure it out. 好的,所以我认为我需要使用SessionData,但似乎无法弄清楚。
  • What I have is a GridView with a button that toggles its visibility 我所拥有的是带有一个可切换其可见性的按钮的GridView
  • Another button that adds data the GridView and refreshes the page to view it immediately. 另一个按钮可在GridView中添加数据并刷新页面以立即查看它。
  • My problem is when the page refreshes, the GridView becomes invisible again until I click the button, how can I keep it how it was prior to refreshing. 我的问题是页面刷新时,GridView再次变为不可见,直到我单击按钮为止,如何保持它的刷新之前的状态。

GridView 网格视图

    <asp:GridView ID="GridViewPrograms" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource" CssClass="Grid">
    <Columns>
        <asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" ReadOnly="True" />
        <asp:BoundField DataField="MiddleName" HeaderText="MiddleName" SortExpression="MiddleName" ReadOnly="True" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" ReadOnly="True" />
        <asp:BoundField DataField="ProgramID" HeaderText="ProgramID" InsertVisible="False" ReadOnly="True" SortExpression="ProgramID" />
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ReadOnly="True" />
        <asp:BoundField DataField="ProgramDate" HeaderText="ProgramDate" SortExpression="ProgramDate" ReadOnly="True" />
    </Columns>
</asp:GridView><br />

Button I have to toggle visibility of GridView 按钮我必须切换GridView的可见性

protected void ButtonPrograms_Click(object sender, EventArgs e) {
    //Change Text Based on Button State
    if (ButtonPrograms.Text == "Programs") {
        ButtonPrograms.Text = "Hide";
    }
    else if (ButtonPrograms.Text == "Hide") {
        ButtonPrograms.Text = "Programs";
    }

    if (GridViewPrograms.Visible == true) {
        GridViewPrograms.Visible = false;
    }
    else if (GridViewPrograms.Visible == false) {
        GridViewPrograms.Visible = true;
    }
}

Change your codebehind code like below 如下更改代码背后的代码

protected void ButtonPrograms_Click(object sender, EventArgs e) {
    //Change Text Based on Button State
    if (ButtonPrograms.Text == "Programs") {
        ButtonPrograms.Text = "Hide";
        GridViewPrograms.Visible = true;
    }
    else if (ButtonPrograms.Text == "Hide") {
        ButtonPrograms.Text = "Programs";
        GridViewPrograms.Visible = false;
    }
}

Also in initial pageload you can set GridView visibility to true as below 同样在初始页面加载中,您可以将GridView可见性设置为true,如下所示

 protected void Page_Load(object sender, EventArgs e)
 {
        if (!IsPostBack)
        {
          GridViewPrograms.Visible = true;
          ButtonPrograms.Text == "Programs"

        }
 }

Combined Mudassir Hasan with while using a databind on the button that refreshes the page 将Mudassir Hasan与结合使用,同时在刷新页面的按钮上使用数据绑定

protected void ButtonPrograms_Click(object sender, EventArgs e) {
//Change Text Based on Button State
if (ButtonPrograms.Text == "Programs") {
    ButtonPrograms.Text = "Hide";
    GridViewPrograms.Visible = true;
}
else if (ButtonPrograms.Text == "Hide") {
    ButtonPrograms.Text = "Programs";
    GridViewPrograms.Visible = false;
}

Databind 数据绑定

if (GridViewPrograms.Visible == true) {
        GridViewPrograms.Visible = true;
    }
    else if (GridViewPrograms.Visible == false) {
        GridViewPrograms.Visible = false;
    }
    GridViewPrograms.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM