C# 正在运行时查找GridView生成的特定单元格。

C# 正在运行时查找GridView生成的特定单元格。,c#,javascript,jquery,asp.net,sql,C#,Javascript,Jquery,Asp.net,Sql,我正在使用UserControl(一个嵌套的GridView)创建一个墙,我想在向下滚动时加载GridView的行,就像facebook一样。我正在努力实现这个方法。。 该示例似乎读取最后一行的第一列,以获取加载的最后一行并获取下一组行。我在UserControl的GridView中添加了一个绑定字段,但我想我无法读取正确的值,因为我收到错误“输入字符串格式不正确”。在 这是我的用户控件的aspx代码 <asp:ScriptManager ID="sm1" runat="server"&g

我正在使用UserControl(一个嵌套的GridView)创建一个墙,我想在向下滚动时加载GridView的行,就像facebook一样。我正在努力实现这个方法。。 该示例似乎读取最后一行的第一列,以获取加载的最后一行并获取下一组行。我在UserControl的GridView中添加了一个绑定字段,但我想我无法读取正确的值,因为我收到错误“输入字符串格式不正确”。在

这是我的用户控件的aspx代码

<asp:ScriptManager ID="sm1" runat="server">
        <Scripts>
            <asp:ScriptReference Path="~/ScriptsScroll/jquery-1.4.1.js" />
        </Scripts>
    </asp:ScriptManager>

    <div id="divProducts" style="height:700px;overflow-x:hidden;overflow-y: scroll;">

