2011-08-25 97 views
0

我想更新我的文本框控件在Asp.Net文本屬性從循環內我已經嘗試使用Ajax更新面板與計時器控件,但無法更新文本屬性。我一直搜索這個自上週以來,但無法找到任何答案任何幫助,將higly讚賞通過循環更新文本框的文本屬性

以下是詳細:

這是我在這個代碼按鈕單擊事件中,你可以看到我更新的

TxtContent.Text 

在一個循環內文本是bei NG返回由稱爲

ReturnBodyText() 

後來我分配文本視圖狀態,以便在計時器滴答事件中,我能回憶起的文字和更新文本框的文本屬性,然後在計時器滴答事件分配一個用戶定義的函數視圖狀態值到文本框。

protected void Button4_Click(object sender, EventArgs e) 
{ 
    ArrayList FilePath = (ArrayList)ViewState["ArrayList"]; 
    int i = 0; 
    int b = 1; 
    foreach(string[] sr in FilePath) 
    { 
     string Month = sr[i]; 
     string datewithzero; 
     datewithzero = String.Format("{0:00}", b); 
     string[] FilesArray = (string[])listfiles(Month, datewithzero).ToArray(typeof(string)); 

     foreach (string FileNameWithExtension in FilesArray) 
     { 
      ListBox4.Items.Add(FileNameWithExtension); 
      TxtContent.Text = ReturnBodyText(Month, datewithzero, FileNameWithExtension); 
      ViewState["Content"] = TxtContent.Text; 
      Timer1.Enabled = true; 
      Timer1_Tick(ViewState["Content"], e); 
     } 
     i = i + 1; 
     b = b + 1; 
    } 
} 

protected void Timer1_Tick(object sender, EventArgs e) 
{ 
    TxtContent.Text =(string) ViewState["Content"]; 
    TxtContent.DataBind(); 
} 

我希望上面的描述將有助於理解我的問題

