Tuesday, January 27, 2009

Setting focus after a postback

I kept loosing focus after the Anthem.net postback.

The issue was that I needed to remain focus on a textbox a user selected even after the previous textbox made a postback in order to do eg. textchanged....

I found THIS piece of code.

Including Javascript block from within C# code

I needed to write some Javascript from within C#

The problem is that I can't add it directly to the file since we use classes which forms our form. So I stumbled across this piece of code which will help!

Default RE: How to implement ClientScript.RegisterClientScriptInclude
Howdy,

1. declaratively (aspx page code)


or programmatically in master page load / init/ prerender / any event
handlers:
Page.ClientScript.RegisterClientScriptInclude("my" ,
ResolveClientUrl("~/scripts/timer.js"));

2. i reckon instead of ClientScript.RegisterClientScriptSource you meant
ClientScript.RegisterClientScriptBlock (or if i got you wrong, do you use
store javascripts in a resource file?)
If i got you right, try this:

protected void Page_Load(object sender, EventArgs e)
{
RegisterMessageScript("Hello World!");
}

private void RegisterMessageScript(string message)
{
System.Text.StringBuilder script =
new System.Text.StringBuilder();

script.Append("\n");

Type type = this.GetType();

if(!ClientScript.IsClientScriptBlockRegistered(typ e, "messageScript"))
ClientScript.RegisterClientScriptBlock(type, "messageScript",
script.ToString());

}

hope this helps

Milosz

You can view it online here.