Tuesday, March 17, 2009

Persist data from a user control back to the page

This is maybe something very specific to my scenario, but I needed to persist data from a user control back to the page holding this control.

I used the following.

In the Page Class: (MyPage)
------------------
I declared the class as such

class MyPage : BasePage, MyFilter.IFilterPersist

I added the following methods

public string GetLastSearchSupplier(string controlId)
{
if (ViewState[controlId] == null) ViewState[controlId] = "-12";
return (string)ViewState[controlId];
}
public void SetLastSearchSupplier(string controlId, string value)
{
ViewState[controlId] = value;
}

In the User Control: (MyFilter)
--------------------

I declared an Interface

public interface IFilterPersist
{
string GetLastSearchSupplier(string controlId);
void SetLastSearchSupplier(string controlId, string value);
}

I declared an instance of the filter

IFilterPersist ownerI;

In the constructor I instantiated the filter

this.ownerI = parentPage as IFilterPersist;

Where I wanted to Get or Set this value to persist it to the containing page I did this

call GetLastSearch... / SetLastSearch...(userControl.ID, "sdsad");

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.

Thursday, December 18, 2008

Unit Testing - Distributed Transaction Coordinator

Unit Testing error while working with Transactions for the Database.

While testing the using "(var transaction = new TransactionScope())" method stated in a previous unit testing blog, I ran into an error that the code in my class could not access my local DB.

I was shown that I need to run the Distributed Transaction Coordinator. This is found under the Windows Services and can be set to automatic.

It's working fine now.

ASP.NET - Page Life Cycle

Page life cycles must be used in the correct matter.

One thing I found working with legacy systems is to make sure you use the page life cycle correctly. There are some good cheat sheets on this - I might add it in here later.

To read up on this on the MSDN site go here.

Dictionaries - KeyValuePair

I needed to use a better collection.

I was working on a legacy framework using HashTables. My colleague suggested to rather use Dictionaries (since they are serializable). I then stumbles over this article explaining how using KeyValuePair is faster than using the regular Keys in a foreach loop. Read more here

Here's an example:
foreach (KeyValuePair pair in d)
{
Console.WriteLine(pair.Key + ", " + pair.Value.ToString());
}

Unit Testing - Database Entries

I needed to unit test some DB entries.

I got this code sample from a colleague of mine.

[TestMethod()]
public void SaveThisTest()
{
using (var transaction = new TransactionScope())
{
//do the target creation
//do the Assert(s)
//dont call transaction.Complete(); //Not calling it will cause a "rollback"
}
}

And that's it... read more here
(this guy has a different point of view, but I have not tried it)