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

时间:2019-06-23 12:01:55

标签: c# mysql asp.net

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

我正在一个社交网站上工作。我使用Gridview创建用户控件,其中包含标签,文本框以显示用户的名称帖子标题帖子主要包含。其他查看者可以在该帖子上喜欢。在Gridview上,我还想显示该特定帖子的视图数。您能否建议我如何实现这一目标。 我也想知道,如何更新帖子表中特定帖子的视图列值。因为此更新查询仅需要在服务器端运行。因此,如何实现此目标(在用户查看该帖子后,在托管网站的服务器上运行查询以更新表中的视图)。

我尝试查看访问者的数量,但是页面上仅显示当前会话用户,而不显示页面。 (在我的情况下,该页面包含GridView,该视图与databse绑定以显示用户发布的各种帖子)

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

这是我的Gridview模板,目前我正在显示1万个硬编码的视图:

<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>

现在,我可以使用答案中所示的javascript来增加视图,但是我想从后面的代码中的javascript中获取该视图的值,以便我可以将其保存在后面的.cs文件代码中的变量中。

1 个答案:

答案 0 :(得分:0)

您可以使用javascript从下面的代码中执行以下操作:

考虑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)");

            }    

        }