如何在C#中点击按钮后隐藏数据

发布于 2025-01-20 07:50:12 字数 119 浏览 0 评论 0原文

在上面的Gridview中有两个按钮1.显示数据2.隐藏数据。如果我们点击显示数据按钮意味着它已经在gridview中显示数据。如果我们单击隐藏数据,它应该隐藏 gridview 中的所有记录。请任何人告诉我如何用 c# 做

In the Above Gridview there Are two Buttons 1.Show Data 2.Hide Data. if we click on show data button means it has display data in gridview. if we clicked on hide data it should hide all records in gridview. please anyone tell me how to do with c#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄风 2025-01-27 07:50:12

好的,作为一般规则,您想发布您的标记的“一些”,甚至是您拥有的一些代码 - 这可以帮助这里的每个人都试图帮助您。

由于您是新手,我会在这里给您一些休息。

好的,所以假设我们在页面上有一个网格。

我们还假设页面上的两个按钮。一个显示网格,另一个用于隐藏网格。

因此,假设我们的GridView看起来像这样 - 标记(和两个按钮)。

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" 
        CssClass="table" OnRowDataBound="GridView1_RowDataBound" ShowHeaderWhenEmpty="True" >
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
            <asp:BoundField DataField="LastName" HeaderText="LastName"    />
            <asp:BoundField DataField="HotelName" HeaderText="HotelName"  />
            <asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="270" />

            <asp:TemplateField HeaderText="Rating">
                <ItemTemplate>
                    <asp:RadioButtonList ID="rbRating" runat="server" RepeatDirection="Horizontal" 
                        CssClass="rBut"
                        SelectedValue='<%# Eval("Rating") %>'>
                        <asp:ListItem Value="0">No Rating</asp:ListItem>
                        <asp:ListItem Value="1">Poor</asp:ListItem>
                        <asp:ListItem Value="2">Fair</asp:ListItem>
                        <asp:ListItem Value="3">Good</asp:ListItem>
                        <asp:ListItem Value="4">Excellent</asp:ListItem>
                        <asp:ListItem Value="5">5 Stars</asp:ListItem>
                    </asp:RadioButtonList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                    <asp:Button ID="cmdView" runat="server" Text="View" CssClass="btn"
                        OnClick="cmdView_Click"/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <br />
    <asp:Button ID="cmdShow" runat="server" Text="Show Grid" CssClass="btn" OnClick="cmdShow_Click"/>
    <asp:Button ID="cmdHideGird" runat="server" Text="Hide Grid" CssClass="btn"
        style="margin-left:10px" OnClick="cmdHideGird_Click"/>

加载网格的代码是这样:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGrid();
        }
    }

    void LoadGrid()
    {
        GridView1.DataSource = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName");
        GridView1.DataBind();
    }

    public DataTable MyRst(string strSQL)
    {
        DataTable rstData = new DataTable();
        using (SqlCommand cmdSQL = new SqlCommand(strSQL,
            new SqlConnection(Properties.Settings.Default.TEST4)))
        {
            cmdSQL.Connection.Open();
            rstData.Load(cmdSQL.ExecuteReader());
            cmdSQL.Connection.Dispose();
        }
        return rstData;
    }

现在我们有了:

“在此处输入图像说明”

现在,我们有两个按钮。一个显示网格,另一个要隐藏。

这两个按钮代码是这样:

    protected void cmdShow_Click(object sender, EventArgs e)
    {
        GridView1.Visible = true;
    }

    protected void cmdHideGird_Click(object sender, EventArgs e)
    {
        GridView1.Visible = false;
    }

Ok, as a general rule, you want to post "some" of your markup, and even some code you have - this helps everyone here try to help you.

Since you are new, I'll give you a bit of a break here.

Ok, so assume we have a grid on a page.

And we assume ALSO two buttons on the page. One to show the grid, the other to hide the grid.

So, say our GridView looks like this - the markup (and two buttons).

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" 
        CssClass="table" OnRowDataBound="GridView1_RowDataBound" ShowHeaderWhenEmpty="True" >
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
            <asp:BoundField DataField="LastName" HeaderText="LastName"    />
            <asp:BoundField DataField="HotelName" HeaderText="HotelName"  />
            <asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="270" />

            <asp:TemplateField HeaderText="Rating">
                <ItemTemplate>
                    <asp:RadioButtonList ID="rbRating" runat="server" RepeatDirection="Horizontal" 
                        CssClass="rBut"
                        SelectedValue='<%# Eval("Rating") %>'>
                        <asp:ListItem Value="0">No Rating</asp:ListItem>
                        <asp:ListItem Value="1">Poor</asp:ListItem>
                        <asp:ListItem Value="2">Fair</asp:ListItem>
                        <asp:ListItem Value="3">Good</asp:ListItem>
                        <asp:ListItem Value="4">Excellent</asp:ListItem>
                        <asp:ListItem Value="5">5 Stars</asp:ListItem>
                    </asp:RadioButtonList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="View">
                <ItemTemplate>
                    <asp:Button ID="cmdView" runat="server" Text="View" CssClass="btn"
                        OnClick="cmdView_Click"/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <br />
    <asp:Button ID="cmdShow" runat="server" Text="Show Grid" CssClass="btn" OnClick="cmdShow_Click"/>
    <asp:Button ID="cmdHideGird" runat="server" Text="Hide Grid" CssClass="btn"
        style="margin-left:10px" OnClick="cmdHideGird_Click"/>

The code to load the grid is this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadGrid();
        }
    }

    void LoadGrid()
    {
        GridView1.DataSource = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName");
        GridView1.DataBind();
    }

    public DataTable MyRst(string strSQL)
    {
        DataTable rstData = new DataTable();
        using (SqlCommand cmdSQL = new SqlCommand(strSQL,
            new SqlConnection(Properties.Settings.Default.TEST4)))
        {
            cmdSQL.Connection.Open();
            rstData.Load(cmdSQL.ExecuteReader());
            cmdSQL.Connection.Dispose();
        }
        return rstData;
    }

And now we have this:

enter image description here

Now, we have the two buttons. One to show the grid, and the other to hide.

Those two buttons code are this:

    protected void cmdShow_Click(object sender, EventArgs e)
    {
        GridView1.Visible = true;
    }

    protected void cmdHideGird_Click(object sender, EventArgs e)
    {
        GridView1.Visible = false;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文