dropdown selected index change without postback no update panel needed - asp.net

i am trying to fill text box from database on dropdownlist selected index changed without page post back.
i tried the following code it does not work
<script runat="server">
private String queryString;
protected void Page_PreRender(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(queryString))
{
Class1 obj = new Class1();
if (ddlEmployee.SelectedIndex >= 0)
{
System.Data.DataSet ds = obj.BindDataSet("Select ID,Email from EmployeeDetails where ID=" + ddlEmployee.SelectedValue);
txtEmail.Text = ds.Tables[0].Rows[0][1].ToString();
}
}
}
protected void Page_Init(object sender, EventArgs e)
{
queryString = "?foo=bar;a=b";
}
</script>

Look into ASP.NET PageMethods. Here's a sample:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:DropDownList ID="ddlEmployee" runat="server">
<asp:ListItem Selected="True" Value="1">One</asp:ListItem>
<asp:ListItem Value="2">Two</asp:ListItem>
<asp:ListItem Value="3">Three</asp:ListItem>
<asp:ListItem Value="4">Four</asp:ListItem>
<asp:ListItem Value="5">Five</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#ddlEmployee").on("change", function () {
PageMethods.SetText($(this).val(), OnSetCompleted);
});
function OnSetCompleted(result) {
$("#TextBox1").val(result);
}
</script>
In the code-behind:
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static string SetText(string id)
{
//do your DB stuff here
return "You selected " + id;
}
Check these:
http://weblogs.asp.net/jalpeshpvadgama/archive/2012/01/07/asp-net-page-methods-with-parameters.aspx
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Related

When I press enterkey on textbox than page_load twice(google chrome)

