Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Server.execute in asp.net - Part 54

Suggested Videos
Part 51 - Different page navigation techniques
Part 52 - Response.Redirect in asp.net
Part 53 - Server.Transfer in asp.net

The following are the different page navigation techniques in asp.net
1. Hyperlink control - Discussed in Part 13 and Part 51 of the ASP.NET video series
2. Response.Redirect - Discussed in Part 52
3. Server.Transfer - Discussed in Part 53
4. Server.Execute
5. Cross-Page postback
6. Window.Open 



In this video we will discuss about 
1. Server.Execute method
2. Difference between Server.Transfer and Server.Execute

Server.Transfer and Server.Execute are similar in many ways.
1. The URL in the browser remains the first page URL.
2. Server.Transfer and Server.Execute can only be used to navigate to sites/pages on the same web server. Trying to navigate to sites/pages on a different web server, causes runtime exception.
3. Server.Transfer and Server.Execute preserves the Form Variables from the original request.

The major difference between Server.Transfer and Server.Execute is that, Server.Transfer terminates the execution of the current page and starts the execution of the new page, where as Server.Execute process the second Web form without leaving the first Web form. After completing the execution of the first webform, the control returns to the second webform.



WebForm1.aspx code:
<div style="font-family: Arial">
    <table>
        <tr>
            <td colspan="2">
                <h1>
                    This is WebForm1</h1>
            </td>
        </tr>
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                :<asp:TextBox ID="txtName" runat="server">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <b>Email</b>
            </td>
            <td>
                :<asp:TextBox ID="txtEmail" runat="server">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnExecute" runat="server" 
                Text="Server.Execute - WebForm2" 
                Width="250px" onclick="btnExecute_Click"/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnExecuteToExternalWebsite" 
                runat="server" Width="250px" 
                Text="Server.Execute - External WebSite" 
                onclick="btnExecuteToExternalWebsite_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblStatus" ForeColor="Green" 
                Font-Bold="true" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
</div>

WebForm1.aspx.cs code:
protected void btnExecute_Click(object sender, EventArgs e)
{
    Server.Execute("~/WebForm2.aspx", true);
    lblStatus.Text = "The call returned after processing the second webform";
}
protected void btnExecuteToExternalWebsite_Click(object sender, EventArgs e)
{
    Server.Execute("http://pragimtech.com/home.aspx");
}

WebForm2.aspx code:
<div style="font-family: Arial">
    <table>
        <tr>
            <td colspan="2">
                <h1>This is WebForm2</h1>
            </td>
        </tr>
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                :<asp:Label ID="lblName" runat="server">
                </asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <b>Email</b>
            </td>
            <td>
                :<asp:Label ID="lblEmail" runat="server">
                </asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnPostBack" runat="server" 
                Text="Simply Post Back" />
            </td>
        </tr>
    </table>
</div>

WebForm2.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
    System.Collections.Specialized.NameValueCollection previousFormcollection = Request.Form;
    lblName.Text = previousFormcollection["txtName"];
    lblEmail.Text = previousFormcollection["txtEmail"];

    //Page previousPage = Page.PreviousPage;
    //if (previousPage != null)
    //{
    //    lblName.Text = ((TextBox)previousPage.FindControl("txtName")).Text;
    //    lblEmail.Text = ((TextBox)previousPage.FindControl("txtEmail")).Text;
    //}
}

6 comments:

  1. Really Super Sir , God Bless you

    ReplyDelete
  2. Hi,
    Is the following line correct? (this line is the last line in this part)

    "After completing the execution of the first webform, the control returns to the second webform"
    Thanks
    Sudarsan Pradhan

    ReplyDelete
    Replies
    1. When Server.Execute is used, a URL is passed to it as a parameter, and the control moves to this new page. Execution of code happens on the new page. Once code execution gets over, the control returns to the initial page, just after where it was called. However, in the case of Server.Transfer, it works very much the same, the difference being the execution stops at the new page itself (means the control is'nt returned to the calling page).

      Delete
  3. Hi, In my case labels in WebForm2 donot retain their viewstate on clicking 'Postback' button. EnableViewState is true for both labels. What could be the cause?

    ReplyDelete
    Replies
    1. I kept many web forms within single web application and it worked as I created new project and copied them to new project. don't know why such things happen :)

      Delete
  4. maybe because OS problem, 64bit.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.