Call (Execute) function (Method) in page load using C# in ASP.Net

tanweeruddinbeeharry
 
on Sep 17, 2021 11:06 AM
619 Views

How to call a function in page load asp.net

The function below is found in a class called page.cs

public static List<string> MyFunction()
{
    var retList = new List<string>();        
    retList.Add("xzy");
    retList.Add("abc");
        
    return retList;
}

On page load how to call the function and check if the list contains xzy and abc?

protected void Page_Load(object sender, EventArgs e)
{
    //call MyFunction()
}
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 18, 2021 01:48 AM

Hi tanweeruddinb...,

Check this example. Now please take its reference and correct your code.

Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public static class page
{
    public static List<string> MyFunction()
    {
        var retList = new List<string>();
        retList.Add("xzy");
        retList.Add("abc");

        return retList;
    }
}

Code

protected void Page_Load(object sender, EventArgs e)
{
    List<string> lists = page.MyFunction();
    if (lists.Contains("xzy") || lists.Contains("abc"))
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Available');", true);
    }
}