I want do textbox_textchanged,but when i press enter key,it will do postback twice,and in my project is Serious error.It will overwrite my error message.I hope let user use textchanged,also user press enterkey can do textchanged event.
When i user google chrome i have this problem,but IE isn't have this porblem.
it's example this's my aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lbl1.Text = "first load";
}
else
{
lbl1.Text = "postback=>> ispostback";
}
}
protected void ttbtest_TextChanged(object sender, EventArgs e)
{
lbl4.Text = "YOU DO ttbtest_TEXTCHANGED,YOU KEY IN:" + ttbtest.Text;
}
my aspx example:
<script src="Scripts/jquery-3.4.1.js"></script>
<script language="javascript" type="text/javascript">
function txtTextKeyDown(txt) {
if ((window.event.keyCode == 13) && txt.value != '') {
__doPostBack("ttbtest", "TextChanged");
}
else return;
}
function ConvertFindEnterKey() {
if (window.event.keyCode == 13) {
var readcode = event.srcElement.value;
__doPostBack("ttbtest", "TextChanged");
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox onkeypress="javascript:return ConvertFindEnterKey();" ID="ttbtest" runat="server"
Width="250px" CssClass="CSInput" MaxLength="30" OnTextChanged="ttbtest_TextChanged" AutoPostBack="true"></asp:TextBox>
<br />
<asp:Label ID="lbl4" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>

Snackbar in ASP.NET

I am using this code from w3schools:
TryIt
How can i integrate it with asp:LinkButton or asp:Button using OnClientClick, while onClick is executing the server code.
Almost a year late to the party, but I've just implemented the same snackbar.
It needs an UpdatePanel and you need to register a client script block on your event handler:
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<div>
<asp:Button runat="server" ID="ShowSnackbar" Text="Show Snackbar" OnClick="ShowSnackbar_Click" />
</div>
<div id="snackbar">
<asp:Label runat="server" ID="Snack" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
The codebehind was tricky but it works. On page load, create a string that contains the javascript, then use that string value to register your script block...
private string snackbarScript;
protected void Page_Load(object sender, EventArgs e)
{
snackbarScript = GenerateSnackbarJS();
}
private string GenerateSnackbarJS()
{
var sb = new StringBuilder();
sb.AppendLine("var x = document.getElementById('snackbar');");
sb.AppendLine("x.className = 'show';");
sb.AppendLine("setTimeout(function(){ x.className = x.className.replace('show', ''); }, 3000);");
return sb.ToString();
}
protected void ShowSnackbar_Click(object sender, EventArgs e)
{
Snack.Text = "Here's the snackbar";
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "snackbar", snackbarScript, true);
}

Weird post back behavior on ASP.NET web form while refreshing

In an ASP.NET web form, I have two button controls and I have come across a weird scenario.
I have btnOpenAddRoleModal and btn2. when I click on btnOpenAddRoleModal, btnAddGlobalRoles becomes visible and then I click on btnAddGlobalRoles which performs some function.
Now when I press F5 to refresh the page, and because ASP.NET post-backs the page again on refresh, it does the post back as usual.
My problem is that it runs the click event handler of btnOpenAddRoleModal instead of btnAddGlobalRoles whereas the last button I clicked was btnAddGlobalRoles.
Anyone knows why this could be a case?
aspx page
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/MainTheme.Master" AutoEventWireup="true" CodeBehind="GlobalRoles.aspx.cs" Inherits="OPT.GlobalRoles" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<header style="overflow: auto;">
<h1 class="zs-left">Global Roles</h1>
<div class="zs-right">
<asp:Button ID="btnOpenAddRoleModal" runat="server" Text="Add User" CssClass="zs-button zs-button-action" OnClick="btnOpenAddRoleModal_Click" OnClientClick="return IncreasePostBackControlValue();" />
</div>
</header>
<section class="page-2cols" style="position: relative;">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<h2 class="zs-inner-title">Roles</h2>
<p class="zs-search-button">
<span class="zs-input-icon zs-icon-search">
<asp:Label ID="lblSearchText" runat="server" Text="Search Roles " Style="font-weight: 700"></asp:Label>
<asp:TextBox ID="txtSearchText" runat="server" CssClass="zs-input" placehoder="Enter key words to search" AutoPostBack="True" OnTextChanged="txtSearchText_TextChanged"></asp:TextBox>
</span>
</p>
<p>
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label></p>
<p>
<asp:Label ID="lblNoResultsMessage" runat="server" Text="No results found" Visible="false"></asp:Label></p>
<p>
<asp:GridView ID="gvGlobalRoles" runat="server" AutoGenerateColumns="False" OnRowDeleting="gvGlobalRoles_RowDeleting" CssClass="zs-data-table">
<Columns>
<asp:BoundField DataField="Serial" HeaderText="Sr. No." />
<asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" />
<asp:BoundField DataField="RoleName" HeaderText="Role" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<span class="zs-icon zs-icon-delete"></span>
<asp:LinkButton Text="Delete" CommandName="Delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</p>
<asp:Panel ID="pnlOverlay" CssClass="zs-overlay" Visible="false" runat="server"></asp:Panel>
<asp:Panel ID="pnlModal" Visible="false" CssClass="zs-modal" Style="display: inline-block; position: fixed; top: 50%; left: 50%; margin-top: 0px; margin-left: 0px; transform: translate(-50%, -50%);" runat="server">
<header>
<span><% =ModalTitle %></span>
<asp:LinkButton ID="lbCloseModal" runat="server" CssClass="zs-icon zs-icon-close zs-icon-large" OnClick="lbCloseModal_Click"></asp:LinkButton>
</header>
<section>
<p>
<asp:Label ID="lblModalMessage" runat="server" Text=""></asp:Label></p>
<asp:Panel ID="pnlDeleteListItem" runat="server" Visible="false">
You have selected to delete a role. Press OK to continue or Cancel to abort.
</asp:Panel>
<asp:Panel ID="pnlAddEditListItem" runat="server" Visible="false">
<p>
<asp:Label ID="lblEmployeeToAdd" runat="server" Text="Employee Email"></asp:Label><br />
<asp:TextBox ID="txtEmployeeToAdd" runat="server" CssClass="zs-input"></asp:TextBox>
</p>
<p>
<asp:Label ID="lblRoleToAdd" runat="server" Text="Role"></asp:Label><br />
<asp:DropDownList ID="ddlRoleToAdd" runat="server" CssClass="zs-input"></asp:DropDownList>
</p>
</asp:Panel>
</section>
<footer>
<asp:LinkButton ID="lbCancelModal" runat="server" OnClick="lbCancelModal_Click">Cancel</asp:LinkButton>
<asp:Button ID="btnAddGlobalRoles" runat="server" Text="Add" CssClass="zs-button zs-button-action" OnClick="btnAddGlobalRoles_Click" OnClientClick="return IncreasePostBackControlValue();" Visible="false" />
<asp:Button ID="btnDeleteGlobalRole" runat="server" CssClass="zs-button zs-button-action" Text="OK" OnClick="btnDeleteGlobalRole_Click" OnClientClick="return IncreasePostBackControlValue();" Visible="false" />
</footer>
</asp:Panel>
<asp:HiddenField ID="hfPostBackControl" runat="server" ClientIDMode="Static" />
<script>
//$(document).on("click", "button,input[type='submit'],input[type='button']", function () {
// $("#hfPostBackControl").val($("#hfPostBackControl").val() == "" ? 1 : Number($("#hfPostBackControl").val()) + 1);
//});
function IncreasePostBackControlValue() {
$("#hfPostBackControl").val($("#hfPostBackControl").val() == "" ? 1 : Number($("#hfPostBackControl").val()) + 1);
return true;
}
</script>
</ContentTemplate>
</asp:UpdatePanel>
</section>
</asp:Content>
aspx.cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OPT.Models;
namespace OPT
{
public partial class GlobalRoles : System.Web.UI.Page
{
BAL bal = new BAL();
string message = "";
int messageStateID;
DataTable dtRoles;
DataTable dtGlobalEmployeeRoles;
GlobalRole globalRoleToDelete;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadRoleDropDown();
LoadGlobalRolesFromDatabase();
Session["sPostBackValue"] = 0;
}
Generic.HideControls(AllMessageControls);
}
protected void Page_PreInit(object sender, EventArgs e)
{
Session["CurrentPage"] = ePage.GlobalRoles;
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (!Pages.ShowGlobalRoles())
{
Generic.RediretToMesagePage("Unauthorized user");
}
if (gvGlobalRoles.Rows.Count > 0)
{
gvGlobalRoles.HeaderRow.TableSection = TableRowSection.TableHeader;
lblNoResultsMessage.Visible = false;
}
else
{
lblNoResultsMessage.Visible = true;
}
}
void LoadGlobalRolesFromDatabase()
{
dtGlobalEmployeeRoles = bal.GetGlobalEmployeeRoles(txtSearchText.Text, ref message, ref messageStateID);
Session["GlobalEmployeeRoles"] = dtGlobalEmployeeRoles;
gvGlobalRoles.DataSource = Session["GlobalEmployeeRoles"] as DataTable;
gvGlobalRoles.DataBind();
}
void LoadRoleDropDown()
{
dtRoles = bal.GetGlobalRoles(ref message, ref messageStateID);
Generic.PopulateDataTableInDropDown(ddlRoleToAdd, dtRoles, "ID", "Name", "Please select");
}
protected void gvGlobalRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowIndex = e.RowIndex;
globalRoleToDelete = new GlobalRole();
dtGlobalEmployeeRoles = Session["GlobalEmployeeRoles"] as DataTable;
globalRoleToDelete.ID = Convert.ToInt32(dtGlobalEmployeeRoles.Rows[rowIndex]["ID"].ToString());
Session["CurrentGlobalRole"] = globalRoleToDelete;
ModalTitle = "Delete Role";
Generic.ShowControls(modalDeleteControls);
}
protected void btnAddGlobalRoles_Click(object sender, EventArgs e)
{
if (Generic.IsRefresh(hfPostBackControl)) { return; }
Employee employee = new Employee();
Role role = new Role();
employee.EmailID = txtEmployeeToAdd.Text;
role.ID = Convert.ToInt32(ddlRoleToAdd.SelectedValue);
bal.CreateGlobalEmployeeRole(employee, role, ref message, ref messageStateID);
if (messageStateID == 1)
{
Generic.HideControls(modalAllControls);
Generic.ShowMessage(lblMessage, message, messageStateID);
}
else
{
ModalTitle = eModalTitle.EditProject;
Generic.ShowMessage(lblModalMessage, message, messageStateID);
}
LoadGlobalRolesFromDatabase();
}
protected void btnDeleteGlobalRole_Click(object sender, EventArgs e)
{
if (Generic.IsRefresh(hfPostBackControl)) { return; }
globalRoleToDelete = Session["CurrentGlobalRole"] as GlobalRole;
bal.DeleteGlobalEmployeeRole(globalRoleToDelete, ref message, ref messageStateID);
if (messageStateID == 1)
{
Generic.HideControls(modalAllControls);
Generic.ShowMessage(lblMessage, message, messageStateID);
}
else
{
ModalTitle = eModalTitle.EditProject;
Generic.ShowMessage(lblModalMessage, message, messageStateID);
}
LoadGlobalRolesFromDatabase();
Generic.HideControls(modalDeleteControls);
}
protected void lbCloseModal_Click(object sender, EventArgs e)
{
Generic.HideControls(modalAllControls);
}
protected void lbCancelModal_Click(object sender, EventArgs e)
{
Generic.HideControls(modalAllControls);
}
protected void btnOpenAddRoleModal_Click(object sender, EventArgs e)
{
if (Generic.IsRefresh(hfPostBackControl)) { return; }
ModalTitle = "Add Role";
Generic.ClearControlValuesInContainer(pnlAddEditListItem);
Generic.ShowControls(modalAddControls);
}
protected void txtSearchText_TextChanged(object sender, EventArgs e)
{
LoadGlobalRolesFromDatabase();
}
private string _ModalTitle;
public string ModalTitle
{
get { return _ModalTitle; }
set { _ModalTitle = value; }
}
protected List<Control> modalAllControls
{
get
{
List<Control> controls = new List<Control>();
controls.AddRange(modalGenericControls);
controls.AddRange(modalAddControls);
controls.AddRange(modalDeleteControls);
return controls;
}
}
protected List<Control> modalGenericControls
{
get
{
return new List<Control>{
pnlOverlay as Control,
pnlModal as Control
};
}
}
protected List<Control> modalDeleteControls
{
get
{
List<Control> controls = new List<Control>{
pnlDeleteListItem as Control,
btnDeleteGlobalRole as Control
};
controls.AddRange(this.modalGenericControls);
return controls;
}
}
protected List<Control> modalAddControls
{
get
{
List<Control> controls = new List<Control>{
pnlAddEditListItem as Control,
btnAddGlobalRoles as Control
};
controls.AddRange(this.modalGenericControls);
return controls;
}
}
protected List<Control> AllMessageControls
{
get
{
List<Control> messageControls = new List<Control>{
lblMessage as Control,
lblModalMessage as Control
};
return messageControls;
}
}
}
}
Few supporting methods here
public static bool IsRefresh(HiddenField hfPostBackControl)
{
int sPostBackValue = HttpContext.Current.Session["sPostBackValue"] == null ? 0 : Convert.ToInt32(HttpContext.Current.Session["sPostBackValue"]);
int hPostBackValue = string.IsNullOrEmpty(hfPostBackControl.Value) ? 0 : Convert.ToInt32(hfPostBackControl.Value);
HttpContext.Current.Session["sPostBackValue"] = hPostBackValue;
return sPostBackValue == hPostBackValue;
}
/// <summary>
/// This method can be used for displaying pop ups after any action. One needs to pass the controls as parameter that need to shown on any event.
/// </summary>
/// <param name="controls">List of controls that need to be displyed as pop-up</param>
public static void ShowControls(List<Control> controls)
{
foreach (Control c in controls)
{
c.Visible = true;
}
}
/// <summary>
/// This method can be used for hiding pop ups being displayed. One needs to pass the controls as parameter that need to hidden on any event.
/// </summary>
/// <param name="controls">List of controls that need to be hidden</param>
public static void HideControls(List<Control> controls)
{
foreach (Control c in controls)
{
c.Visible = false;
}
}
public static void ShowMessage(Label lblMessage, string message, int messageStateID)
{
lblMessage.Text = message;
lblMessage.CssClass = messageStateID == 1 ? "zs-message zs-success" : "zs-message zs-error";
lblMessage.Visible = true;
}

