Pages

Wednesday, 25 July 2012

How to handle large file uploads which exceeds the configured request length Say you have in web config :


Snippet
<httpRuntime maxRequestLength="4048" />

Cause:
When you try to upload a file grater then 4MB you will get an error with the status of 404.13. To catch this error we have to utilise Application_Error as .net framework does not generate a request object and initiate a page lifecycle for this type of request. So no events get fired, but first of all you will end up in a error page.
What we can do is:
In Global.asax file:
protected void Application_Error(object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Url.ToString().Contains("FileUpload.aspx") && 
        HttpContext.Current.Error.InnerException.Message.Contains("Maximum request length exceeded"))
    {
        HttpContext.Current.ClearError();
        HttpContext.Current.Response.Redirect(
            string.Format("~/FileUpload.aspx?Message={0}""Upload file is over the allowed limit"));
    }
}

Then you can FileUpload.aspx like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string message = this.Request.QueryString.Get("Message");
            if (!string.IsNullOrEmpty(message)) this.lblMessage.Text = message;
            else this.lblMessage.Visible = false;
        }
        protected void Save(object sender, EventArgs e)
        {
            
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="lblMessage" />
        <asp:FileUpload runat="server" ID="fuFile" />
        <asp:Button runat="server" ID="btnSave" Text="Save" />
    </div>
    </form>
</body>
</html>

How to print ASCII charactors in asp.net


<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            TableRow tr = new TableRow();
            int index = 0;
            for (int i = 31; i < 256; i++)
            {
                string text = string.Format("{0:000} - (&#{0})", i);
                if (index % 10 == 0)
                {
                    tr = new TableRow();
                    this.tblCharSet.Rows.Add(tr);
                }
                tr.Cells.Add(new TableCell() { Text = text });
                this.ddlCharactors.Items.Add(new ListItem(string.Format("{0:000}", i), i.ToString()));
                index++;
            }
        }     
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DropDownList ID="ddlCharactors" runat="server" />
        <asp:Table runat="server" ID="tblCharSet" />
    </form>
</body>
</html>

How to stop a asp.net button being postback to server


Attach an OnClientClick java script function with return inline. From the function return false based on your logic
<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
    <script type="text/javascript">
        function Validate() {
            alert("Validate Controls");
            var isValid = false;
            if (isValid)
                document.forms[0].submit();
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button runat="server" ID="btnLogin" 
            OnClientClick="javascript:return Validate()" 
            Text="Login" />
    </form>
</body>
</html>

How to stop a asp.net button being postback to server


Attach an OnClientClick java script function with return inline. From the function return false based on your logic
<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
    <script type="text/javascript">
        function Validate() {
            alert("Validate Controls");
            var isValid = false;
            if (isValid)
                document.forms[0].submit();
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button runat="server" ID="btnLogin" 
            OnClientClick="javascript:return Validate()" 
            Text="Login" />
    </form>
</body>
</html>