November 2007 - Posts

 In this blog post, i will be showing different ways to pass data between webforms. Different techniques could be implemented, it all depends on what serves you most!  We all know that Http is stateless, so data should be stored somewhere in order to be able to retrieve it.
         
        1- Query String    
        2- Cookies
        3- Session variables
        4- Cross Page Posting
        5- Submit form
        6- Server.Transfer or Server.Execute

We will talk in details about each one and which kind of data it could store.
    
    1- Querystrings: Using Query string variables, you can pass data between webforms. below is an example
        
             Ex: Suppose you want to pass the TextBox1.Text variable from WebForm1 to WebForm2 on button click event.
                    
                   protected void Button1_Click(object sender, EventArgs e)
                   {
                        Response.Redirect("WebForm2.aspx?id=" + TextBox1.Text);
                   }
                
                 To Read the value of "id" in WebForm2, you should use the below code
                 string queryStringID = Request.QueryString["id"];
            
                 Now queryStringID will hold the data from the querystring.

    
    2- Cookies: AS you might already know, cookies are small text files stored in the client machine. Cookies can only store up to approximately 4 kbs.
                      Once the cookie is stored into the client machine, each request from the client to your application, the web browser will look for the cookie and send                                 it via the Request Object.
        
                      Ex: To store a value of TextBox1.Text inside the cookie use the below code
            
                            protected void Button1_Click(object sender, EventArgs e)
                            {
                               HttpCookie cookie = new HttpCookie("UserName");
                               cookie.Value = TextBox1.Text;
                               cookie.Expires = DateTime.Now.AddDays(1);
                               Response.Cookies.Add(cookie);
                               Response.Redirect("WebForm2.aspx");
                             }
                      Now in webform2 page_load event, you should write the below code to get the value
                  
                          if(Request.Cookies["UserName"] != null)
                             Response.Write(Request.Cookies["UserName"].Value);

    3- Session Variables: By default, session variables are stored in the webserver's memory. Session variables are unique per each user.
                
                                      Ex: To store a value inside a session variable use the below code
            
                                            protected void Button1_Click(object sender, EventArgs e)
                                            {
                                                Session["UserName"] = TextBox1.Text;
                                                Response.Redirect("WebForm2.aspx");
                                            }

                                        To retrieve the value from WebForm2 use the below code
    
                                            Response.Write(Session["UserName"]);

    4- Cross Page Posting (ASP.NET 2.0): Cross page posting was introduced in ASP.NET 2.0. I already have a blog post about it.

                                                                Ex: The same example is taken from my previous blog post
            
                                                                      Set the Button1 PostBackUrl property to WebForm2.aspx

                                                                      Now in WebForm2.aspx, Write the below code to get the value of textbox1

                                                                       TextBox txtFirstPage = (TextBox)(PreviousPage.FindControl("TextBox1"));
                                                                       Response.Write(String.Format("You wrote {0} in Page1",txtFirstPage.Text));

    5- Submit Form: You can submit the form in Webform1.aspx to webform2.aspx. In that case, you can retrieve all the WebForm1's form element from                                                        webform2. What we will do in the below example is to add another form ( not a server side one) which will have two HTML controls; the first is an  
                              submit control the second is a hidden field which will have the value of TextBox1 web server control to be posted to webform2
        
                              Ex: now the HTML code of webform1 will look like below

                                    <form id="Form1" method="post" runat="server">
                                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                    </form>
                                    <form name="SubmittedForm" action="WebForm2.aspx" method="post">
                                    <input id="Submit1" type="submit" value="submit" onclick="CopyTextToHiddenField()" />
                                    <input name="Hidden1" type="hidden" />
                                    </form>
        
                            Of course you realized the we handled the onclick event of the submit button which will call a javascript function to copy the value of TextBox1 into                                 the hidden field (Hidden1) in order to post it to webform2. Also you so in the second form tag the action attribute in which we specified to which                                         page the second form (SubmittedForm) will be posted.
            
                            Add the below javascript code inside the <head> tag of WebForm1
            
                            <script language="javascript">
                            function CopyTextToHiddenField()
                            {
                                var textbox1Value = document.getElementById("<%=TextBox1.ClientID%>").value;
                                document.forms[1].document.getElementById("Hidden1").value = textbox1Value;
                            }
                            </script>

                            Now you retrieve the value of "Hidden1" hidden field in webform 2 using the below code
            
                             Response.Write(Request.Form["Hidden1"]);   

     6- Server.Transfer & Server.Execute: These two functions take 2 parameters; the first is the webform name to which you want to redirec the user the                                                                                     second is a bool parameter to specify if you want the form to be reserved.
            
                                                                Ex:
                                                                      protected void Button1_Click1(object sender, EventArgs e)
                                                                      {  
                                                                        Server.Transfer("WebForm2.aspx", true);
                                                                      }

            
                                                                Now in webform2 page_load event use the below code to get the value of TextBox1
            
                                                                      Response.Write(Request.Form["TextBox1"]);
                                                               Same Code for Server.Execute..