ASP.NET: Ajax UpdatePanel issue

I am supposed to update the progress & display the corresponding message on the client side while the function is still under execution at server side.
How can I achieve this?
The function looks like:
protected void Button1_Click(object sender, EventArgs e)
{
string Result = "Success";
if (Result == "Success")
{
Label1.Text = "Plan mst Completed";
Thread.Sleep(2000); //Some functionality here
Label1.Text = "Packing date mst Started";
}
if (Result == "Success")
{
Label1.Text = "Packing date mst Completed";
Thread.Sleep(2000); //Some functionality here
Label1.Text = "Etd mst Started";
}
if (Result == "Success")
{
Label1.Text = "Etd mst Completed";
Thread.Sleep(2000); //Some functionality here
Label1.Text = "Inner box mst Started";
}
}
And I have to update the text of Label1 while the above function is still under execution.
I have tried using AJAX (although I'm still a beginner), but with no success. Here's what I did:
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10" ontick="Timer1_Tick1">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
And the corresponding events:
protected void Page_Load(object sender, EventArgs e)
{
UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional;
}
protected void Timer1_Tick1(object sender, EventArgs e)
{
UpdatePanel1.Update();
}
Any other alternatives other than AJAX such as via jQuery or otherwise are also welcome.
these might help you:
http://forums.asp.net/t/1500201.aspx
ASP.NET Asynchronous label update

update the textbox text property through loop

I want to update the text property of my textbox control in Asp.Net from within loop.I have tried using Ajax updatepanel with the timer control but unable to update the text property.I have been searching this since last week but unable to find any answer any help would be higly appreciated
Following is the detail:
This is my button click event in this code you can see I am updating the
TxtContent.Text
within a loop the text is being returned by a user defined function called
ReturnBodyText()
afterwards I am assigning the text to the viewstate so that at the timer tick event I can recall the text and update the textbox text property and then at the timer tick event assigning the viewstate values to the textbox.
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();
}
I Hope the above description will help you guys in understanding my problem
The complete aspx file and the code behind is as follows:
Aspx Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="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;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Collections;
public partial class _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();
}
Sadly, your code did not help me much. However, I wrote this which I believe does what you want -partially-. It seems to me nothing more can be done though. Here is the code:
.aspx file:
<%# 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 file:
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);
}
}
}
}

Resources