Archive

Archive for the ‘Asp.net’ Category

How to use ajax (update panel) to enable disbale panel based on selection


<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        public void SetAccountInfoStatus(object sender, EventArgs e)
        {
            this.pnlAccountInformation.Enabled = !this.ckboxAccountStatus.Checked;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>        
        <asp:UpdatePanel runat="server" ID="upnlAccountInformation">
            <ContentTemplate>
                <asp:CheckBox runat="server" ID="ckboxAccountStatus" Text="Account Closed" AutoPostBack="true" OnCheckedChanged="SetAccountInfoStatus" />
                <asp:Panel runat="server" ID="pnlAccountInformation">
                    <asp:TextBox runat="server" ID="txtAccountNo" Text="ACC12345676"></asp:TextBox>
                    <asp:TextBox runat="server" ID="txtMemeberNo" Text="MEM12345678"></asp:TextBox>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
Categories: Asp.net Tags: ,

How to avoid attribute value encording in Asp.net 4.0


.Net 4.0 is encoding values when using Attributes.Add. In previous versions it didn’t. With the new behaviour it is no longer possible to write attributes containing single quotes.
Here’s an example.

<asp:TextBox ID="txtTest" runat="server" />

txtTest.Attributes.Add(“onkeyup”, “keyuphandler(‘hello’)”);
With the application pool framework version set to 2.0 it produces the desired result:

<input name="txtTest" type="text" id="txtTest" onkeyup="keyuphandler('hello')" />

With it set to 4.0 it produces an undesirable result:

<input name="txtTest" type="text" id="Text1" onkeyup="keyuphandler('hello')" />

.Net 4.0 needs to be fixed to allow the developer to write attribute values containing single quotes.
 Solution:

<system.web>
 <httpRuntime 
   encoderType="ActiveTest.HtmlAttributeEncoder" 

Than implement the class as below:

namespace ActiveTest
{
    public class HtmlAttributeEncoder : HttpEncoder
    {
        protected override void HtmlAttributeEncode(string value, System.IO.TextWriter output)
        {
            output.Write(value);
        }
    }
}
Categories: Asp.net Tags:

How to invalidate a Response cache based on a condition


Snippet

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    this.Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(ValidateCacheOutput), null);
}
protected void ValidateCacheOutput(HttpContext context, object data, ref HttpValidationStatus status)
{
    string name = context.Request.QueryString.Get("name");
    if (name.ToLower().Equals("john"))
        status = HttpValidationStatus.IgnoreThisRequest;
    else
        status = HttpValidationStatus.Valid;
}
Categories: Asp.net Tags:

How to register a startup (document).ready(..) script only for the first page load


Snippet

    public partial class Test : Page
    {
        private string script = @"
            $(document).ready(function () {

            });
        ";
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!this.Page.IsPostBack)
                this.Page.ClientScript.RegisterStartupScript(this.GetType(),
                    this.GetType().Name, this.script, true);
        }
    }