简体   繁体   English

如何计算在asp.net c#和mysql数据库中发布的视图数量

[英]how to calculate number of views to post in asp.net c# and mysql database

how to calculate number of views to post in asp.net c# and mysql database. 如何计算在asp.net c#和mysql数据库中发布的视图数量。

I am working on one social networking site. 我正在一个社交网站上工作。 I create User control with Gridview which contain labels, textboxs to show user's Name Post title Post main contain. 我使用Gridview创建用户控件,其中包含标签,文本框以显示用户的名称帖子标题帖子主要包含。 Other viewers can do like on that post. 其他观众可以在该帖子上做。 On Gridview I also want to show number of views to that particular post. 在Gridview上我还想显示该特定帖子的观看次数。 Could you please suggest me how to achieve this. 你能建议我如何实现这个目标。 I also want to get know, How to update views column values for that particular Post in post table. 我还想知道,如何更新post表中特定Post的视图列值。 because this query to update will need to run only on server side. 因为要更新的查询只需要在服务器端运行。 So how to achieve this too(run query on server where site hosted after user view on that post to update views in table). 那么如何实现这一点(在该帖子上的用户视图之后托管站点的服务器上运行查询以更新表中的视图)。

I tried with to view number of visitor but it's show only current session users on page not on post. 我试着查看访问者的数量,但它只在页面上显示当前会话用户而不是帖子。 (in my case page contain GridView which bind with databse to show various post posted by user ) (在我的案例页面中包含与数据库绑定的GridView,以显示用户发布的各种帖子)

void Session_Start(object sender, EventArgs e)
{
    Application.Lock();
    Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
    Application.UnLock();
}

this is my Gridview template, currently i am showing 10k views hard-coded : 这是我的Gridview模板,目前我正在显示硬编码的10k视图:

