4

I'm using the event click() to call a method in Code Behind, like this:

HTML

<asp:button bordercolor="White" id="btnAddGS" onclick="AddGSBandeira" runat="server">

JAVASCRIPT

$("#ctl00_ContentPlaceHolder1_btnAddGS").click();

C#

public void AddGSBandeira(object sender, EventArgs e)
{

}

Its work normally, but I need to pass a param in the javascript call, like this:

$("#ctl00_ContentPlaceHolder1_btnAddGS").click("param");

But I do not know how this works ...can anybody help?

2 Answers 2

1

The best thing to do is create a hidden control and populate it's value with JavasScript on the click event. Your code behind will be able to access that value on your postback (AJAX or otherwise).

Markup

<asp:HiddenField ID="myHiddenField" runat="server" />
<asp:button bordercolor="White" id="btnAddGS" 
            onclick="AddGSBandeira" 
            onclientclick="SetHiddenValue()" runat="server">

JavaScript

function SetHiddenValue()
{
    document.getElementById("<%=myHiddenField.ClientID%>").value = "[Your value here]";
}

C#

public void AddGSBandeira(object sender, EventArgs e){}
{
    var jsVal = myHiddenField.Value;
}
Sign up to request clarification or add additional context in comments.

5 Comments

the problem is, that I have an ID in the javascript.. And this ID i need in my Codebehind.. So how I can give this ID from JS to Codebehind, to work with it?
you can access it in code behind like that: myHiddenField.Value
@Fender, as stated in my post :)
I think comments should be blocked while edits are being made :)
finally got it, it worked.. i tried it he whole time with a <asp:hiddenfield>.. i didnt work, so I took a normal HTML hiddenfield and it worked, thx 4 answer :)
0

You can do this with trigger.

http://api.jquery.com/trigger/

$("#ctl00_ContentPlaceHolder1_btnAddGS").trigger("click",["param"]);

I believe the 2nd parameter to trigger should be an array of arguments to pass to the function.

3 Comments

thx for your answer! But how can I handle this Param from my Method in the CodeBehind? protected void button_clicked(object sender, EventArgs e, int param){} ??
@Fender I don't agree with you. kingjiv has offered an alternative way to reach the goal from eMi.
@reporter: this does not pass a parameter to the server side function. it passes a parameter to the client side click function and this is not what eMi wants

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.