asp.net 当页面加载时如何计算网格行大小?

inn6fuwd  于 2023-08-08  发布在  .NET
关注(0)|答案(2)|浏览(109)

我想设置一个div的可见性根据网格的大小是在同一页上;打开页面时加载网格如何执行此操作?我写了下面的js,但有错误。

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" ShowHeader="false" ShowFooter="false"
        CssClass="table table-bordered table-condensed"
        EmptyDataText="Kayıtlı İş Tecrübesi Bilgisi Bulunmamaktadır."
        OnRowDataBound="OnGridRowDataBound" OnRowCommand="OnGridRowCommand" OnRowCreated="OnGridRowCreated"
        DataKeyNames="KAYITNO"
        RowStyle-CssClass="ozgecmis-grid-row" AlternatingRowStyle-CssClass="ozgecmis-grid-row-alternate" SelectedRowStyle-CssClass="ozgecmis-grid-row-selected">
        <Columns>
            <asp:TemplateField ItemStyle-CssClass="ozgecmis-data-key">
                <ItemTemplate>
                    <asp:Button runat="server" ID="dataKeyButton" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"KAYITNO") %>' Visible="false" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-CssClass="ozgecmis-data-command" ItemStyle-VerticalAlign="Middle">
                <ItemTemplate>
                    <a href="#" class="btn btn-small btn-info">Seç</a>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-CssClass="ozgecmis-data-text">
                <ItemTemplate>
                    <%#FormatData(Eval("ISYERIUNVAN"),Eval("GIRISTARIHSTR"),Eval("CIKISTARIHSTR"),Eval("ISTANIMI"),Eval("SEKTOR"),Eval("POZISYON"),Eval("MESLEK"),Eval("MESLEKUZMANLIK")) %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


    $( "#grid" ).on( "load", function() {
        var gridView = document.getElementById("<%=grid.ClientID %>");
        var rows = gridView.getElementsByTagName("tr")
        if (rows.length>0){
            isTecrubesiBos.Visible = true;
        }
        } );

字符串

sg24os4d

sg24os4d1#

您的griddiv都是服务器端控件,当页面加载时,它们会在那里更改id。所以页面加载后意味着document.ready通过document.getElementById("<%= isTecrubesiBos.ClientID %>").style.display = 'block';显示你的div
范例:

$(function() {
  var gridView = document.getElementById("<%= grid.ClientID %>").rows.length;
  if (gridView.length > 0) {
     document.getElementById("<%= isTecrubesiBos.ClientID %>").style.display = 'block';
  }
});

字符串
如果有任何情况,需要隐藏div

document.getElementById("<%= isTecrubesiBos.ClientID %>").style.display = 'none';

km0tfn4u

km0tfn4u2#

在加载网格并设置其数据源时进行检查。
所以,说这个:

<asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" CssClass="table">
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
        <asp:BoundField DataField="LastName" HeaderText="LastName"    />
        <asp:BoundField DataField="HotelName" HeaderText="HotelName"  />
        <asp:TemplateField HeaderText="Active" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:CheckBox ID="chkActive" runat="server" 
                    Checked='<%# Eval("Active") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Description" HeaderText="Description"  />
        <asp:TemplateField HeaderText="Hotel Information" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Button ID="cmdView" runat="server" Text="Edit" CssClass="btn" 
                    OnClick="cmdView_Click" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:TextBox ID="isTecrubesiBos" runat="server"></asp:TextBox>

字符串
现在在代码中,你可以这样做:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        LoadGrid
    End If
End Sub

Sub LoadGrid()

    Dim rstData As DataTable
    rstData = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName")
    isTecrubesiBos.Visible = rstData.Rows.Count > 0
    GHotels.DataSource = rstData
    GHotels.DataBind()

End Sub


我们看到/看到这个:
x1c 0d1x的数据
因此,由于数据源的行数> 0,那么我们在该时间点可以自由设置所讨论的文本框的可见性。

相关问题