<asp:GridView ID="GridViewUserScraps2" runat="server" ItemStyle-VerticalAlign="Top" AutoGenerateColumns="False" GridLines="None" Width="100%" ShowHeader="False" runat="server" CellPadding="4" OnSelectedIndexChanged="GridViewUserScraps1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <div class="feed">
                    <div class="box-2">
                        <div class="box-inner">
                            <div class="row">
                                <div class="col-1">
                                    <div class="profile-img">
                                        <%--  getnewspostuserpic(object imgSRC)--%>
                                        <asp:Image ID="Image1" runat="server" src='<%#getnewspostuserpic(Container.DataItem)%>' ImageUrl='<%# DataBinder.Eval(Container.DataItem,"ImageName") %>' class="img-fluid mx-auto d-block"/>    <%--<img src="images/user-profile-img.png" class="img-fluid mx-auto d-block">--%>
                                    </div>
                                </div>
                                <div class="col-11">
                                    <div class="user-name pl-2">
                                        <h5 class="txt-black semi-bold pb-1 pt-1"><asp:HyperLink ID="HyperLink1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'></asp:HyperLink><%--Mayur Danej--%></h5>
                                        <p class="txt-light-grey light">  <asp:Label ID="lblSendDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"SendDate") %>'></asp:Label><%--10 May 2018--%></p>   
                                    </div>
                                </div>
                                <h5 class="txt-black semi-bold pb-1 pt-3"> <asp:Label ID="IdeasTitle" runat="server"  Text='<%# Eval("IdeasTitle") %>' visible="true"></asp:Label></h5>
                            </div>
                            <div class="clear20"></div>
                            <asp:TextBox ID="post" runat="server" class="form-control"  CommandArgument="<%#Container.DataItemIndex %>"  Text=' <%#DataBinder.Eval(Container.DataItem, "Message")%>' Font-Size="Smaller" TextMode="MultiLine"  Enabled="true"    OnCommand="txtSuggestion_Command"   ReadOnly="True"  ToolTip="Extend at corner to Read All if Containt not visible" ></asp:TextBox>
                            <div class="col-6">
                                <div class="col-4">
                                    <asp:Button ID="btnReadMore" class="readmore txt-blue text-right"  CommandName="Select" CommandArgument="<%#Container.DataItemIndex %>" Text="Read More..." runat="server" OnCommand="txtSuggestion_Command" ></asp:Button >
                                </div>
                            </div>
                            <%--<p>Lorem ipsum dolor sit amet, <a href="#">consectetur</a> adipiscing elit. Nunc luctus orci sit amet feugiat volutpat. Ut ac rutrum elit. Aliquam molestie quam ac <a href="#">lectus volutpat</a> scelerisque.</p>--%>
                        </div>
                        <div class="inner-img">
                            <img align="middle" id="imgpost" src='<%#getSRC1(Container.DataItem)%>' runat="server" style='<%# CheckVisibility(DataBinder.Eval(Container.DataItem,"imageurl")) %>' class="img-fluid mx-auto" alt="Something wrong with this image" />  <%--  <img src="images/inner-img.jpeg" class="img-fluid mx-auto d-block">--%>
                        </div>
                        <div class="box-inner">
                          <%--<p>Nulla et iaculis massa, sed pellentesque ex. Morbi et turpis ut urna dictum laoreet eget vitae odio. Nunc eget ex sagittis, tincidunt tortor a, mollis elit. Maecenas leo mi, viverra eu accumsan ac, dapibus non leo. Nunc vitae dapibus ligula, sit amet malesuada velit. Fusce ut condimentum tellus, quis ultrices orci. Mauris sagittis vulputate dui, non elementum nibh lacinia et. Nullam vehicula sem et lacus fermentum rhoncus.</p>--%>
                        </div>
                        <div class="clear30"></div>
                        <div class="like-share">
                            <div class="row">
                                <div class="col-8">
                                    <p class="txt-light-grey inline">
                                        <i class="fa fa-eye txt-blue pr-2 fs18"></i>10k Views |  
                                        <span><asp:Button ID="ButtonDos" class="unlike-btn txt-red pr-2"  CommandName="Select" CommandArgument="<%#Container.DataItemIndex %>" Text="" runat="server" OnCommand="ButtonDos_Command"> </asp:Button ></span>  
                                        <span><asp:Label ID="postLIDDO"  runat="server" Text='<%# Eval("do") %>' Visible="true"></asp:Label>
                                            <asp:Label ID="postLID" runat="server"  Text='<%# Eval("ScrapId") %>' Visible="false"></asp:Label>
                                            <asp:Label ID="like" runat="server"  Text='<%# Eval("doset") %>' Visible="true"></asp:Label>
                                            <asp:Label ID="unlike" runat="server"  Text='<%# Eval("dontset") %>' Visible="true"></asp:Label>
                                            <h5 class="txt-black semi-bold pb-1 pt-1"><asp:HyperLink ID="lnkLocations" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Locations") %>'></asp:HyperLink></h5>
                                            <h5 class="txt-black semi-bold pb-1 pt-1"><asp:HyperLink ID="lnkTags" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Tags") %>'></asp:HyperLink></h5>
                                        </span>
                                        <%--102 Likes --%><%-- <i class="fa fa-commenting txt-teal pr-2"></i> 25 Comments</p>--%></p>
                                    </div>
                                    <div class="col-4">
                                        <p class="semi-bold txt-blue text-right"><i class="fa fa-share-alt pr-2"></i> Share
                                            <asp:Button ID="Button1" class="readmore txt-blue text-right"  CommandName="Select" CommandArgument="<%#Container.DataItemIndex %>" Text="Shares..." runat="server" OnCommand="share_Command" > </asp:Button >
                                            <div id="fb-root"></div>
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="clear40"></div>

                    </div>
                    <%-- <a href='<%#getUserHREF1(Container.DataItem)%>'>
                    <img align="middle" src='<%#getSRC1(Container.DataItem)%>' class="img-responsive center-block" style='<%# CheckVisibility(DataBinder.Eval(Container.DataItem,"imageurl")) %>; width: 206px; height: 149px;'  border="0" ID="kk" /></a>--%>

                </ItemTemplate>
            </asp:TemplateField>
        <asp:BoundField />
    </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" />
</asp:GridView>

Now i am able to increase views using javascript as shown in answer but i want to get that views value from javascript in my code behind so that i can use it to save in variable in .cs file code behind . 现在我能够使用javascript增加视图,如答案中所示,但我希望从我的代码后面的javascript中获取视图值,以便我可以使用它来保存在.cs文件代码中的变量。

you can do this with javascript from code behind as follows: 您可以使用后面的代码中的javascript执行此操作,如下所示:

Considering Label1 is control in gridview which show view's count on mouse over on it. 考虑Label1是gridview中的控件,它显示视图在鼠标上的计数。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {  
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                

                Label lit = e.Row.FindControl("Label1") as Label;               

                e.Row.Attributes.Add("onmouseover", String.Format("javascript:var d=document.getElementById('{0}'); d.innerHTML = parseInt(d.innerHTML) + 1; return false;", lit.ClientID));

                e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

            }    

        }

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

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