Asp.Net Tips and Tricks


1.
Call remote methods: This can be done in 2 ways,

a.    1st
that u have a restful svc like a webAPI that binds every HTTP verb to a method

b.    2nd in
other cases like mine, where I had a simple asp.net web app, my home.aspx was
calling svc.aspx and passing the method to be executed as  a “action” query
string : AJAXResponse.aspx?action=btn1_Clk&ID=” + iD;

Now in the c# code, in case u have called AJAXResponse.aspx, then first make sure that in its aspx file all u have is : <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AJAXResponse.aspx.cs" Inherits="RFBAppUsingInterface.AJAXResponse" %>

 

 And in it’s .cs file’s Page_Load(),
check the action and call the method:

string action = HttpContext.Current.Request["action"];

            if (action == " btn1_Clk")
            {
                string ID = HttpContext.Current.Request["iD"];
                if (ID != null)
                    btn_Clk(ID);
            }
 

2.
SESION

a.       Read
from session:  HttpContext.Current.Session[“abc”])

3.  Response
JSON

          Response.Clear(); //MAKE SURE U DO THIS

            RESULT = PerformAction(dfd);

 
            var json = new JavaScriptSerializer();

            var resp = new JSONResponse();

            resp.a = df;
            resp.b = 
            resp.c = 
            Response.Write(json.Serialize(resp));
        }
    }

    public class JSONResponse
    {
        public string a { getset; }
        public string b { getset; }
        public string c { getset; }
    }

4.  Load the file at runtime: string path = HostingEnvironment.MapPath(@"~/App_Data/abc_" + Environment + ".xml");
                                                 var stream = new StreamReader(path);
                                                   config = Helper.LoadRBFConfiguration(stream);

5.  Role
of Global.asax:Whatever needs to be done only at Application Start should be put in Application_Start() of Global.asax.cs.

Also, if you want something to persist, create it here, Eg. If u want int a globally, all u have to do in home.aspx is : Global.a

6.  TO register anything: Page.ClientScript.RegisterStartupScript(GetType(), “ex”,

                                                                "alert('" +
                                                                "U don’t have access"');"true);

7.  In
home.aspx.designer.cs, add:

/// <summary>
        /// GroupPnl control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Panel GroupPnl;

Below are some changes to it:

a.  GroupPnl.Height = Unit.Percentage(100);
b.  GroupPnl.BackColor = System.Drawing.Color.Silver;
c.  CheckPanel.BackColor = Color.FromArgb(207, 238, 248);
d.  abc.BorderStyle = BorderStyle.Double;
e.  sd.OnClientClick = "CheckAll_ClkXMLHttpReq(event);return false;";// Note that in case you don’t do return false; here, it will do a page reload even though onclk u are executing a js function..
f.  GroupPnl.Controls.Add(new LiteralControl(
                            "

&nbsp;    

 +
                            pair.Key + "</span></span></p>");
g.  GroupCheckStatus.Click += new EventHandler(CheckGroup_Click); //Server Side Event
h.  GroupCheckStatus.Click += (sender, e) => CheckGroup_Click(sender, e); //Server Side Event
i.  If you hide some element on server side (ie in aspx page), then its not send over in the html, so u cannot show it using js latr..hence hide and show using js only…
j.   var Response = new TextBox(); Response.Wrap = true; OR  DetailsText.TextMode = TextBoxMode.MultiLine; // Show multi line in textbox

8.
If a solution has multiple projects, say A is dll proj and B is web
app (asp.net), the A can read the web.config from B

9.
To be able to override any machine.config tag in ur app, the
registration of that tag in m.c (under <configSections>)should be set as allowDefinition=MachineToApplication” or allowDefinition=Everywhere

10.

11.

 

 

 

–>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s