Home > Asp.net > Call a C# Method from aspx Page

Call a C# Method from aspx Page


Sometimes you need to call a method written in codebehind(.aspx.cs) from .aspx page.

Here is a simple example:

In your aspx page

<asp:Label ID=”lbllable” runat=”server” text=’<%# GetCustomerID() %>’ />
In your codebehind(.aspx.cs) page

  1. private string _CustomerID = “8888″;
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. lbllable.DataBind();
  5. }
  6. public string GetCustomerID
  7. {
  8. get { return this._CustomerID; }
  9. }

When you are using in gridview or any other data bound control then

in your aspx page
<%# GetEmployee(Eval(“EmpId”))%>

in your codebehind

public object GetEmployee(object EmpId)
{

EmpId = 1234;
return EmpId;
}

About these ads
  1. Sapan Kumar Singh
    December 21, 2011 at 10:04 am | #1

    It’s nice.
    I exact same searching for making url .
    thanks a lot

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: