Posts Tagged 'ASP .NET'

Web Forms RegisterStartupScript Fails

I have been working on MVC for close to 4 years now and have been out of touch with Web Forms. In one of our product (web forms grrrrrrrrrrr) I had to help our my team who was facing a weird problem.

ClientScript.RegisterStartupScript block was not registering the javascript at all….We tried numerous methods (load, prerender etc) but it always failed. That is when my team mate found that the form tag is missing the runat=server and it worked!!!

So this if for people who are facing the exact same problem. Add the runat server to your form tag for your clientscript or startupscript to work.

Happy Programming!!!

Cheers,

Raja

Advertisements

Getting the controlname which caused postback

I am working on a site in which I use a lot of JQuery but there are some pages which I had to use Server Controls and there was a need to get which control caused the postback.  After a bit of searching I found this nice method which did the trick.


private string  getPostBackControlName()
{
 Control control = null;
 //first we will check the "__EVENTTARGET" because if post back made by       the controls
 //which used "_doPostBack" function also available in Request.Form collection.
 string ctrlname = Page.Request.Params["__EVENTTARGET"];
 if (ctrlname != null && ctrlname != String.Empty)
 {
 control = Page.FindControl(ctrlname);
 }
 // if __EVENTTARGET is null, the control is a button type and we need to
 // iterate over the form collection to find it
 else
 {
 string ctrlStr = String.Empty;
 Control c = null;
 foreach (string ctl in Page.Request.Form)
 {
 //handle ImageButton they having an additional "quasi-property" in their Id which identifies
 //mouse x and y coordinates
 if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
 {
 ctrlStr = ctl.Substring(0, ctl.Length - 2);
 c = Page.FindControl(ctrlStr);
 }
 else
 {
 c = Page.FindControl(ctl);
 }
 if (c is System.Web.UI.WebControls.Button ||
 c is System.Web.UI.WebControls.ImageButton)
 {
 control = c;
 break;
 }
 }
 }
 return control.ID;
}

Happy Programming!!!

Cheers,

Raja

Disappointed with Linq to Entity :-(

We have a scenario where in we have to check for the existence of User Information in a database (residing in production SQL Server) other than the application database. We are using LINQ to Entities as our data cum business layer (Isn’t that what it should be ???). Since I have to get the information from another database (or server rather) I didnt want to create another entity set so I just created a stored proc in my application database to check for that (we have linked server set up so just took 2 mins to write the SP). Now I cannot use the written stored procedure in my L 2 E since it is not returning any entity as such :-(. I searched online and could not find any direct methods to do so. We have to work around to make this happen. I hope they fix this in the forth coming releases. Do you guys know of any direct method by which I can make use of a function import which does not return a value or a scalar value?

Happy Programming!!!

Cheers,

Raja

Checkbox + JQuery Pattern

We were working on a project which includes multiple selection entries under multiple categories. All the checkboxes are dynamically generated (Database lookup) since those were master items maintained by the admins of the system. We were able to easily generate the checkboxes by bringing in data using JSON and dynamically appending checkboxes to a DIV using JQuery. Now the problem was to get all the selected entries per category. That is when we brought in the following pattern by which a good naming convention followed by the amazing power of Jquery came into play. We made sure that all our check boxes followed a particular pattern like chk+category+id(the primary id). And then wrote the function given below to get the selected items in a particular category.

function GetCheckedItems(item)
{
    var strIDList="";   
    //this is the pattern: the class is going to be the checkboxtype followed by checkbox ex. contactcheckbox
    //so to get the generalized checkbox typ replace the checkbox with ""
    //this would give us just .contact. so taking the substring from 1 which would give us contact
    var strSearch=item.toString().replace(('checkbox'),'').substring(1);  
    //now prefixing chk to it so that we get the checkbox name
    strSearch="chk"+strSearch;
    //alert (strSearch);
    $(item).each(
        function()
        {
            if ($(this).is(":checked"))
                //replacing the chk followed by the checkbox type to get the IDs
                strIDList+=$(this).attr("id").replace((strSearch),"") + ",";
        }
    )
    //getting the length to negate the last ,
    var strLength=strIDList.length - 1;
    return strIDList.substring(0,strLength);
}

Hope this helps.

Happy Programming!!!

Cheers,

Raja

Little hack to have localized images – ASP .NET

In my current project we are implementing localization to support Spanish. We thought it would be good if we have our header image change as per the language specification selected by the user. Obviously we thought of adding those images under their specific resource files but the problem was how to display them. Although we found the code to display those images dynamically (using Response.ContentType as image and Response.BinaryWrite) we were in search of a better solution. That is when I did this little hack to set the path for the src of the image from the resource file as given below:


<img id="imgTest" runat="server" alt="test image" src="<%$ Resources:ImageFile %>" meta:resourcekey="imgTestResource1"/>

And specified the path of the image to point to specific image files in the respective resource files. Works like a charm. I dont know if this is a good solution but it works fine. I would love to hear your feedback or better solutions if any.

Happy Programming!!!

Cheers,

Raja

Server side safe JSON

We are working on a project which involves a lot of JQuery and JSON. We wanted to make sure that the JSON does not fail to evaluate so I got the function given below (after a bit of searching) to make the string safe so that it successfully evaluates. There you go…..

// Make a string safe for JSON evaluation
private static string SafeJSON(string sIn)
{
    StringBuilder sbOut = new StringBuilder(sIn.Length); //using the StringBuilder Class for performance
    foreach (char ch in sIn) //Looping through the string (dont u think a counter is better???)
    {
        if (Char.IsControl(ch) || ch == '\'')
        {
            int ich = (int)ch;
            sbOut.Append(@"\u" + ich.ToString("x4"));
            continue;
        }
        else if (ch == '\"' || ch == '\\' || ch == '/')
        {
            sbOut.Append('\\');
        }
        sbOut.Append(ch);
    }
    return sbOut.ToString();
}

Hope this helps.

Happy Programming!!!

Cheers,

Raja