完整的aspx文件你們和後面的代碼如下:

  • .aspx的代碼:

    <%@ Page Language =「C#」AutoEventWireup =「true」CodeFile =「Default.aspx.cs」Inherits =「_ Default」%>

    <%@註冊組件= 「AjaxControlToolkit」 命名空間= 「AjaxControlToolkit」 的TagPrefix = 「ASP」 %>

    <asp:ListBox ID="ListBox1" runat="server" Height="509px" Width="59px"> 
        </asp:ListBox> 
    
        <asp:Button ID="Button2" runat="server" Height="26px" onclick="Button2_Click" 
         Text="To be pressed first" Width="130px" /> 
    
        <asp:Button ID="Button3" runat="server" onclick="Button3_Click" 
         Text="To be pressed 2nd" Width="131px" Height="26px" /> 
    
        <asp:ScriptManager ID="ScriptManager1" runat="server"> 
        </asp:ScriptManager> 
    
          <asp:Timer ID="Timer1" runat="server" Interval="1000" 
         ontick="Timer1_Tick" Enabled="False"> 
          </asp:Timer> 
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
         <ContentTemplate> 
          <asp:TextBox ID="TxtContent" runat="server" Height="508px" AutoPostBack="True" 
           TextMode="MultiLine"></asp:TextBox> 
          <asp:Button ID="Button5" runat="server" Height="26px" onclick="Button4_Click" 
           Text="To be pressed Third" Width="122px" /> 
    
         </ContentTemplate> 
         <Triggers> 
          <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> 
         </Triggers> 
        </asp:UpdatePanel> 
    
    </div> 
    </form> 
    

    - Codebehind:

    using System; using System.Collections.Generic;使用System.Linq的 ; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;使用System.Net的 ;使用System.IO的 ;使用System.Collections的 ;

    公共部分類_Default:FTP {

    protected void Button2_Click(object sender, EventArgs e) 
    { 
        string[] array = (string[])listfiles().ToArray(typeof(string)); 
        ViewState["FolderName"] = array; 
        foreach (string FileName in array) 
        { 
         ListBox1.Items.Add(FileName); 
        } 
    } 
    protected void Button3_Click(object sender, EventArgs e) 
    { 
        string[] arraylistbox2 = (string[])ViewState["FolderName"]; 
        string[] arrays = new string[12]; 
        ArrayList ListPath = new ArrayList(); 
        int dateFolder; 
        foreach (string FileName in arraylistbox2) 
        { 
         dateFolder = Convert.ToInt16(FileName); 
         dateFolder = dateFolder - 1; 
         ListPath.Add((string[])(listfiles(FileName).ToArray(typeof(string)))); 
        } 
        ViewState["ArrayList"] = ListPath; 
    } 
    protected void Button4_Click(object sender, EventArgs e) 
    { 
        ArrayList FilePath = (ArrayList)ViewState["ArrayList"]; 
        int i = 0; 
        int b = 1; 
        foreach(string[] sr in FilePath) 
        { 
         string Month = sr[i]; 
         string datewithzero; 
         datewithzero = String.Format("{0:00}", b); 
         string[] FilesArray = (string[])listfiles(Month, datewithzero).ToArray(typeof(string)); 
    
         foreach (string FileNameWithExtension in FilesArray) 
         { 
    
          TxtContent.Text = ReturnBodyText(Month, datewithzero, FileNameWithExtension); 
          ViewState["Content"] = TxtContent.Text; 
          Timer1.Enabled = true; 
          Timer1_Tick(ViewState["Content"], e); 
         } 
         i = i + 1; 
         b = b + 1; 
        } 
    } 
    protected void Timer1_Tick(object sender, EventArgs e) 
    { 
    
        TxtContent.Text =(string) ViewState["Content"]; 
        TxtContent.DataBind(); 
    
    
    } 
    
+0

你能確認你的定時工作?就像你在調試時你正在觸發Timer1_Tick事件一樣? – Bobby

+0

如果可以,可以發佈所有相關的代碼嗎?包括.aspx內容。那會讓我們重現你的問題。儘管我尊重那些沒有這個能夠解決這個問題的人,但我不是其中之一。 – besamelsosu

+0

@Bobby是我Timer1_Tick事件正在調試模式下命中斷點 – user758528

回答

1

可悲的是,你的代碼並沒有幫助我很多。但是,我寫了這個我相信做你想要的東西 - 部分 - 。在我看來,雖然沒有更多可以做到。這裏是代碼:

。aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
     <asp:Timer runat="server" ID="Timer1" Interval="1000" Enabled="true" 
      ontick="Timer1_Tick" /> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
      <Triggers> 
       <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> 
      </Triggers> 
      <ContentTemplate> 
       <asp:Label ID="Label1" runat="server" Text=""></asp:Label> 
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
      </ContentTemplate> 
     </asp:UpdatePanel> 
    </div> 
    </form> 
</body> 
</html> 

aspx.cs文件:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Threading; 

namespace WebApplication1 
{ 
    public partial class Default : System.Web.UI.Page 
    { 
     protected static string keeper; 
     protected static bool inUse = false; 
     protected void Page_Load(object sender, EventArgs e) {} 
     protected void Button1_Click(object sender, EventArgs e) 
     { 
      Thread worker = new Thread(new ThreadStart(workerFunction)); 
      worker.Start(); 
      return; 
     } 
     protected void workerFunction() 
     { 
      inUse = true; 
      for (int i = 0; i < 3; i++) 
      { 
       TextBox1.Text += "foo"; 
       keeper = TextBox1.Text; 
       Thread.Sleep(1000); 
      } 
      inUse = false; 
      keeper = ""; 
     } 
     protected void Timer1_Tick(object sender, EventArgs e) 
     { 
      if (inUse) 
      { 
       TextBox1.Text = Convert.ToString(keeper); 
      } 
     } 
    } 
}