Example with CommandArgument & ClientClick Property ,Postback Nature of the Webpage:



Example with CommandName

Create the webpage and design the web page


  • Set the following properties---- selects the Button btnAdd----Go to properties window, type Add in CommandName property
  • select Button btnSubtract ----Go to properties window, type Subtract in Command Name property
  •  select Button btnMultiply----Go to properties window, type Multiply in CommandName property
  • select Button btnDivide ----Go to properties window, type Divide in CommandName property
  •  Call the same Function F1( ) from all Buttons(i.e.,btnAdd,btnSubtract,btnMultiply,btnDivision) Command Event ---- write the following code

    protected void F1(object sender, CommandEventArgs e)
   {
        int a,b,c=0;
        a=Convert.ToInt32(txtNum1.Text);
        b = Convert.ToInt32(txtNum2.Text);
        if (e.CommandName == "Add")
            c = a + b;
        else if (e.CommandArgument == "Subtract")
            c = a - b;
        else if (e.CommandName == "Multiply")
            c = a * b;
        else if (e.CommandName == "Division")
            c = a /b;
        txtResult.Text = c.ToString();
    }

                     
    Run the Webpage & Check



Example with CommandArgument property:-


Create the web page & Design the web page



  • Select the Button btnSubmit
  • Go to properties window, type Sampath in CommandArgument property
  • Click on Events Button
  • Double  click on CommandEvent & write the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CommandArgument : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Command(object sender, CommandEventArgs e)
    {
        txtSample.Text = e.CommandArgument.ToString();
    }
}




Example to work with on OnClientClick Property:-

In General [client side code will be in javascript code]

  • Select btnSubmit Go to Properties window
  • Go to OnClientClick property
  • Type the Function name F1( )
  • Go to source part and Writethe following code  Before </head> tag

<script type="text/javascript">

Function F1()
{
        alert("Hellow Sampath");
}

    </script>


PostBack nature of the web page in ASP.NET
  • In general when user types the request for any web page from the browser window by typing the required URL address, we call this as 1st request for the web page
  • User may click on any Button or other Controls of the page then page request will be resubmitted as PostBack
  • This result will called as PostBack request and the nature called PostBack nature of the web page
  • To identify whether the request 1st request or PostBack request, we use the property of the web page ‘Is PostBack’
  • This property is a Boolean property stores false at 1st request and true at PostBack Request
Example with PostBack nature

Create a New Web Page ,Design the Webpage   
  • Goto Source & write the following code in  Page_Load Event and Button_Click Event
protected void Page_Load(object sender, EventArgs e)
{
        Response.Write("Welcome from PageLoad");
 }
 protected void btn_Click(object sender, EventArgs e)
 {
        Response.Write("Hellow from Button Click");
 }

         Run the WebPage & Check
  • If we want to display Welcome only at first Request & don’t want to run PageLoad Event code at PostBack .We Modify the Code Like:--

protected void Page_Load(object sender, EventArgs e)
     {
        if(IsPostBack==false)
        Response.Write("Welcome from PageLoad");
     }





No comments:

Post a Comment