Example with Functions


Example to call a Function from the click Event of a Button

Create a New Webpage Design the Webpage

 

Select the Button submit ---- Go to properties window ----Click on Events Button ----Go to click Event, type F1 press enter key ---- Write the following code
      
public partial class CallFunBtn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void F1(object sender, EventArgs e)
    {
        txtSample.Text = "Welcome Sampath ";
    }
}


Run the WebPage & Check


Example to call A Function from two or more Buttons

Create A New Webpage, Design the Webpage



  • Select the Button1 ---- Go to properties window  ---- click on Events Button  ----Go to click Event  ----Type the Function name F1  ---- Double Click  -----It will generate the following function
  • Switch to design view  ---- select the Button 2  ---- go to properties window ----click on Events Button  ----go to click Event  -----Type the Function name F1 ----Double Click on it  ---write the following code

protected void F1(object sender, EventArgs e)
{
        txtSample.Text = "Welcome";
}

  • Here in the above example, if the user click on Button  B1 or Button B2 same task will be performed but we want to perform different task, when user clicks on B1 and B2 Buttons, By calling the same Function for this purpose we use Command Name property but Command Name property not support by click Event and support by CommandEvent so we call the Function F1 from Command Event of both the Button and perform different task
Example to perform different tasks by calling the same Function from two or more Buttons:

Create a New web page, Design the Webpage




  • Select the btnWelcome --- Go to properties window --- Type ‘Wel’ in CommandName property
  • Click on Events Button ---Go to Command Event Type “F1” press enter key
  • Select the Button btnHellow--- Go to properties window --- Type ‘Hel’ in Command name property 

  •  Go to CommandEvent type the Function name ‘F1’ --- Double Click there
 
protected void F1(object sender, CommandEventArgs e)
{
        if (e.CommandName == "Wel")
            txtSample.Text = "Welcome";
        else if (e.CommandName == "Hel")
            txtSample.Text = "Hellow";

 }

  • Run the WebPage & Check


No comments:

Post a Comment