2016-02-05 39 views
0

我試圖在我的應用程序中顯示文本。輸入是我接受使用FileUpload的文本文件。我必須將文本分成單詞,並將每個單詞分解爲相應的字符。然後,每個單詞首先以字母順序顯示,然後整個單詞顯示在一個字幕中。在asp.net和c#中顯示文本時添加延遲#

到目前爲止,我已設法接受該文件。我也設法分割內容。但我無法知道如何在顯示每個字母后添加延遲。另外,顯示每個字符後,它必須在下一個字符出現之前消失。我嘗試使用Thread.Sleep(),但它所做的只是凍結我的UI,使線程在開始時處於睡眠狀態,並一次顯示所有內容。

同上Task.Delay()。

有人告訴我使用計時器,但我是C#編程新手,不知道它們是如何工作的。到目前爲止,這是我所做的。第一個按鈕(即Button1)按原樣顯示文件的內容。第二個按鈕(即Button2)用於分割文本。

aspx.cs文件:

protected void Page_Load(object sender, EventArgs e) 
{ 

} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    String a = FileUpload1.FileName; 
    String ext = Path.GetExtension(a); 
    //Label1.Text = ext; 
    if (ext == ".txt") 
    { 

     System.IO.StreamReader reader = new System.IO.StreamReader(FileUpload1.FileContent); 
     string text = reader.ReadToEnd(); 
     //Response.Write(text); 
     TextBox1.Text = text; 



     //Above section for text file 


    } 
    if (ext == ".docx" || ext == ".doc") 
    { 
     //String b = FileUpload1.PostedFile.FileName; 
     // Open a doc file. 

     string filename = Path.GetFileName(FileUpload1.FileName); 
     FileUpload1.SaveAs(Server.MapPath("~/") + filename); 
     Application application = new Application(); 
     Document document = application.Documents.Open(Server.MapPath("~/") + filename); 

     // Loop through all words in the document. 
     int count = document.Words.Count; 
     for (int i = 1; i <= count; i++) 
     { 
      // Write the word. 
      string text = document.Words[i].Text; 
      //Response.Write(text); 
      TextBox1.Text = text; 
     } 
     // Close word file 
     application.Quit(); 
    } 
} 

protected void Button2_Click(object sender, EventArgs e) 
{ 
    String a1 = FileUpload1.FileName; 
    String ext1 = Path.GetExtension(a1); 
    if (ext1 == ".txt") 
    { 

     System.IO.StreamReader reader = new System.IO.StreamReader(FileUpload1.FileContent); 
     string text = reader.ReadToEnd(); 


     /*foreach (char c in text) 
     { 
      TextBox2.Text = c.ToString(); 
      System.Threading.Thread.Sleep(); 
     }*/ 


     List<string> list = new List<string>(); //code for splitting 
     String[] words = text.Split(); 
     for (int i = 0; i < words.Length; i++) 
     { 
      list.Add(words[i]); 
     } 
     foreach (string word in words) 
     { 

      Char[] letters = word.ToCharArray(); 
      foreach (char letter in letters) 
      { 

       Response.Write("<marquee>"+letter+"</marquee>"); 
       Response.Write("<br>"); 



      } 
      Response.Write("<marquee>" + word + "</marquee>"); 
      Response.Write("<br>"); 

     } 

    } 

} 
+0

,這就是你應該把代碼做延遲...在Javascript ,基本上。首先將所有數據發送給客戶端,然後使用Javascript在適當的時間顯示它。 –

回答

1

你應該使用客戶端腳本,而不是服務器代碼...使用的UpdatePanel .... :) 在你的aspx:

<asp:UpdatePanel ID="updatePanel1" runat="server"> 
    <ContentTemplate> 
    <div style="display:none"> 
    <!-- Hidden Field For Storing our formatted Text --> 
    <asp:Literal ID="hiddenLiteral" runat="server"/></div> 
    <!-- a field for store Javascript --> 
    <asp:Literal ID="scriptLiteral" runat="server"/> 
    <!-- a field which use for Showing Text To User --> 
    <asp:Literal ID="displayLiteral" runat="server"/> 

    </ContentTemplate> 
</asp:UpdatePane> 

然後編寫一個javascript for literalValue,然後更新updatePanel,如下所示:

protected void Button2_Click(object sender, EventArgs e) 
{ 
    String a1 = FileUpload1.FileName; 
    String ext1 = Path.GetExtension(a1); 
    if (ext1 == ".txt") 
    { 

     System.IO.StreamReader reader = new System.IO.StreamReader(FileUpload1.FileContent); 
     string text = reader.ReadToEnd(); 

     List<string> list = new List<string>(); //code for splitting 
     String[] words = text.Split(); 
     for (int i = 0; i < words.Length; i++) 
     { 
      list.Add(words[i]); 
     } 
     var textTobeShown = new List<string>(); 
     foreach (string word in words) 
     { 

      Char[] letters = word.ToCharArray(); 
      foreach (char letter in letters) 
      { 

       textTobeShown.Add("<marquee>"+letter+"</marquee>"); 

      } 
       textTobeShown.Add("<marquee>" + word + "</marquee>"); 
     } 
    //use <sep> for separating text 
    hiddenLiteral.text=String.Join("<sep>",textTobeShown); 
    //Call Our Javascript Function when updatePanel Update Fields Value 
    scriptLiteral.Text=String.Concat("<script> DisplayText('",hiddenLiteral.ClientID,"','",displayLiteral.ClientID,"'); </script>"); 

    //updating our updatePanel 
    updatePanel1.Update(); 
    } 

} 

,並在你的HTML(CSS)定義的腳本標記像這樣:如果你想在客戶端的延遲

<script> 

    function DisplayText(hiddenFieldId,textFieldId){ 
     var hiddenValue=document.getElementById(hiddenFieldId); 
     var textField=document.getElementById(textFieldId); 
     if(!hiddenValue || !textField) {throw 'HddenField or TextField Not Find';} 
     var textToBeShown=hiddenValue.innerHTML.split('<sep>'); 
     if(textToBeShown.length==0){throw 'there is nothing to show';} 
     var count=textToBeShown.length; 

     //create delay for shown Text, in 0.5secon,1second... 
     var delay=0.5; 

     //loop through text and show them, after showing each text, delay for 500ms(0.5s) 
    timeout([0,count], 1, function(i){ 
    textField.innerHTML+=textToBeShown[i]+'<br />'; 
}); 

    } 

function timeout(range, time, callback){ 
    var i = range[0];     
    callback(i); 
    Loop(); 
    function Loop(){ 
     setTimeout(function(){ 
      i++; 
      if (i<range[1]){ 
       callback(i); 
       Loop(); 
      } 
     }, time*1000) 
    } 
}  
</script>