0

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

3
  • Are you wanting to "click" the button that is referenced (by name) in the value of the selected radio button? Commented Oct 11, 2012 at 11:59
  • radio1-radio2-radio3. and below them an "OK" button. When user clicks OK, I want to fire some code behind. And there are 3 chances of different code behind blocks. What one is fired depends on the radio selection by user. What's inside of selValue should be the VALUE of those 3 radios. Commented Oct 11, 2012 at 12:04
  • If you're purely after the selected radio, why don't you make the radio controls runat="server" and then you can see what is selected on the post-back? Commented Oct 11, 2012 at 12:07

4 Answers 4

1

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

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

4 Comments

Not following completely. This would eliminate my aspbutton?
@Daniel - apologies, I misunderstood what you were trying to do, I have updated my answer to hopefully be more what you're after - if not, then I simply don't understand your question
Thank you very much! Values at server-side is what I needed, I mixed it up a little bit with the js. But now it's done. Thanks again.
Ok, good, glad I finally figured out what you needed! Good luck with the rest of your project @Daniel :-)
1

It sounds like you are looking for eval().

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

Comments

1

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

Comments

1

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

Comments

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.