Daniel Sh.
Daniel Sh.

Reputation: 2074

How to ASP Button onClick= "js variable"?

jQuery

$(document).ready(function () {
        $('#ButtonRadioValue').click(function () {
            var selValue = $('input[name=radio]:checked').val();
        });
    });

Radio

<div id="radio" align="center">
    <input type="radio" id="radio1" value="BtnNextCaseClick" name="radio" checked="checked"/><label for="radio1" style="width: 109px; margin-right:-.9em">Next</label>
    <input type="radio" id="radio2" value="CloseCase" name="radio" /><label for="radio2" style="margin-right:-.9em">Close Case</label>
    <input type="radio" id="radio3" value="CloseWeb" name="radio" /><label for="radio3" style="width: 109px">Close Web</label>
</div>   

So, below that radio I have a button, and I want it to execute different code behind according to radio selection. That value is stored in selValue. So how can I tell onclick event to run what's inside of selValue

Button

<asp:Button ID="ButtonRadioValue" CssClass="customButton" runat="server" onclick=" js variable here" Text="Aceptar" style="width: 111px; height: 30px"/>

Thanks.

EDIT

Since there was no real need for client-side procedure. I did it from Server side with this.radio1.Checked == true

Upvotes: 0

Views: 276

Answers (4)

freefaller
freefaller

Reputation: 19963

Update: It appears I have misunderstood what is required, so I will leave my original below

If you want to know which radio button was selected on the post-back, then either make the radio's server-side controls (either by converting into <asp:RadioButton> or simply adding runat="server" to them. Then you can test for radio1.Checked, etc.

Otherwise you can check the form data using Request.Form("radio1"). If result is Nothing (VB.Net) or null (C#) then the radio was not selected. If there is a value there, you know the radio is selected.


Original / incorrect assumption

Unless I've misunderstood the issue, you don't need to have a special attribute in the <asp:Button> as you can do this just in the jQuery you've already written.

Add the following after your var selValue...

$("#" + selValue).trigger("click");

To produce...

$(document).ready(function () {
    $('#ButtonRadioValue').click(function () {
        var selValue = $('input[name=radio]:checked').val();
        $("#" + selValue).trigger("click");
    });
});

One thing to note is that if the control is within Master Page, Placeholder, etc, then the button will need the full ClientID, otherwise the jQuery will not find it


As a side note, the OnClick attribute of the <asp:Button> is used to wire-up the event handler for the click on a post-back to the server. If you want to run javascript then you want to use the OnClientClick attribute, which will produce an onclick attribute in the rendered HTML

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can try with OnClientClick event in order to call client function

Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.button.onclientclick(v=vs.80).aspx

Server Side : OnClick

Client Side : OnClientClick

Upvotes: 1

Konstantin Dinev
Konstantin Dinev

Reputation: 34915

The Click is a server event. If you want JS code to execute use onclientclick.

Upvotes: 1

davehale23
davehale23

Reputation: 4372

It sounds like you are looking for eval().

var selValue = $('input[name=radio]:checked').val();
eval(selValue);

Upvotes: 1

Related Questions