简体   繁体   English

如何使用 html/css 设置 gridview、boundfield 和 asp.net 上的列的样式

[英]How to style gridview, boundfield, and columns on asp.net with html/css

I am trying to style gridview/boundfield/columns with html/css (mostly bootstrap) on asp.net. I'm not too sure how to make it prettier since I cannot implement any divs in between the gridview codes since it can't read it.我正在尝试在 asp.net 上使用 html/css(主要是引导程序)设置 gridview/boundfield/columns 的样式。我不太确定如何让它更漂亮,因为我无法在 gridview 代码之间实现任何 div,因为它无法读取它. I have two codes below that I am trying to style.我在下面有两个代码,我正在尝试设置样式。

Code 1 in asp.net with bootstrap html/css: asp.net 中的代码 1,带有引导程序 html/css:

       <div class="row">
                <div class="col-md-8 mx-auto">
                    <asp:GridView ID="gridviewJobsPageLoad" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="true" OnSelectedIndexChanged="gridviewJobsPageLoad_SelectedIndexChanged">
                        <Columns>
                            <asp:BoundField DataField="JobDescription" HeaderText="Job Title" />
                            <asp:BoundField DataField="JobYear" HeaderText="Job Year" />
                            <asp:BoundField DataField="DateStart" HeaderText="Start Date" />
                            <asp:BoundField DataField="Category" HeaderText="Category" />
                            <asp:BoundField DataField="Link" HeaderText="Link" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                            <asp:TemplateField ShowHeader="false">
                                <ItemTemplate>

                                    <asp:Button runat="server" ID="btnApplyToJob" class="btn btn-secondary btn-lg" OnClick="btnApplyToJob_Click" Text="Apply Now" CommandArgument='<%# Eval("Link") %>' />


                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField ShowHeader="false">
                                <ItemTemplate>

                                    <%-- <asp:CheckBox ID="checkBoxAppliedConfirmation" runat="server" AutoPostBack="true" OnCheckedChanged="checkBoxAppliedConfirmation_CheckedChanged" />--%>
                                    <asp:Button ID="btnConfirmation" class="btn btn-secondary btn-lg" runat="server" Text="Confirmed" OnClick="btnConfirmation_Click" Visible="true" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>



                    </asp:GridView>

                </div>
            </div> 
    <style>
    tbody, td, tfoot, th, thead, tr {
        width: 50%;
        font-size: 1.05em;
        padding: 10px;
        border-color: #3D0859;
        border-style: solid;
        border-width: 3px !important;
    } </style>

Code 1 image: enter image description here代码 1 图片:在此处输入图片描述

Code 2:代码 2:

    <div class="row">
    <div class="col-md-10 mx-auto">
        <div class="grid">
                <asp:GridView ID="gridviewAnnouncement" runat="server" AutoGenerateColumns="false" Width="100%"
                    AutoGenerateSelectButton="true" OnSelectedIndexChanged="gridviewAnnouncement_SelectedIndexChanged" 
                    GridLines="Horizontal" BorderStyle="Solid">
                    <Columns>
                        <asp:BoundField DataField="Header" HeaderText="Announcement" HtmlEncode="false" /> 
                        <asp:TemplateField ShowHeader="false">
                            <ItemTemplate>

                                <asp:Button type="button" class="btn btn-secondary" runat="server" ID="btnInterested" Text="Interested" 
                                    OnClick="btnInterested_Click" Visible="false"/>
                            </ItemTemplate>
                            </asp:TemplateField>
                    </Columns>
                </asp:GridView>
          </div>
            </div>
</div>

Code 2 image: enter image description here代码 2 图片:在此处输入图片描述

Well, if we drop in a gridview, say like this:好吧,如果我们插入一个 gridview,这样说:

    <div style="padding:25px;width:60%">
        <asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" OnRowDataBound="GHotels_RowDataBound" >
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName"   />
                <asp:BoundField DataField="City" HeaderText="City"           />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
                <asp:BoundField DataField="Description" HeaderText="Description" />

                <asp:TemplateField HeaderText="Gender">
                    <ItemTemplate>
                    <asp:RadioButtonList ID="RGender" runat="server" 
                        RepeatDirection="Horizontal" 
                        Text='<%# Eval("Gender") %>' 
                        AutoPostBack="true"
                        OnSelectedIndexChanged="RGender_SelectedIndexChanged"
                        >
                        <asp:ListItem>M</asp:ListItem>
                        <asp:ListItem>F</asp:ListItem>                            
                    </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Option">
                    <ItemTemplate>
                    <asp:DropDownList ID="cboOption" runat="server" 
                        DataValueField="ID"
                        DataTextField="Feature" >
                    </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

And this code:这段代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GHotels.DataSource = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName");
            GHotels.DataBind();
        }
    }

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

we get this:我们得到这个:

在此处输入图像描述