<table cellpadding="0" cellspacing="1" border="0">
    <tr>
        <td align="left">
            <asp:GridView ID="GridViewUserScraps" ItemStyle-VerticalAlign="Top" AutoGenerateColumns="False"
                GridLines="None" Width="100%" ShowHeader="False" runat="server" AlternatingRowStyle-BackColor="#A5A5A5"
                CellPadding="4" ForeColor="#333333" DataKeyNames="ScrapId" OnRowCommand="GridViewRowCommand">
                <Columns>

                    <asp:BoundField DataField="id" />

                    <asp:TemplateField>

                        <FooterTemplate>

                            <asp:LinkButton ID="LinkButton1" runat="server">Load More</asp:LinkButton>

                        </FooterTemplate>
                        <ItemTemplate>
                            <table align="left" cellpadding="1" cellspacing="2">
                                <tr>
                                    <td>
                                        <a href='<%#getUserHREF(Container.DataItem)%>'>
                                            <img align="middle" src='<%#getSRC(Container.DataItem)%>' border="0" width="50px" /></a>
                                    </td>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                            <div align="justify">
                                <b>
                                    <%#DataBinder.Eval(Container.DataItem,"firstname")%>
                                </b>
                                <br />
                                <%#DataBinder.Eval(Container.DataItem, "Message")%>
                                <br />
                                <asp:Image ID="Image" runat="server"  ImageUrl='<%# Eval("url") %>' style="max-width:500px;" />
                            </div>
                            <span class="SmallBlackText">Posted On: &nbsp;</span>
                            <asp:Label ID="lblSendDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SendDate")%>'></asp:Label>
                            </span>
                            <br />
                            <%-- <asp:LinkButton ID="lnklike" runat="server" 
                                CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" 
                                CommandName="LikeCmd">Like</asp:LinkButton>
                            &nbsp;&nbsp;&nbsp;
                            <asp:LinkButton ID="lnkunlike" runat="server">unlike</asp:LinkButton>--%>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;
                            <asp:ImageButton ID="lnklike" runat="server" ImageUrl="~/Images/thumbsup.png" Height="20px"
                                Width="20px" CommandName="like" CommandArgument='<%# Eval("ScrapId")%>' />
                            <asp:UpdatePanel runat="server" ID="UpdatePanel" UpdateMode="Conditional">
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="lnklike" EventName="Click" />
                                </Triggers>
                                <ContentTemplate>
                                    &nbsp;<asp:Label ID="Label1" runat="server" Text='<%# Controls_GetUserScraps.abc((int)Eval("ScrapId")) %>' />
                                </ContentTemplate>
                            </asp:UpdatePanel>
                            <%--<asp:Label ID="Label1" runat="server" Text='<%# WebPageName.StaticMethodName((int)Eval("ScrapId")) %>' />--%>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:ImageButton ID="lnkunlike" runat="server" CommandArgument='<%# Eval("ScrapId")%>'
                                CommandName="unlike" Height="20px" ImageUrl="~/Images/thumbsdown.png" Width="20px" />
                            <asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="lnkunlike" EventName="Click" />
                                </Triggers>
                                <ContentTemplate>
                                    &nbsp;<asp:Label ID="Label2" runat="server" Text='<%# Controls_GetUserScraps.xyz((int)Eval("ScrapId")) %>'></asp:Label>
                                    &nbsp;&nbsp;
                                </ContentTemplate>
                            </asp:UpdatePanel>
                            <asp:LinkButton ID="lnkcomment" runat="server">Comment</asp:LinkButton>
                            &nbsp;&nbsp;
                            <asp:LinkButton ID="lnkviewall" runat="server" CommandName="viewall" CommandArgument='<%# Eval("ScrapId")%>'>View All</asp:LinkButton>
                            <br />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:Panel ID="Pnlchildgrid" runat="server">
                                <asp:GridView ID="childgrid" runat="server" AutoGenerateColumns="False" OnRowCommand="childgrid_RowCommand"
                                    Style="width: 450px; float: right;">
                                    <Columns>
                                        <asp:TemplateField>

                                            <ItemTemplate>
                                                <table align="left" cellpadding="1" cellspacing="2">
                                                    <tr>
                                                        <td>
                                                            <a href='<%#getUserHREF(Container.DataItem)%>'>
                                                                <img align="middle" src='<%#getSRC(Container.DataItem)%>' border="0" width="30px" /></a>
                                                        </td>
                                                        <td>
                                                            &nbsp;
                                                        </td>
                                                    </tr>
                                                </table>
                                                <div align="justify">
                                                    <b>
                                                        <%#DataBinder.Eval(Container.DataItem, "firstname")%></b><br />
                                                    <%#DataBinder.Eval(Container.DataItem, "commentmsg")%>
                                                    <br />
                                                </div>
                                                <span class="SmallBlackText">Posted On: &nbsp;</span>
                                                <asp:Label ID="lblSendDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SendDate")%>'></asp:Label>
                                                </span>
                                                <br />
                                                <asp:ImageButton ID="lnklike" runat="server" Height="20" Width="20" CommandName="childlike"
                                                    CommandArgument='<%# Eval("commentid")%>' ImageUrl="~/Images/thumbsup.png" />
                                                &nbsp;
                                                <asp:Label ID="Label1" runat="server" Text='<%# Controls_GetUserScraps.abc1((int)Eval("commentid")) %>' />
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                <asp:ImageButton ID="lnkunlike" runat="server" CommandArgument='<%# Eval("commentid")%>'
                                                    CommandName="childunlike" Height="20" ImageUrl="~/Images/thumbsdown.png" Width="20" />
                                                &nbsp;<asp:Label ID="Label2" runat="server" Text='<%# Controls_GetUserScraps.xyz1((int)Eval("commentid")) %>'></asp:Label>
                                                &nbsp;&nbsp;
                                                <%--  <asp:LinkButton ID="lnkcomment" runat="server">Comment11111</asp:LinkButton>--%>
                                                <br />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                                <br />
                                <asp:Panel ID="Panel1" runat="server">

                                    <asp:TextBox ID="txtcomment" runat="server" Width="500px" ></asp:TextBox>
                                    <asp:Button ID="Button1" runat="server" Text="Comment" CommandName="comment" CommandArgument='<%# Eval("ScrapId")%>' />
                                </asp:Panel>
                            </asp:Panel>
                            <br />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <RowStyle BackColor="#EFF3FB" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </td>
    </tr>
    <tr>
        <td align="left">
            &nbsp;
        </td>
    </tr>
</table>
</div>