Hope this Helps

Posted by haissam | 9 comment(s)

Dotnetslackers will be rewarding the top 3 contributors to the forums per each calendar month with prizes ranging from Xbox 360’s to 3rd party software components and books!

Participate now! be a member of DNS community....

Regards, 

Posted by haissam | with no comments

Microsoft Released a service pack for .NET Framework 2.0. This release provides security improvements, and prerequisite feature support for .NET Framework 3.0 Service Pack 1, and .NET Framework 3.5

Download it from

.NET Framework 2.0 SP1

Regards,

Posted by haissam | 2 comment(s)

If you want to pass for example a textbox value from one webform to another, we usually do that using either the query string or the Session variable. ASP.NET 2.0 introduced Cross Page Posting which allow us to retrieve the value of the textbox in the second webform.

Now let's jump to the code.

For this purpose, Create two pages (Page1.aspx, and Page2.aspx)

Page1.aspx will have two controls
 1- A textbox (TextBox1).
 2- A Button (Button1)
 
Set the PostBackUrl property of the Button1 to Page2.aspx.

Now in Page2.aspx page_load event, Write the below code

 TextBox txtFirstPage = (TextBox)(PreviousPage.FindControl("TextBox1"));
 Response.Write(String.Format("You wrote {0} in Page1",txtFirstPage.Text));

When Page2.aspx is loaded, you will see that you were able to retrieve the value you entered on Page1.

Hope this helps,

Posted by haissam | with no comments

In this blog post, i will show how to map urls in your ASP.NET application. Suppose you have a link to an aspx page which has a long guid name. To make it simple for you to use is to map this url into a simpler one.
First you need to enable this feature from the web.config file in your application folder by adding under the <System.Web> tags the below

 <urlMappings enabled="true">
   <add url="~/Test.aspx" mappedUrl="Default-guid-1232-1233-44431-3223.aspx"/>
 </urlMappings>

If you don't have an aspx page called "Default-guid-1232-1233-44431-3223.aspx", Create one to continue with this demo.

Now Open Default.aspx page, on button_click event write the below code
Response.Redirect("~/Test.aspx");

Run Default.aspx, click the button, you will see that you are being redirected to Default-guid-1232-1233-44431-3223.aspx WebForm.Eventhough we didn't have a physical file called "Test.aspx".


As you saw, Using url mappings is straight forward and easy to use.

Hope this helps,

Posted by haissam | with no comments

Today I was amazed to find out that Microsoft has released VS2008. We all have been waiting for the new visual studio to find out the new features.

This can be downloaded by MSDN Subscribers.

For more information check below link

VS 2008

Regards,

Posted by haissam | with no comments

If you have for example two ASP.NET applications and you want to share the authentication cookie between those application, you can do that using the SSO.

To configure your applications to share the authentication cookie, each application has to have same configurations in the web.config file. One important notice in here that the name, protection and path attributes should be the same also in the two web.config files

<authentication mode="Forms" >
        <forms loginUrl="login.aspx" name=".ASPXAUTH" protection="All" timeout="90" path="/" />
      </authentication>

      <machineKey validationKey="C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE" decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F"
          validation="SHA1" />

Copy the above configurations into the web.config.


Best Regards,

 

Posted by haissam | with no comments

 Microsoft is set to release a pair of low-cost enterprise search products, a move one company official said comes at a time when the market in question is "at a tipping point."

The products, Search Server 2008 and Search Server Express 2008, are based on technology pulled out of SharePoint Server 2007, according to Jared Spataro, group product manager for enterprise search at Microsoft.

Check it out at

www.microsoft.com/enterprisesearch

Best Regards,

 

Posted by haissam | with no comments

Microsoft Corp. on Monday committed to releasing Visual Studio 2008 and its .Net Framework 3.5 technology by the end of this month

For more information, check out below link

VS2008

Regards, 

Posted by haissam | with no comments

The Windows Live™ ID Client 1.0 software development kit (SDK) provides a managed API for Windows Live sign-in from within desktop client applications. Included in the release is a sample application together with its source code so that you can build your own client applications.

For more information, please check out below link

http://www.microsoft.com/downloads/details.aspx?familyid=B5A78784-922D-4267-A6E9-5D2ECF1DCED8&displaylang=en

HC

Posted by haissam | with no comments
The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.