Update PageTitle on Timer.Tick - c#

I've got a page with a Timer that is being used as a trigger on an UpdatePanel. The page also contains a TabContainer and several TabPanels. Look at this question for more information. Basically, I've got an UpdatePanel as the element in each TabPanel's ContentTemplate, and the UpdatePanel is triggered by the Timer.
My page displays data by reading a database on each tick. I've got the following code running on each Timer.Tick in my codebehind:
protected void timeRefresher_Tick(object sender, EventArgs e)
{
UpdateLivePageTitle();
}
The UpdateLivePageTitle() function reads the new information from the database and sets Page.Title accordingly. However, this information is of course not sent to the browser because there is no full page postback--only an async postback to the update panels. As a result, my page title is not being updated until the whole page is being posted back, which destroys the purpose of using UpdatePanels in the first place.
I figure there would be a way to do this by using the document.title JS element and call that from within UpdateLivePageTitle(). But as of now, I haven't been able to figure out how to do this. I tried using the following in my UpdateLivePageTitle() function:
string updatePageTitleScript = String.Format("document.title = '{0}'", newPageTitle);
ToolkitScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "UpdatePageTitle", updatePageTitleScript, true);
But the result of this was that my TabContainer stopped rendering. I'm also not sure that would work with the async partial page postbacks, either. Any ideas?
Thanks!

You forgot the ; from your script.
Oh, and if I remember correctly, the framework should be able to update the title if you just set Page.Title.

Related

Difference between !IsPostBack and refresh in Asp.Net

I have written some codes in !IsPostBack block. This code is getting executed when the page loads for the first time. That is fine. But the problem is, when I refresh the page by hitting the f5 key this executes again which I don't want to do. I have searched many articles and found difference between PostBack and refresh. I know about this. But my question is difference between !IsPostBack and Refresh. Can we write some code which executes only when the page loads for the 1st time not when we refresh the page. By the way I have written my !IsPostBack block inside Page_Init() method and I am using c# for codebehind. Thanks.
Refersh and IsPostback are somewhat unrelated:
Refresh in browser generally mean "re-run last action that resulted in this page". Usually it causes GET request, but it as well can cause POST if page was shown as result of postback. Side note: you often can find sites warning you not to refresh page during non-reversible operations like "charge my credit card" as it may trigger duplicate post.
IsPostBack simply states that request come to server as POST, not GET.
Combining that you can get Refresh that triggers either branch of if (IsPostBack) check. In most cases so server will receive GET request and hence execute !IsPostBack branch.
If you really need to detect if page was rendered once already - setting cookie or writing information into Session would be reasonable solution.
Please change your code behind code as given below.
Boolean IsPageRefresh;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["postids"] = System.Guid.NewGuid().ToString();
Session["postid"] = ViewState["postids"].ToString();
}
else
{
if (ViewState["postids"].ToString() != Session["postid"].ToString())
{
IsPageRefresh = true;
}
Session["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Session["postid"].ToString();
}
}

Setting Content in RadEditor

I have visited the Telerik's website and viewed their demos etc...
But I am having problems trying to load content (html) in the RadEditor.
I have a Button_Click event where I get my html string and then set it to the RadEditor. The RadEditor is inside a RadWindow and only becomes visible when the button is clicked.
protected void btnSubmitHtml_Click(object sender, EventArgs e)
{
RadEditor1.Content = "<p>hello there</p>";
RadWindow1.Visible = true;
}
This doesn't show the html inside the RadEditor for some odd reason. I suspect it is the page life cycle that is involved with this problem.
Are there any suggestions to solve this?
I have encountered this problem multiple times and never found a "Proper" resolution.
However, a great work around is to simply set the content from the clientside via injected script. The end result is the same, and if you can tolerate the 10 millisecond delay, worthy of consideration.
EDIT after comment requested reference
Basically all you need to get an instance of the editor using ASP.NET WebForms $find function. That takes the html ID of the root of the rendered object and returns the client side viewModel if one exists.
The $(setEditorInitialContent) call at the end assumes that jQuery is present and delays the execution of the function till page load.
<telerik:radeditor runat="server" ID="RadEditor1">
<Content>
Here is sample content!
</Content>
</telerik:radeditor>
<script type="text/javascript">
function setEditorInitialContent() {
var editor = $find("<%=RadEditor1.ClientID%>"); //get a reference to RadEditor client object
editor.set_html("HEY THIS IS SOME CONTENT INTO YOUR EDITOR!!!!");
}
$(setEditorInitialContent);
</script>
Take a look here to see how to get a RadEditor to work in a RadWindow: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-radeditor-in-radwindow.html.
Said shortly, here is what you need to have in the OnClientShow event of the RadWindow:
function OnClientShow()
{
$find("<%=RadEditor1.ClientID %>").onParentNodeChanged();
}
To edit Html code only you can add -
EnableTextareaMode="true"
Add this property to the RadEditor.
I suspect that the way the control tries to interpret the html might be one of the problems. The other thing that may be causing this problem is the page life cycle.

