如何隐藏随机生成的gridview行 [英] How to hide randomly generated row of gridview

查看:17
本文介绍了如何隐藏随机生成的gridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI 编码器我有一个 gridview,其中的数据来自数据库.. 现在所有行的 id 都是相同的,我希望它只显示我的 gridview 的第一行并隐藏其他人的其余部分但不隐藏它们的值我只是想隐藏他们.我想要它们的价值,因为我有一个灯箱.

HI coder i have a gridview in which data comes from database.. now id of all row is same what i want it to show only first row of my gridview and hide rest of the others but not their value i just wana hide them. i want their value because i have a lightbox on it.

这是我的代码:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="False" PageSize="1" PersistedSelection="true" 
    DatakeyNames="pptId,Priority">
        <Columns>

            <asp:TemplateField HeaderText="pptId" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblpptId" runat="server" Text='<%# Bind("pptId") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>    
          <asp:TemplateField HeaderText="Priority" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblPriority" runat="server" Text='<%# Bind("Priority") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="View PPT">
                <ItemTemplate>
                    <a id="imageLink" href='<%# Eval("Imageurl") %>' title='<%#Eval("Description") %>'
                        rel="lightbox[Brussels]" runat="server">Start PPT</a>
                </ItemTemplate>
            </asp:TemplateField>                
        </Columns>
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
         </asp:GridView>

:

protected void Page_Load(object sender, EventArgs e)
{
    BindModule();

}
protected void BindModule()
{
    try
    {
        con.Open();
        string id = Request.QueryString["pptId"].ToString();
        //Query to get Imagesurl and Description from database
        SqlCommand command = new SqlCommand("select * from Image_Master where pptId='" + id + "' and IsEnable='True' order by Priority", con);
        dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(command);
        da.Fill(dt);          
        GridView1.DataSource = dt;
        GridView1.DataBind();                      
        GridView1.Dispose();
    }
    catch (Exception ex)
    {
        Response.Write("Error occured : " + ex.Message.ToString());
    }
    finally
    {
        con.Close();
    }
}

在上面的代码中,每一行的 pptId 都是相同的,我的图像显示在我的 gridview 中,我想要的是只显示第一张图像并隐藏其他图像而不是它们的值..

in the above code pptId is same for each row, my images show inside my gridview what i want is to show only first images and hide rest of other but not their values..

我该怎么做..... 提前致谢

how to i do that.....thanks in advance

推荐答案

你不能用 css 来做这个吗?

Can't you just do this with css?

table tr:nth-child(n + 3) {
    display: none;
}

这里有对第n个子css-selector的解释:

There is an explanation of the nth-child css-selector here:

http://css-tricks.com/how-nth-child-作品/

我也在另一个答案中解释过:

I've also explained it in another answer here:

https://stackoverflow.com/a/21166162/1256868

您可能需要在生成的表上添加一个类或一个静态 ID,以便您可以单独使用该表,但仍然...

You would probably need a class or a static id on the generated table so you can single that table out, but still...

编辑以下评论:

尝试在gridview中添加一个css类并像这样添加上面的css:

Try adding a css class to the gridview and adding the css above like this:

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        table.Grid tr:nth-child(n + 3) {
            display: none;
        }
    </style>
</head>
<body>
    <form runat="server">
    <asp:GridView runat="server" CssClass="Grid">
        <%--Your templates here--%>
    </asp:GridView>
    </form>
</body>
</html>

Asp.net 将从您的 gridview 生成一个 html 表格,这就是您需要对表格应用样式的原因.浏览器永远不会看到任何 asp.net 代码.该代码只是生成浏览器知道如何显示的 html 的一种方式.

Asp.net will generate an html table from your gridview, that is why you need to apply styling to a table. The browser never sees any asp.net code. That code is simply a way of generating html that the browser knows how to display.

关于更好的 IE 支持的

Edit regarding better IE support:

我猜你要问的是对 IE8(可能还有 7)的支持,因为 IE9 及更高版本支持 nth-child.要支持 IE7 及更高版本,请将您的 css 更改为使用 + 选择器,它具有更好的支持:

I guess what you are asking is for IE8 (and possibly 7) support, as IE9 and up support nth-child. To support IE7 and up, change your css to use the + selector, which has better support:

table.Grid tr + tr + tr{    
    display: none;
}

+ 选择器是相邻的兄弟选择器.选择器 tr + tr 因此选择任何紧跟在另一个 tr 元素之后的 tr.如需进一步说明,请参阅:http://css-tricks.com/child-and-兄弟选择器/(特别是标题为相邻兄弟组合器"的部分).

The + selector is the adjacent sibling selector. The selector tr + tr thus selects any tr that immediatly follows another tr element. For further exaplnation see: http://css-tricks.com/child-and-sibling-selectors/ (specifically the section titled 'Adjacent sibling combinator').

这篇关于如何隐藏随机生成的gridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