Archive for category C#
ASP.NET DataGrid – Export to CSV encoding issue
Posted by nlvraghavendra in .Net, C#, Uncategorized on March 23, 2012
If you are exporting an asp.net grid to the CSV format you might see some special characters appearing the exported file. This is because of not setting the right encoding format. I initially tried with UTF8, UTF32 and Unicode and none of them exported the data correctly. Finally I set the encoding to default like this -
Response.ContentEncoding = Encoding.Default;
Finally the single quotes and hypens were appearing correctly in the CSV. Please not that you have to HtmlDecode your data if you have already HtmlEncoded it.
Passing a value from external class to a asp.net page and binding it to a control
Posted by nlvraghavendra in .Net, C#, Events-delegates on June 25, 2010
Off late, I was working in a WCF application which involves all the crazy WCF stuffs like duplex contract, callback contract, asynchronous delegates etc. At one point I was in a position where I need to pass an item from an external class to an asp.net page and bind it to the control.
To call a function in a asp.net page from another class we have to either declare the function to be static (so that we can invoke it directly with the class name) or create an instance for the page class and call it through the object.
Both of them wont work because of the following reasons -
- If the function is static, it can’t operate on the controls present in the page (since the controls are not static)
- If you create an instance for that page class all the controls will be lost as a new instance of the page is created
Finally i achieve it through events and by declaring the function to be static.
//Create a delegate for the event
private delegate void DelegateHandler (type object);
//Create the event
private static event DelegateHandler RaiseEvent event;
//Create an object for the delegate
private static DelegateHandler oHandler = null;
//Add the function to be called from the static function to the delegate
oHandler = new DelegateHandler(OriginalFunctionToBeCalled);
//Add the delegate to the event
event += oHandler;
public static void StaticFunctionToBeCalled(Type obj)
{
event(obj); //Raise the event
}
private void OriginalFunctionToBeCalled(Type obj)
{
//bind the value to the control
}
Thats it. Hope it would be useful for someone.