Call an event handler on another page

Simple question here, but I've got a nagging feeling that there's a more interesting solution than the one I've chosen:
Page Two consists of a dropdown, and the change event is handled to execute some query.
protected void ddlSavedQueries_SelectedIndexChanged(object sender, EventArgs e)
{
/* stuff happens */
}
Page One is a home page, where I'm providing another version of that dropdown. I'd like the change event in this case to redirect control to Page Two, and then execute the event handler.
My cheap solution is just a Redirect with a querystring value that is handled on page load. Am I missing a more interesting approach?
If you don't want to ugly things up with a querystring value, I suppose you could put something in Session and pick it up on Page_Load of the second page (and then clear it out of Session). Not exactly an awesome improvement though.
Does the same page always get displayed when you change that dropdown? If so, consider using client side javascript to redirect to the correct page, then fire any logic on the subsequent page in the page_load event. Example using jQuery:
$(function() {
$("select.classyouneedtodefine").change(function() {
document.location.href = "somepage.aspx?value=" + $(this).val();
});
});
haven't tested the above...just shooting from the hip

ASP.Net: Page_Load() being called multiple times

I don't know alot about ASP.Net but I'm trying to make a new control for a message box. You enter some info and press a button.
However, for some bizarre reason when the button is pressed, Page_Load() gets called a second time, and all of the member variables are reset to null! I need those variables, and Page_Load() has not reason to be called a second time! Of course the callstack is useless.
Remember, in ASP.Net every time you cause a postback of any kind, including handling events like button clicks, you're working with a brand new instance of your page class that must be rebuilt from scratch. Any work you've done previously to build the page on the server is gone. That means running the entire page life cycle, including your page load code, and not just the click code.
Always two there are, no more, no less. A request and a response.
When the page posts back, the Page_Load method is called. Then, once the server actually processes the page and sends you a new one based on changes, the Page_Load is called again, actually the first time on the new page sent to you.
So if you are pulling data in the Page_Load event or setting some values, enclose it in the following block:
if(!Page.IsPostBack)
{
}
to preserve some of your state. Otherwise, the instructions that you put into the Page_Load event will execute every time.
It helps to review the ASP.Net page lifecycle :)
As Joel mentioned, instance variables will be lost once the page is sent back to the client. However, there are various methods of storing the values of your variables so you can retrieve them later. This page on State Management is a good starting point if you want to learn more.
Any tag/element which requires url reference like img, anchor, object etc must be checked for the empty reference.
e.g. href="", url="", src="" are some common errors.
This code works for me:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["something"] == null)
{
Session["something"] = "1";
}
else
{
Session["something"] = null;
//your page load code here
}
}
}
For me, the issue was a bit complicated, but I found that the
protected override void OnPreRender(EventArgs e)
handler is only called once, so it's safer to put some actions in there if it's not too late in the pipeline for you.
An extension of #user3207728's response, I found this link explains it well and has a simple solution...
http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html
Unfortunately checking just for if (!Page.IsPostBack) is not enough as IsPostBack will always be FALSE on a refresh.
Just a shot in the dark but maybe add this after your page_load:
if (!IsPostBack)
{
you can use sessions or viewstate to retain the values of variables..
if you want to redirect to a different page , use session[]
else if you want to stay on the same page , use viewstate[]
In my Case the Problem Was Related to Iframe, One Time I removed the Iframe Everithing Work Fine
<iframe id="pdf2"
src="#"
width="100%"
height="100%">
</iframe>

UpdatePanel.Update(), nothing seems to happen

I'm working with an UpdatePanel that I'd like to refresh programmatically on the server side. The reason is I display some data that takes a pretty long time to load, so I need to display the page and some sort of progress meanwhile.
What I did is the following, on a page with one UpdatePanel and one ScriptManager:
protected void Page_Load(object sender, EventArgs e)
{
if(scriptManager.IsInAsyncPostBack)
testLabel.Text = "AfterUpdate";
else
jobsUpdatePanel.Update();
}
This does not what I'd like to do: I'd like the page to be displayed and immediately trigger an asynchronous update of the UpdatePanel in order to load the data - which is what I do instead of assigning another silly text to testLabel.
This is the markup of the UpdatePanel (leaving the ContentTemplete away for the sake of readability):
<asp:UpdatePanel ID="jobsUpdatePanel" UpdateMode="Conditional" ChildrenAsTriggers="true" runat="server">
There is no postback performed at all. Can anybody give me a hint what I'm doing wrong?
Matthias
You can't push an update from the server to the browser. What the Update method does is to include the contents of the update panel in an AJAX response, so for that to have any effect there has to be a response going back to the browser.
If you want a lengthy process to run on the server and get updates in the browser, you have to start the process in a separate thread, so that the main thread can complete and return the response to the browser. Then the browser can do postbacks or AJAX calls to the server and ask the background thread for the progress status.

Categories

Resources