CSharp Commands


Whiel using the AJAXToolkit i came up with the situation where i needed to run some javascript after a server-side event was called. After poking around for a bit i found the command “ScriptManager.RegisterClientScriptBlock”. This command sends back asynchronously javascript that will be executed by the clients browser on update. Here is an example:-

string strscript = "var cbut=$get('programmaticPopup');alert(cbut.id+' '+cbut.style.position);";
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ToggleScript", strscript, true);

this.Page is your pages current Page object, “ToggleScript” is the name you are giving the script block, strscript is a string containg the javascript and following that is true which is there to tell the command to suround the inputed javascript with “

p.s. you will notice the javascript command $get(elementname), this returns the object with the ID you gave it at design time just like document.getElementById();. If you look at the source of a running script all the ID tags have strange strings appended to the start of the ID name you gave it. $get finds the element without all the crap. Its part of the AJAX Toolkit.

Here is a function to redimension an array in csharp:-

private static void ReDim(ref TextBox[] arr, int length)
    {
        try
        {
            if (arr == null)
            {
                arr = new TextBox[length];
            }
            else
            {
                if (arr.Length != length)
                {
                    TextBox[] arrTemp = new TextBox[length];
                    if (length > arr.Length)
                    {
                        Array.Copy(arr, 0, arrTemp, 0, arr.Length);
                        arr = arrTemp;
                    }
                    else
                    {
                        Array.Copy(arr, 0, arrTemp, 0, length);
                        arr = arrTemp;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            string x = ex.ToString();
            //Console.WriteLine(x);
        }
    }

This example is for a string array but you can overload it with whatever type you want.

Here is a way to check if a string is a number:-

public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
    {
        Double result;
        return Double.TryParse(val, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);
    }

Here is a link so you can figure out what to put in the “NumberStyle” field.
http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx

In a project i had been working on i wanted to have a central MasterPageFile that had variable sub MasterPageFile’s. I finally figured out that you have to override the “OnPreInit” Event on the page you wish to modify. Here is an example:-

protected override void OnPreInit(EventArgs e)
    {
 
        if (Web_Security.IsSU())
        {
            this.MasterPageFile = "~/management/SUMasterPage.master";
        }
        else
        {
            this.MasterPageFile = "~/management/MemberMasterPage.master";
        }
        base.OnPreInit(e);
 
    }

Hope this saves you some time.

Here is an example on how to delete files in the file sytem:-

public static void DeleteFile(string FilePath)
    {
        try
        {
            FileInfo TheFile = new FileInfo(HttpContext.Current.Server.MapPath(FilePath));
            if (TheFile.Exists)
            {
                File.Delete(HttpContext.Current.Server.MapPath(FilePath));
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        catch (FileNotFoundException e)
        {
            LogEntry log = new LogEntry();
            log.EventId = 701;
            log.Message = "Error Deleting File\r\n" + FilePath + "\r\n" + e.ToString() + "\r\n" + e.StackTrace;
            log.Categories.Add("General");
            log.Severity = TraceEventType.Information;
            log.Priority = 5;
            Logger.Write(log);
        }
        catch (Exception e)
        {
            LogEntry log = new LogEntry();
            log.EventId = 1301;
            log.Message = "Error Deleting File Exception\r\n" + FilePath + "\r\n" + e.ToString() + "\r\n" + e.StackTrace;
            log.Categories.Add("General");
            log.Severity = TraceEventType.Information;
            log.Priority = 5;
            Logger.Write(log);
        }
 
    }

The example code above has some custom error exception handling that you may want to remove.