<div><asp:HiddenField runat="server" ID="hiddenLastProductID" />
        <asp:Button runat="server" Text="Get More records" ID="btnGetMoreRecords" 
            onclick="btnGetMoreRecords_Click" />
    </div>
    <div id="divProgress" style="margin-top: -50px;margin-left:150px;z-index:-999">
            <img src="../loading.gif" width="100" height="100" alt="" />
    </div>
    <div>
    </div>
    <script type="text/javascript">

        var previousProductId = 0;
        //Max records to display at a time in the grid.
        var maxRecordsToDisplay = 30;

        $(document).ready(function () {

            //initially hide the loading gif
            $("#divProgress").hide();

            //initially hide the button
            $("#btnGetMoreRecords").hide();

            //Attach function to the scroll event of the div
            $("#divProducts").scroll(function () {
                var scrolltop = $('#divProducts').attr('scrollTop');
                var scrollheight = $('#divProducts').attr('scrollHeight');
                var windowheight = $('#divProducts').attr('clientHeight');
                var scrolloffset = 20;
                if (scrolltop >= (scrollheight - (windowheight + scrolloffset))) {

                    //User has scrolled to the end of the grid. Load new data..
                    $("#divProgress").ajaxStart(function () {
                        $(this).show();
                    });
                    $("#divProgress").ajaxStop(function () {
                        $(this).hide();
                    });
                    BindNewData();

                }
            });

        });
        function BindNewData() {
            //           
            var lastProductId = $("#<%=GridViewUserScraps.ClientID %> tr:last").children("td:first").html();

            //get last table row in order to append the new products
            var lastRow = $("#<%=GridViewUserScraps.ClientID %> tr:last");

            //Fetch records only when the no. of records displayed in the grid are less than limit.
            if (GetRowsCount() < maxRecordsToDisplay) {
                if (parseInt(lastProductId, 10) > parseInt(previousProductId, 10)) {
                    previousProductId = lastProductId;

                    $.post("FetchRecordsHandler.ashx?lastProductId=" + lastProductId, function (data) {
                        if (data != null) {
                            //append new products rows to last row in the gridview.
                            lastRow.after(data);
                        }
                    });
                }
            }
            else {

                //Set value of last product id in hidden field so that we can access it from code behind.
                $("#hiddenLastProductID").val(lastProductId);
                //Check If there is more records in the database
                if (parseInt(lastProductId, 10) > parseInt(previousProductId, 10))
                    $("#btnGetMoreRecords").show();
            }


        }

        function GetRowsCount() {
            //Count no. of rows except header row in the grid.
            //var rowCount = $('#GridView1 tr').length - 1;
            var rowCount = $('[id*=GridViewUserScraps]  tr');
            return rowCount;

        }
    </script>





张贴于:



var previousProductId=0; //网格中每次显示的最大记录数。 var maxRecordsToDisplay=30; $(文档).ready(函数(){ //最初隐藏正在加载的gif $(“#divProgress”).hide(); //最初隐藏按钮 $(“#btnGetMoreRecords”).hide(); //将函数附加到div的滚动事件 $(“#divProducts”)。滚动(函数(){ var scrolltop=$('#divProducts').attr('scrolltop'); var scrollheight=$('#divProducts').attr('scrollheight'); var windowheight=$('divProducts').attr('clientHeight'); var scrolloffset=20; 如果(scrolltop>=(scrollheight-(windowheight+scrolloffset))){ //用户已滚动到网格的末尾。加载新数据。。 $(“#divProgress”).ajaxStart(函数(){ $(this.show(); }); $(“#divProgress”).ajaxStop(函数(){ $(this.hide(); }); BindNewData(); } }); }); 函数BindNewData(){ // var lastProductId=$(“#tr:last”).children(“td:first”).html(); //获取表的最后一行以追加新产品 var lastRow=$(“#tr:last”); //仅当网格中显示的记录数小于限制时才能获取记录。 如果(GetRowsCount()parseInt(previousProductId,10)){ previousProductId=lastProductId; $.post(“FetchRecordsHandler.ashx?lastProductId=“+lastProductId,函数(数据)){ 如果(数据!=null){ //将新产品行追加到gridview中的最后一行。 lastRow.after(数据); } }); } } 否则{ //在隐藏字段中设置最后一个产品id的值,以便我们可以从代码隐藏中访问它。 $(“#hiddenLastProductID”).val(lastProductId); //检查数据库中是否有更多记录 if(parseInt(lastProductId,10)>parseInt(previousProductId,10)) $(“#btnGetMoreRecords”).show(); } } 函数GetRowsCount(){ //计算网格中除标题行以外的行数。 //var rowCount=$('#GridView1 tr')。长度-1; var rowCount=$('[id*=GridViewUserScraps]tr'); 返回行计数; }
作为第一件事,我会尝试更改此js行

$("#hiddenLastProductID").val(lastProductId);


$("#hiddenLastProductID").val(lastProductId);
$("#<%= hiddenLastProductID.ClientID %>").val(lastProductId);
<asp:HiddenField runat="server" ID="hiddenLastProductID" ClientIDMode="Static" />