1

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.

1
  • Set the label text in page_load of your code-behind? Commented Aug 29, 2014 at 17:39

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is what i was searching for. So i realize that i can't pass parameters in this way like in javascript: <asp:Button ID="btn" runat="server" Text="Click" OnClick="SayHello(parameters)"/>
@MattiaQuaglietta yes i think so, i tried in every way but failed.
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;

}

1 Comment

@Slippery Pete Please click upvote or click 'correct' if above answer is useful.
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;
}

2 Comments

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)).
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.