update label inside updatepanel in asp.net - asp.net

i need to be able to update the label text whenever i want without clicking on a button or something similar to that. the update happens based on server-client logic. i tried to use java script, but when i call the script from a function i wrote it doesn't work. now i'm trying to use updatepanel and no luck so far. i tried to use
UpdatePanel1.Update();
but it didn't refresh the label. the label defined inside the updatepanel:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label3" runat="server" Text="Label" AccessKey="l"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
is there a away to force it to refresh?

If you just want to update your label from javascript you can try this function:
<script type="text/javascript">
function updateLabel(newValue) {
$('#<%= Label3.ClientID %>').val(newValue);
}
</script>

Related

Trigger asp.net update panel using hyperlink

I am developing a website in asp.net 2010 and use asp.net controls to use Ajax.
I have an update panel that does not have _Load event and no trigger tag and should be triggered (not updated) manually by clicking a hyperlink (tag 'a'). I have to use hyperlink instead of other controls such as button for some reasons.Now I want a javascript or jquery function to trigger my update panel or call a function in server side code that triggers it.
my asp.net code:
<a id="trg" href="#div5"></a>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
my c# function that should be called to update UpdateUanel1 by clicking hyperlink trg:
protected void func()
{
Label1.Text=System.DateTime.Now.ToString();
}

CKEditor not working properly inside update panel

I am having a problem with CKEditor inside an update panel in asp.net.
I have tab control on page with multiple CKEditor's i.e one ckeditor in each tab.
string scriptAdd = #"var editor = CKEDITOR.instances['ctl00_ContentPlaceHolder1_faqeditor']; if (editor) { editor.destroy(true); } CKEDITOR.replace('ctl00_ContentPlaceHolder1_faqeditor');";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "", scriptAdd, true);
The above code snippet helps in rendering the editor in update panel. But when a postback is done it still shows the earlier value and not the changed one i.e the editor does not reflect the changes made after the tab is changed in the update panel.
The same thing works perfectly fine without update panel.
Is there any solution for this problem?
just force ckeditor to update the textarea on change :
var ckEditor = CKEDITOR.replace('ctl00_ContentPlaceHolder1_faqeditor');
ckEditor.on("change", function (event) {
event.editor.updateElement();
});
Sorry for the late response on this, but the answer may be helpful to others as well. You also need to do the following in code behind:
ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), "updatescript", "CKEDITOR.instances['ctl00_ContentPlaceHolder1_faqeditor'].updateElement();");
Hope this helps.
<form id="form1" runat="server">
<asp:ScriptManager ID="scrpM" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btnshow" runat="server" Text="Show Hidden Editor" />
<div id="divEditor" runat="server" visible="false">
<asp:PlaceHolder ID="plCKEditor" runat="server"></asp:PlaceHolder>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
----------
Add editor inside a div with visible="false"
and on the button click you set visible="True"
it's works fine for me

Timer Tick in Asp.net?

whats is the equivalent of these in asp?
it doesn't work for me!
lbl_date.Text = FormatDateTime(Now, DateFormat.LongDate)
lbl_time.Text = FormatDateTime(Now, DateFormat.LongTime)
lbl_date.Text = DateTime.Now.ToLongDateString();
lbl_time.Text = DateTime.Now.ToLongTimeString();
The thing to remember here is that all your vb code does is generate an html document, and nothing else. Once that job is done, the page class you're working in is even sent off to the garbage collector and the processor thread you were in is repurposed to serve another http request. Therefore trying to set a label based on a timer event is just plain nuts — your timer will likely be disposed before it ever has a chance to tick.
Instead, you want to do this particular job in javascript. Look into javascript's setTimeout() method.
There is no Timer control in ASP.NET as such, but AJAX timer.
In that case you have to put these labels inside AJAX update panel as follows
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="2000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="lbl_date" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="lbl_time" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
and in code behind for Timer1_Tick event put
lbl_date.Text = FormatDateTime(Now, DateFormat.LongDate)
lbl_time.Text = FormatDateTime(Now, DateFormat.LongTime)
this should work..

UpdatePanel, Timer and TexBox Problem

<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Always" >
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="300" ontick="Timer1_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
</ContentTemplate>
</asp:UpdatePanel>
Problem is that TextBox1’s cursor is not blinking it is static but you can write in it. It gives the impression that it freezed. Why cursor is not blinking?
You should set your timer interval near as 1000 milliseconds. That should solve your blinking problem.
When the post-back completes you can run a piece of JavaScript which will remove the focus from the textbox (e.g. it will no longer accept text) and then you can immediately re-focus on the textbox (so it will accept text again). This may "reset" the cursor so that it displays properly.
Try adding this to your ASPX, preferably outside of the UpdatePanels:
<script type="text/javascript">
function fixTextBoxFocus()
{
var textBox = document.getElementById("<%= TextBox1.ClientID %>");
textBox.blur(); //Remove the focus from the text box.
textBox.focus();//Re-focus on the textbox.
}
</script>
And then, in your code-behind (replace MyPage with the name of your page's class):
protected void Timer1_Tick(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(typeof(MyPage), "fixTextBoxFocus", "fixTextBoxFocus();", true);
}
Now, when the partial-post-back occurs, this script will be executed each time. Give it a try and let me know if it helps to fix the issue.
Placing a timer inside an updatepanel will result in a repeat refresh of that update panel. SInce you have placed the timer interval as 300 milliseconds. That may be the problem.
What you need to think about -
Do you really want to place the timer in the update panel?
Do you really need to keep the interval as 300 milli seconds.
Will it not be possible to move the textbox outside the updatepanel?

How to not focus when using Ajax Timer Control in asp.net

when i using ajax control but control tick every 1 secound and when i click other control the control not focus in selection my control when timer tick
That particular timer must be placed inside the update panel.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
Here's another way to do it:
http://mattberseth.com/blog/2008/05/bug_bash_enablingdisabling_the.html
This is how I approached it (note that the server-side tags for Timer1 and the "txtSymbol" textbox are approached differently):
<asp:Content ID="HeadContent" runat="server" ContentPlaceHolderID="HeadContent">
<link rel="stylesheet" type="text/css" href="/Website1/Styles/lessantique.css" />
<script type="text/javascript">
function pageLoad(sender, args){
// fetch the timer components
var timer = $find('<%= this.Timer1.ClientID %>');
// fetch the INPUT element
var textbox = $get('Content1_txtSymbol');
$addHandler(textbox, 'focus', Function.createDelegate(this, function(){
// disable the Timer so we don't refresh the page
// while the user is entering the data
timer._update(false, timer.get_interval());
}));
$addHandler(textbox, 'blur', Function.createDelegate(this, function(){
// re-enable the Timer
timer._update(true, timer.get_interval());
}));
}
</script>
</asp:Content>
regarding the server side code for the timer listed in the previous response, that approach did not work for me since I have more than one update panel, where the text box is not in the same panel that uses the timer. In that case I followed the advice at http://forums.asp.net/t/1632054.aspx/1, where a new panel is implemented, as such:
<asp:UpdatePanel runat="server" ID="UpdatePanel3" UpdateMode="Conditional" ChildrenAsTriggers="false" >
<ContentTemplate>
<asp:Timer runat="server" id="Timer1" Enabled="true"
Interval="2000" OnTick="Timer1_Tick" />
</ContentTemplate>
</asp:UpdatePanel>
Despite the above not producing errors, the results were still unsatisfactory, therefore the client-side approach.

Resources