1

I have this code on the client side:

<asp:imagebutton id="ctrlSend" runat="server" ImageUrl="Btn_New.gif" onclick="ctrlSend_Click"></asp:imagebutton>

And I have this function receiving the click:

protected void ctrlSend_Click(object sender, System.Web.UI.ImageClickEventArgs e)

This works fine however I want to add a dynamic parameter which I will calculate using Javascript on the client side and be passed to the ctrlSend_Click function.

Can somebody please explain how to do this? I have explored using CommandArgument however I believe that cannot be set via a Javascript call before hand. I want the argument to be calculated on button click.

1
  • 3
    How are you planning on calling the javascript function? Also this looks more like ASP.net webforms than MVC, correct me if I'm wrong. Commented Jul 9, 2014 at 1:28

2 Answers 2

4

So you can't do it with the CommandArgument. What you can do is have a hidden field, set that with javascript and access that in the serverside event handler.

.aspx

<asp:imagebutton id="ctrlSend" runat="server" ImageUrl="Btn_New.gif" onclick="ctrlSend_Click" OnClientClick="setParameter"></asp:imagebutton>
<input type="hidden" id="clickParameter" runat="server" />

Note the runat="server" on the hidden field. This makes it a .net accesible control. Also note the OnClientClick attribute on the image button.

javascript

function setParameter(){
     var hdnField = document.getElementById("<%=clickParameter.ClientID %>");
     //Use Client ID to avoid any ASP.net name mangling

     hdnField.value = ;//Whatever your logic is to set this value;

     return true; //Should now ensure the form is submitted.

}

C# Code Behind

protected void ctrlSend_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
    string param = clickParameter.value;
    //Cast and convert your parameter as needed from a string.
}

There may be some syntax errors in the above, but it should get you going in the right direction.

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

Comments

1

This is not possible since javascript runs in the browser after the server side is done executing. You have to make an ajax request in order to pass data from javascript to the server.

3 Comments

I'd say that's not entirely true. You could have some client side javascript run before the form is submitted on button click, form submit etc. This could then set a value in a hidden form field, which is then picked up server-side. Passing it directly, you're right, can't be done.
Yes, but that will only work if there is a button click, which won't be the case on the initial render of the page
Correct. It's unclear at what stage the OP wants to set this parameter. I assumed on click/submit....but you know what they say about assumptions!

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.