Sunday, May 31, 2009

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;

}

0 comments: