February 10, 2014 06:49 by
Peter
I have one page that contains one file upload control to accept files from user and saving it in one folder. I have written code to upload file and saving it to folder it’s working fine after completion of my application my friend has tested my application like he uploaded large size file nearly 10 MB file at that time it’s shown the error page like “the page cannot displayed”. This topic contains only brief information about ASP.NET, if you're loooking for ASP.NET Hosting and want to be more familiar with ASP.NET, you should try HostForLIFE.eu.
Again I have search in net I found that file upload control allows maximum file size is 4MB for that reason if we upload file size larger than 4MB we will get error page like “the page cannot displayed” or “Maximum request length exceeded”. After that I tried to increase the size of uploaded file by setting some properties in web.config file like this:
<system.web>
<httpRuntime executionTimeout="9999" maxRequestLength="2097151"/>
</system.web>
Here httpRuntime means: Configures ASP.NET HTTP runtime settings. This section can be declared at the machine, site, application, and subdirectory levels.
executionTimeout means: Indicates the maximum number of seconds that a request is allowed to execute before being automatically shut down by ASP.NET.
maxRequestLength means: Indicates the maximum file upload size supported by ASP.NET. This limit can be used to prevent denial of service attacks caused by users posting large files to the server. The size specified is in kilobytes. The default is 4096 KB (4 MB).
After that write the following code in aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
After that write the following code in code behind
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (FileUpload1.PostedFile.ContentLength < 20728650)
{
try
{
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
Label1.Text = "File size exceeds maximum limit 20 MB.";
}
}
}
After that write the following code in web.config
<system.web>
<httpRuntime executionTimeout="9999" maxRequestLength="2097151"/>
</system.web>
February 5, 2014 15:41 by
Peter
In this example i am showing how to use Cross Page Posting or Postback In ASP.NET 2.0, 3.5, 4.0 Using C#. Cross Page posting is used to submit a form on one page (say default.aspx) and retrieve values of controls of this page on another page (say Default2.aspx). There are two ways we can use cross page postsbacks in ASP.NET.
1st method
In this i've created a Default.aspx page with two textbox and one button , button click will post back to Default2.aspx and there we will retrieve and show values of both textboxes Html source of Default.aspx page is like
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
First Name:
<asp:TextBox ID="txtFirstName" runat="server">
</asp:TextBox><br /><br />
Last Name:
<asp:TextBox ID="txtLastName" runat="server">
</asp:TextBox><br /><br /><br />
<asp:Button ID="btnSubmit" runat="server"
OnClick="btnSubmit_Click"
PostBackUrl="~/Default2.aspx"
Text="Submit to Second Page" /><br />
</div>
</form>
</body>
</html>
Don't forget to set PostBackUrl Property of Button
PostBackUrl="~/Default2.aspx"
Now to retrieve values of textBoxes on Default2.aspx page, write below mentioned code in Page_Load event of second page (Default2.aspx)
protected void Page_Load(object sender, EventArgs e)
{
//Check whether previous page is cross page post back or not
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
TextBox txtPbFirstName = (TextBox)PreviousPage.FindControl("txtFirstName");
TextBox txtPbLastName = (TextBox)PreviousPage.FindControl("txtLastName");
Label1.Text = "Welcome " + txtPbFirstName.Text + " " + txtPbLastName.Text;
}
else
{
Response.Redirect("Default.aspx");
}
}
If you are using masterpages then you need to write code to FindControl as mentioned below
ContentPlaceHolder exampleHolder =(ContentPlaceHolder)Page.PreviousPage.Form.FindControl ("Content1"));
TextBox txtExample = exampleHolder.FindControl("txtFirstName");
2nd Method
Using Property to expose and Consume values of TextBox
If we are using this method then we don't need to use FindControl method at all
For this we need to create property in code behind of the page to be cross page post back (Default.aspx)
Html of the page needs no changes ,
C# code behind for Default.aspx
public TextBox pbTxtFirstName
{
get
{
return txtFirstName;
}
}
public TextBox pbTxtLastName
{
get
{
return txtLastName;
}
}
Now write this code in page_Load event of second page to retrieve values of controls
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Label1.Text = "Welcome " + PreviousPage.pbTxtFirstName.Text + " " + PreviousPage.pbTxtLastName.Text;
}
else
{
Response.Redirect("Default.aspx");
}
}
ASP.NET 4.5 Preview introduces new model binding for ASP.NET web forms. The concept of model binding was first introduced with ASP.NET MVC and now it has incorporated with ASP.NET Web Forms. You can easily perform any CURD operation with any sort of data controls using any data access technology like Entity Framework, ADO.NET, LINQ to SQL Etc. In this post I am going talk about how you can bind the data with ASP.NET DropdownList using new Model Binding features.
Let’s say we have a speaker database and we wants to bind the name of the speakers with the DropDownList. First placed an ASP.NET Dropdown control with the page and set the “DataTextField” and “DataValueField” properties.
We can set the ddlName.DataSource to specifying the data source from the code behind and bind the data with dropdpwnlist, but in this case from the code behind to providing the data source.
Now, instead of specifying the DataSource, we will be setting the Dropdownlists SelectMethod property to point a method GetSpeakerNames() within the code-behind file.
Select method is expected to return us result of type IQueryable<TYPE>. Here is GetSpeakerName() method is defined as follows.
So, Instead of specifying the data source we are specifying the SelectMethod, which return the IQueryable type of Speaker object. Run the application, you will find the names binded with dropdown list. Hope this helps !
March 11, 2013 06:11 by
Scott
In this article I try to explain the default submit behavior of form and panel. Suppose, you want to press/click submit button on Enter key press or you are trying to post the form on Enter key press. In asp.net, to achieve this functionality we need to set "Defaultbutton" property either in Form or in panel.
Form DefaultButton Property
<form id="form1" runat="server" defaultbutton="btnSubmit">
<div>
<asp:TextBox ID="txtUserID" runat="server"/> <asp:TextBox ID="txtUserpwd" runat="server"/> <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit _Click" Text="Submit"/>
</div>
</form>
Panel DefaultButton Property
<asp:Panel ID="Panel1" runat="server" defaultbutton="btnSubmit">
<div>
<asp:TextBox ID="txtUserID" runat="server"/> <asp:TextBox ID="txtUserpwd" runat="server"/> <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit _Click" Text="Submit"/>
</div>
</asp:Panel >
Note
1. We specify the defaultbutton property at the Form level in the form tag when there is only one Submit Button for post back.
2. We specify the defaultbutton property at the Panel level in the Panel tag when there are multiple Submit Button for post back.