But, if we want to bootstrap the Gv, then just set it to the class "table".但是,如果我们想引导 Gv,则只需将其设置为 class“表”。

在此处输入图像描述

and, it now looks rather nice:而且,它现在看起来相当不错:

在此处输入图像描述

Note how much nicer the spacing is.请注意间距有多好。 And if I set the div to 100%, then the grid looks VERY nice and will span the whole page (the GV in fact becomes "responsive" when you do this.如果我将 div 设置为 100%,那么网格看起来非常漂亮并且会跨越整个页面(当你这样做时 GV 实际上会变得“响应式”。

And there no reason why you can't say add css styling to any control.并且没有理由不能说向任何控件添加 css 样式。 So, for example, lets style the radio button with Css, say:因此,例如,让单选按钮的样式为 Css,例如:

在此处输入图像描述

So, now the gv is responsive (sizes to browser size), and I even added css to the radio buttons.所以,现在 gv 是响应式的(大小为浏览器大小),我什至将 css 添加到单选按钮。

So, you can apply booth strap and css to a gridview - quite much like any other web site.因此,您可以将 booth strap 和 css 应用于 gridview - 与任何其他 web 站点非常相似。

HOWEVER - having stated the above?但是 - 已经说明了以上内容?

The layout abilities of the GV is limited. GV 的布局能力有限。

if you want the mother of all layout options - and quite near unlimited flexibility?如果您想要所有布局选项之母 - 并且非常接近无限的灵活性?

Then adopt the ListView.然后采用ListView。 They render as a table, but have significant advantages:它们呈现为表格,但具有显着优势:

They are:他们是:

All the controls don't need to be put inside of that messy templatefield.不需要将所有控件都放在那个凌乱的模板字段中。 I mean, maybe for 4-8 controls - sure, use the GV.我的意思是,也许对于 4-8 个控件——当然,使用 GV。 But after that, and ALSO wanting custom layout, and ALSO wanting to use plane jane asp.net controls?但在那之后,还想要自定义布局,还想使用 plane jane asp.net 控件?

Then HANDS down, the ListView control is 100x better.然后放下手,ListView 控件好 100 倍。 Not only is it better, but as stated, you don't have to bother with template fields, and EVEN better is not only can you drop in plane jane markup - including divs etc.?它不仅更好,而且如前所述,您不必为模板字段而烦恼,更好的是您不仅可以放入平面简标记 - 包括 div 等? But you also can style the markup with much greater ease.但您也可以更轻松地设置标记样式。

So, converting the above to a listview, we get this:所以,将上面的转换为列表视图,我们得到:

Note that I did use the wizard and a "on page" data source to create this listview.请注意,我确实使用了向导和“页面上”数据源来创建此列表视图。 Then I go delete crazy on the lv, and remove ALL OF the templates.然后我 go 在 lv 上疯狂删除,并删除所有模板。

So, this took me no time flat to build (for this post on so).所以,这让我没有时间去构建(对于这篇文章来说)。

So, I now have this:所以,我现在有这个:

    <div style="padding:20px;width:60%">
        <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" OnItemDataBound="ListView1_ItemDataBound" >

            <ItemTemplate>
                <tr style="">
                    <td><asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /></td>
                    <td><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' /></td>
                    <td><asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' /></td>
                    <td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
                    <td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
                    <td><asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /></td>

                    <td>
                        <asp:RadioButtonList ID="RGender" runat="server" 
                            RepeatDirection="Horizontal" 
                            Text='<%# Eval("Gender") %>' 
                            AutoPostBack="true"
                            OnSelectedIndexChanged="RGender_SelectedIndexChanged"
                             CssClass="rMyChoice"  >
                            <asp:ListItem>M</asp:ListItem>
                            <asp:ListItem>F</asp:ListItem>                            
                        </asp:RadioButtonList>
                    </td>
                    <td>
                        <asp:DropDownList ID="cboOption" runat="server" 
                            DataValueField="ID"
                            DataTextField="Feature" >
                        </asp:DropDownList>
                    </td>
                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table id="itemPlaceholderContainer" runat="server" border="0" class="table">
                    <tr runat="server" style="">
                        <th runat="server">ID</th>
                        <th runat="server">FirstName</th>
                        <th runat="server">LastName</th>
                        <th runat="server">City</th>
                        <th runat="server">HotelName</th>
                        <th runat="server">Description</th>
                        <th runat="server">Gender</th>
                        <th runat="server">HotelOption</th>
                    </tr>
                    <tr id="itemPlaceholder" runat="server">
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:ListView>

If you look close, it NOT really more markup - and in fact it rather clean.如果仔细观察,它实际上并没有更多的标记——事实上它相当干净。

We now get this:我们现在得到这个:

在此处输入图像描述

but, here is where the fun comes:但是,这就是乐趣所在:

You can see this is mostly just markup - so we can modify this.你可以看到这主要是标记 - 所以我们可以修改它。 Say, more like a card view of some sort:比如说,更像是某种卡片视图:

However, we can use markup now.但是,我们现在可以使用标记。 Say we move description and hotel to the same column,假设我们将描述和酒店移动到同一列,

Like this:像这样:

            <ItemTemplate>
                <tr style="">
                    <td>
                        
                     <asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>'
                         Font-Size="Large"/>
                    <br />
                     <asp:Label ID="DescriptionLabel" runat="server" 
                         Text='<%# Eval("Description") %>'
                         Font-Italic="true" />
                    </td>
                    <td><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' /></td>

And now:现在:

            <LayoutTemplate>
                <table id="itemPlaceholderContainer" runat="server" border="0" class="table">
                    <tr runat="server" style="">
                        <th runat="server">HotelName</th>
                        <th runat="server">FirstName</th>
                        <th runat="server">LastName</th>
                        <th runat="server">City</th>
                        <th runat="server">Gender</th>
                        <th runat="server">HotelOption</th>
                    </tr>
                    <tr id="itemPlaceholder" runat="server">
                    </tr>
                </table>
            </LayoutTemplate>

So, we now get this:所以,我们现在得到这个:

在此处输入图像描述

So, note how we can add as much information, or layout to each section.因此,请注意我们如何向每个部分添加尽可能多的信息或布局。

Sometimes a repeater might be better, but ListView tends to be the best choice WHEN you start to need fancy layout, or things beyond a simple grid.有时中继器可能更好,但当您开始需要花哨的布局或简单网格以外的东西时,ListView 往往是最佳选择。

I could also say move the First/Last Name over to the left side, maybe add a picture of the hotel - it don't matter.我还可以说将名字/姓氏移到左侧,也许添加一张酒店的图片 - 没关系。

So, as above shows:所以,如上所示:

Even adding css styles to the grid view and controls is 100% possbile and supported.甚至将 css styles 添加到网格视图和控件也是 100% 可能并受支持的。

But, using the ListView means you using plane jane asp.net controls - no messy template tags, and as you can see, you quite much free to put any kind of markup in each column - including divs or whatever floats your boat.但是,使用 ListView 意味着您使用 plane jane asp.net 控件 - 没有凌乱的模板标签,正如您所见,您可以非常自由地在每一列中放置任何类型的标记 - 包括 div 或任何漂浮在船上的东西。

so, gridview is fine, can be styled with css. However, the INSTANT you go beyond simple grid layouts, then Listview is far better, winds up with less markup, no templated stuff, and tends to support not only more layout options, but also is more HTML friendly.所以,gridview 很好,可以用 css 设置样式。但是,INSTANT 你 go 除了简单的网格布局,然后 Listview 要好得多,最后标记更少,没有模板化的东西,并且往往不仅支持更多的布局选项,而且也比较友好 HTML。

And for simple, the GV wins, but even above, you can see the total amount of markup for the listview was not much less.简单来说,GV 获胜,但即使在上方,您也可以看到列表视图的标记总量并不少。 GV wins if you let it auto generate columns or just have a few.如果您让它自动生成列或仅生成一些列,GV 将获胜。 But when you go custom layout and want to use many different standard asp.net controls, the Listview starts to win big time as above shows.但是,当您 go 自定义布局并希望使用许多不同的标准 asp.net 控件时,Listview 开始大获全胜,如上所示。

Same goes for first name, last name and gender.名字、姓氏和性别也是如此。

We can just "bunch" them up into the markup for the one column.我们可以将它们“捆绑”到一列的标记中。 Say like this:像这样说:

                <td>
                    <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
                    <asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
                    <br />
                    <asp:RadioButtonList ID="RGender" runat="server" 
                        RepeatDirection="Horizontal" 
                        Text='<%# Eval("Gender") %>' 
                        AutoPostBack="true"
                        OnSelectedIndexChanged="RGender_SelectedIndexChanged"
                         CssClass="rMyChoice"  >
                        <asp:ListItem>M</asp:ListItem>
                        <asp:ListItem>F</asp:ListItem>                            
                    </asp:RadioButtonList>
                </td>
                <td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>

Now, remove the last name column - and gender.现在,删除姓氏列和性别。 So we have this:所以我们有这个:

        <LayoutTemplate>
            <table id="itemPlaceholderContainer" runat="server" border="0" class="table">
                <tr runat="server" style="">
                    <th runat="server">HotelName</th>
                    <th runat="server" style="width:120px">Guest Name</th>
                    <th runat="server">City</th>
                    <th runat="server">HotelOption</th>
                </tr>
                <tr id="itemPlaceholder" runat="server">
                </tr>
            </table>
        </LayoutTemplate>

And now we have this:现在我们有了这个:

在此处输入图像描述

So we could actually keep bunching up controls - but nice how I was able to just move the markup from one column shove it into "one" column.因此,我们实际上可以继续组合控件 - 但很高兴我能够将标记从一列移动到“一”列。

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

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