0

i'm developing a web application for my company, but i'm coming up against a very simple problem that i don't now how to resolve. I search a lot on internet but i can't find anything. I need to call a code behind method from an asp.net controls passing variables.

This is the method in the code behind (file.aspx.cs):

protected void SayHello(object sender, EventArgs e, String RandomName)
{
  Response.Write(PrintedString);
 }

And this is a asp.net control that call the method through OnLoad event :

<asp:Label ID="Label1" runat="server" Text="Hello" OnLoad="Visibility('ciao mamma')"></asp:Label>

What's wrong with this simple thing? Where i'm wrong?

Please answer to this simple question, it's driving me crazy...Thanks.

3 Answers3

1

You can set the value to label on Page_Load event

You can use below code.

protected void Page_Load(object sender, EventArgs e)

{

   string UserName="test";
   Label1.Text="Hello "+ UserName;

}

prog1011
  • 3,425
  • 3
  • 30
  • 57
1

You can pass an argument to the event handler by the following way, but please note it will work for only asp button.

<asp:Button ID="btn" runat="server" Text="Click" OnCommand="btn_Command" CommandArgument="YourArgumentValue"/>

and then in the code behind file you do like this :

protected void btn_Command(object sender, CommandEventArgs e)
{
   Response.Write(e.CommandArgument);
}

It will work for only asp button because they have a onCommand attribute with them.

whenever you will click on the button this code behind file code gets executed.

Hope this helps.

Vishesh
  • 112
  • 3
  • 14
0

Is this what you need?

Markup:

<asp:Label ID="lbl" runat="server"><%=SayHello("foo") %></asp:Label>

Code behind:

protected string SayHello(string name)
{
    return "Hello " + name;
}
Slippery Pete
  • 3,051
  • 1
  • 13
  • 15
  • No, i write an example. What I need is call a function passing a parameter value. I call a method with an event (for example OnLoad) with a parameter, like every programming language. In the code behind I do some operation with this parameter that I passed from the event in the web form (for example response.write(parameter)). – Mattia Quaglietta Aug 29 '14 at 18:20
  • There is a misunderstanding here. I don't want set the label's value, i use it as example. I need to call a method through a event from the web form (file.aspx) to a method declared in the code behind (file.aspx.cs), passing custom parameter to the method. – Mattia Quaglietta Aug 30 '14 at 15:09