I have a FileSystemWatcher which monitors a directory for text files and when found it shows part of text in the textbox. I tried adding a breakpoint and saw that the .Text property have the desired value but I'm not able to see it on the client side(webform).
asp
<asp:TextBox ID="txtName" runat="server" EnableViewState="true"></asp:TextBox>
C#
FileSystemWatcher watcher;
protected void Page_Load(object sender, EventArgs e)
{
watcher = new FileSystemWatcher(path);
watcher.Created += OnFileCreated;
watcher.Changed += OnFileCreated;
watcher.InternalBufferSize = 64000;
watcher.Filter = "*.txt";
watcher.EnableRaisingEvents = true;
}
private void OnFileCreated(object sender, FileSystemEventArgs e)
{
if(e.Name == fileName)
{
try
{
if(File.Exists(fileName))
{
string[] lines = File.ReadAllLines(fileName);
if (lines.Length >= 3)
{
if (lines[1] == string.Empty || lines[1] == null)
{
string message = "Not a valid value";
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
else
{
string name = lines[1];
txtname.Text = name;
}
}
else
{
Console.WriteLine("The file does not have at least 3 lines.");
}
}
}
catch (Exception ex)
{
errorTxt.Text = ex.ToString();
}
}
}
When breakpoint is added I can see that txtname.Text = name; have name value





The problem will be that your code setting the textbox.text property is running server-side, so if the page displaying the textbox was refreshed then it would display the text you are expecting. If you want the textbox to update without a page refresh then you'll need some client-side code to handle it, but alternatively you could just the page every so often by including a meta refresh line in your page header, e.g.