如何为每个帖子附上一个时间?

时间:2014-09-12 05:06:39

标签: asp.net c#-4.0 timer aspxgridview

我试图将经过的时间附加到我的gridview控件上显示的每个评论帖子中。这是我现在正在处理的评论部分页面的一部分。

所以我想出了使用ajax定时器控件,并在虚拟页面中对我的Timer_Tick()事件处理程序进行编码和测试,以创建一个运行时钟,这样我就可以获得经过时间的值,如"就在几秒钟之前"," 8分钟前"," 2小时前","昨天上午10:00" 和等等。我想你现在弄清楚我的意思就像在Facebook上发帖一样,你会看到每个帖子的时间。我在虚拟页面上测试时只使用一个定时器控件并且工作正常我能够获得那些时间过去的值并且时钟运行良好。现在,当我在我的网格视图中嵌入计时器控件时,我遇到的问题就是我只遇到顶行将显示时间值,但前面的行是空的。因此,每次输入新评论时,只有最后一条评论将显示已运行的时间,其余评论为空。关于如何正确地做到这一点的任何建议?...谢谢

这是我的gridview的源代码:

<asp:GridView ID="gvParentGrid" runat="server" Width="395px" 
AutoGenerateColumns="False"  GridLines="None" BorderStyle="Solid" BorderWidth="0px" OnRowCommand="gvParentGrid_RowCommand"
            BorderColor="White" DataKeyNames="ID" onrowdatabound="gvParentGrid_RowDataBound"  EnableModelValidation="True"  >

<Columns>
<asp:TemplateField >
<ItemTemplate>

 <tr>
      <td id ="comment" onmouseover="highlightBG(this, '#CCCCFF');highlightNext(this, 'black')" onmouseout="highlightClear(this, 'white')" class ="highlightab" style ="border-bottom :2px solid gray;border-bottom-color :#C0C0C0; border-left :0px; border-left-color :White; border-right :0px; border-right-color :White; border-top :0px; border-top-color :White;background-color :White;border-bottom :2px solid gray; border-left :0px; border-left-color :White; border-right :0px; border-right-color :White; border-top :0px; border-top-color :#C0C0C0;background-color :White; height :100px; width :405px; margin-top:-5px; margin-bottom :0px">              
           <div id ="Closebtn" class ="Close" style="width:500px; display :none;"><asp:Button ID="Button3" runat="server" Text="X" style =" cursor:pointer; margin-left :470px; border:0px; background-color :White; color :blue; font-weight :bold; " ToolTip="Delete" /></div>

        <div style="width:435px">
            <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer>   <asp:Label ID="Label10" runat="server" Text="Time Elapsed/Date" CssClass="date" Font-Size="Smaller" Font-Names="Verdana"></asp:Label></div>

          <asp:Image ID="Image1" runat="server" style="  margin-right :5px; background-image :url('Image/blankpeople.png');"  ImageAlign ="Left" Height ="60px" Width="60px" />
            <asp:Label ID ="ComID" runat ="server" style="display :none" Text =' <%#Eval("ID") %>' />
           <asp:Label ID="name" runat="server" style="font-weight :bolder; color :Blue; "  Text='<%# Eval("Name")%>' ></asp:Label>
        <p id ="content" class="comment more" style ="font-family:Verdana; font-weight:normal;font-size:small; border-radius: 4px 4px 4px 4px; max-width :395px; min-height :5px; margin-top :5px; margin-bottom :0px; margin-left :65px; display :block; background-color: #CCCCFF;"> <%# DataBinder.Eval(Container.DataItem,"Comments").ToString().Replace("\n", "<br />") %> </p>
         <div style="width:435px;margin-left :65px;">
         <div style=" width:200px;float:left">
           <a href="JavaScript:divexpandcollapse('div<%# Eval("ID") %>');" style =" margin-top :1px; text-decoration :none " >
            <input id='btndiv<%# Eval("ID") %>' type="button" class ="btndivname" value="Reply" style =" border:0px; background-color :inherit; color :blue; cursor :pointer; "  />
          </a>  
            <asp:Label ID="Label8" runat="server" Text='<%#Eval("Like") %>'>' Font-Size="Smaller" Font-Names="Verdana"></asp:Label>
            <asp:ImageButton ID="ImageButton1" onclick="ImageButton1_Click" runat="server" ImageUrl="~/Image/thumb-up-icon.png"  ImageAlign="Top"  />
           &nbsp; &nbsp;
             <asp:Label ID="Label9" runat="server" Text='<%#Eval("Dislike") %>'>' Font-Size="Smaller" Font-Names="Verdana"></asp:Label> 
            <asp:ImageButton ID="ImageButton2" onclick="ImageButton2_Click" runat="server" ImageUrl="~/Image/thumb-down-icon.png"  ImageAlign="Top" />
        </div> 
          <div style="width:170px;float:right">
          &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:Label ID="Label7" runat="server" Text="Report As" Font-Size="X-Small" Font-Names="Verdana"></asp:Label> <asp:DropDownList ID="DropDownList1" runat="server" ToolTip="Report Comment As" Width="20px" AutoPostBack="True"> <asp:ListItem>Profanity</asp:ListItem>
          <asp:ListItem>Abused</asp:ListItem>
          <asp:ListItem>Threat </asp:ListItem>
          <asp:ListItem>Harassment</asp:ListItem>
         <asp:ListItem>Obscenity</asp:ListItem>
         <asp:ListItem>Copy Infringement </asp:ListItem> 
         <asp:ListItem>Child Pornography </asp:ListItem> 
         </asp:DropDownList>
           </div>  
        </div>  

     </td>    
 </tr>

