Archive for April, 2013

Restrict the size of file uploads in ASP.NET

For Specific directories rather than entire application:

<location path="Upload">
    <system.web>
        <httpRuntime executionTimeout="9999" maxRequestLength="2097151" />
    </system.web>
</location>

For individual uploader:
int maxSize = 2097151;
if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLeng­ th <= maxSize)
{
   //put some logic

}

			

Setting up Web.config to allow uploading of large files in ASP.net C#

During the construction of an intranet application that allows users to upload documents and share information between users, I observed that when a user tries to upload files that are larger than 4 MB, the file is not uploaded, and he gets a HTTP 401.1 error.

The Problem

By default, Machine.config is configured to accept HTTP Requests upto 4096 KB (4 MB) and it is reflected in all your ASP.NET applications. We can change the Machine.config file directly, or you can change only theWeb.config file of the application(s) you want to.

The Solution

Open your Web.config file, and just below the <system.web> tag, add the following tag:

<httpRuntime 
executionTimeout="90" 
maxRequestLength="4096" 
useFullyQualifiedRedirectUrl="false" 
minFreeThreads="8" 
minLocalRequestFreeThreads="4" 
appRequestQueueLimit="100" 
enableVersionHeader="true"
/>

Now, just take a look at the maxRequestLength=”4096″ attribute of the <httpRuntime> tag.

You can change the value to some other value of your choice.

webconfig setting

Problem during set focus in textbox while DropShadowExtender / RoundedCornersExtender is used

I want to set focus on textbox when page load. But when I use dropshadowextender or RoundedCornersExtender, I am not able to set focus on textbox at page load.

I use ToolkitScriptManager in login page.

Login Page code is as below

<tr style=”height: 30px;”>

<td style=”width: 20%”>
&nbsp;
</td>
<td class=”Login_Text” style=”width: 15%”>
Password
</td>
<td class=”Login_Text” style=”width: 5%”>
:
</td>
<td style=”width: 40%”>
<asp:TextBox ID=”TxtPwd” runat=”server” MaxLength=”15″ 
TextMode=”Password” ToolTip=”Please Enter Password.” Width=”171px”></asp:TextBox>
<asp:RequiredFieldValidator ID=”ReqPass” runat=”server” ControlToValidate=”TxtPwd”
Display=”None” ErrorMessage=”Enter Password” SetFocusOnError=”True”></asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID=”VCEPWD” runat=”server” TargetControlID=”ReqPass”>
</cc1:ValidatorCalloutExtender>
</td>
<td style=”width: 20%”>
&nbsp;
</td>
</tr>
<tr style=”height: 30px;”>
<td style=”width: 20%”>
&nbsp;
</td>
<td class=”Login_Text” style=”width: 15%”>
&nbsp;
</td>
<td class=”Login_Text” style=”width: 5%”>
&nbsp;
</td>
<td style=”width: 40%”>
&nbsp;&nbsp;<asp:ImageButton ID=”BTNLOGIN” runat=”server” ImageUrl=”~/Images/LOGIN/LOGIN_BTN.PNG”
OnClick=”BTNLOGIN_Click” />
</td>
<td style=”width: 20%”>
&nbsp;
</td>
</tr>
</table>
</asp:Panel>
<cc1:DropShadowExtender ID=”DSE1″ runat=”server” TargetControlID=”pnlUserLogin” BehaviorID=”DropShadowBehavior1″
Width=”3″ Rounded=”true” Radius=”20″ Opacity=”.5″ TrackPosition=”true”>
</cc1:DropShadowExtender>

Solution : 

Put below code to set focus in textbox after extender control. 100% working solution

<script type=”text/javascript” language=”javascript”>
function pageLoad(){
$get(“<%=TxtUid_Alias.ClientID%>”).focus();
}
</script>

 

 

Overwrite Confirmtext message in ConfirmButtonExtender

Original code in Usercontrol

<asp:Button ID=”btndel” runat=”server” Enabled=”False” Text=”Disable”
ToolTip=”Delete” Width=”100px” onclick=”btndel_Click” />
<cc1:ConfirmButtonExtender TargetControlID=”btndel” runat=”server” ID=”cbe1″

ConfirmText=”Do you want to delete record? Are You sure?”>

</cc1:ConfirmButtonExtender>

Write below code in page_load event in code behind code

On Page_Load event

AjaxControlToolkit.ConfirmButtonExtender cbe = (AjaxControlToolkit.ConfirmButtonExtender)Page_Action1.FindControl(“cbe1”);

cbe.ConfirmText = “Do you want to Disable record ? Are you sure ?”;

This code is used when programmer wants to display different message on different situation using same button event.

Here I have explained usercontrol button event.