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

huangapple go评论72阅读模式
英文:

How to count grid row size when the page is loaded?

问题

我想根据同一页上的网格的大小设置一个 div 的可见性;当页面打开时加载网格。我该怎么做?我写了以下的 js 但是出现了错误。

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

I want to set visibility of a div according to the size of grid which are on the same page; grid is loaded when the page is opened How can I do this ? I wrote following js but took errors.

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



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

答案1

得分: 1

Your grid and div both are server side control, they change their id when page load. So after page load means document.ready show your div via document.getElementById("isTecrubesiBos").style.display = 'block'; .

Example:

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

If in any case, you need to hide div:

document.getElementById("isTecrubesiBos").style.display = 'none';
英文:

Your grid and div both are server side control, they change there id when page load. So after page load means document.ready show your div via document.getElementById(&quot;&lt;%= isTecrubesiBos.ClientID %&gt;&quot;).style.display = &#39;block&#39;; .

Example:

$(function() {
  var gridView = document.getElementById(&quot;&lt;%= grid.ClientID %&gt;&quot;).rows.length;
  if (gridView.length &gt; 0) {
     document.getElementById(&quot;&lt;%= isTecrubesiBos.ClientID %&gt;&quot;).style.display = &#39;block&#39;;
  }
});

If any case, need to hide div :

 document.getElementById(&quot;&lt;%= isTecrubesiBos.ClientID %&gt;&quot;).style.display = &#39;none&#39;;

答案2

得分: 0

在加载网格并设置其数据源时执行此检查。

所以,可以这样写:

<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

因此,我们在此时可以根据数据源的行数是否大于0来设置文本框的可见性。

英文:

Do that check WHEN you load the grid and set its data source.

So, say this:

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

&lt;asp:TextBox ID=&quot;isTecrubesiBos&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;

And now in code, you can do this:

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(&quot;SELECT * FROM tblHotelsA ORDER BY HotelName&quot;)
    isTecrubesiBos.Visible = rstData.Rows.Count &gt; 0
    GHotels.DataSource = rstData
    GHotels.DataBind()

End Sub

So, we get/see this:

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

So, since the data source has a row count > 0, then we at that point in time are free to set the visible of the text box in question.

huangapple
  • 本文由 发表于 2023年7月6日 18:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627801.html