</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>

  <!--This is the child gridview -->
<div id="div<%# Eval("ID") %>" style="display:none; position:relative; left: 65px; overflow: auto; margin-top :5px;margin-bottom:5px">
      <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
       <ContentTemplate> 
<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Double"  BorderColor="white" GridLines="None" Width="325px" onrowcommand="gvChildGrid_RowCommand"  EnableModelValidation="True" onrowdatabound="gvChildGrid_RowDataBound">
<Columns >
<asp:TemplateField >
<ItemTemplate >
<tr >
  <td style ="margin-top:0px; border-bottom :2px solid Blue;border-bottom-color :Gray; border-left :0px; border-left-color :White; border-right :0px; border-right-color :White; border-top :0px; border-top-color :White;background-color :White; height :100px; width :325px"> 
 <asp:Image ID="Image1" runat="server" style=" margin-right :5px; background-image :url('Image/imagebackground.png');"  ImageAlign ="Left" Height ="60px" Width="60px" />

 <asp:Label ID="name" runat="server"  Font-Bold="True" Text='<%# Eval("Name")%>' ForeColor="Blue"></asp:Label>

 <p id ="content2" class="comment more" style ="font-weight:lighter; font-size:xx-small; border-radius: 4px 4px 4px 4px; max-width :325px; min-height :5px; margin-top :5px; margin-left :65px; display :block; background-color: #CCCCFF; font-size :small"> <%# DataBinder.Eval(Container.DataItem,"Replies").ToString().Replace("\n", "<br />")  %> </p>

    </td>
</tr>

</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>

      <!-- This is the inputbox and the reply button -->
<asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>
   <br />
  <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  <br />
  <asp:TextBox ID="TextBox4" Height ="200px" Width ="325px"  TextMode ="MultiLine" CssClass="norez" runat="server" AutoPostBack="True"></asp:TextBox>
  <br />
      <div style="width:325px">
    <div style="float:left;width:100px">
  <asp:Button ID="Button2" runat="server" Text="Post Reply" OnClick="Button2_Click" OnClientClick="return ShowProgress2()" />
   </div>
        <div align="center" style="float:right;width:225px"> 
    <asp:UpdateProgress ID="UpdateProgress2" runat="server"  DisplayAfter="1" AssociatedUpdatePanelID="UpdatePanel2" >
    <ProgressTemplate>
        <img id="Img1" runat="server" src="Image/loading.gif" alt="wait image" style=" height:40px; width:40px; text-align:left" />&nbsp; <b>Processing Request...</b></ProgressTemplate>
    </asp:UpdateProgress>  
         </div>
               </div>
 </ContentTemplate>
  <Triggers>
  <asp:AsyncPostBackTrigger ControlID="Button2" EventName ="Click" />
</Triggers>  

    </asp:UpdatePanel>
</div>

</ItemTemplate>
</asp:TemplateField>
</Columns>

</asp:GridView>

1 个答案:

答案 0 :(得分:0)

您需要在sqpecific时间间隔后重新填充网格控件,以刷新gridview.Put中的数据,从这样的网格视图控件中取出并重新绑定Timer_Tick事件上的网格数据。

 <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="5000" Enabled="true">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
        <asp:GridView ID="gvParentGrid" runat="server" Width="395px" AutoGenerateColumns="False"
            GridLines="None" BorderStyle="Solid" BorderWidth="0px" OnRowCommand="gvParentGrid_RowCommand"
            BorderColor="White" DataKeyNames="ID" OnRowDataBound="gvParentGrid_RowDataBound"
            EnableModelValidation="True">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

在后面的代码重新绑定gridview on&#34; Timer1_Tick &#34;像这样的事件

protected void Timer1_Tick(object sender, EventArgs e)
{
      // rebind  GridView Controls
}

现在,两个GridViews每5秒自动刷新一次。

要设置标签文本,您可以从后面的代码中调用服务器端方法

设计师代码

 <asp:Label ID="Label10" runat="server" Text='<%# getElapsedTime(Eval("Qtn_Date").ToString())%>'><asp:Label>

背后的代码

public string getElapsedTime(string date)
{
    DateTime ansdate = Convert.ToDateTime(date);
    if ((DateTime.Now - ansdate).TotalDays > 1)
    {


        return String.Format("asked {0:MMM d \\'yy} at {0:hh:mm}", ansdate);
    }
    else
    {
        TimeSpan span = (DateTime.Now - ansdate);
        if (span.Hours > 1)
        {
            return String.Format("asked {0} hours, ago ",
                 span.Hours);
        }
        else
        {
            return String.Format("asked {0} minutes, ago ",
        span.Minutes);
        }
    }
}