0

I`ve got three radio buttons in my form. Every rbutton has own label as below

 <form method="post">

        <input type="radio" name="radio"  "onclick="removeField()">
        <label>Text1</label>
             <br>
        <input type="radio" name="radio" onclick="removeField()">
        <label>Text2</label>
            <br />
        <input type="radio" name="radio"  onclick="addField()">
        <b>Another</b>
            <br />

How I can get in my c# code label of radiobutton which is on?

miechooy
  • 3,178
  • 12
  • 34
  • 59

1 Answers1

1

You could just make the radio button's value be the same text as the label. When you post the form it should give you a key/value pair of 'radio=Text1' (or whatever.

<form method="post">

        <input type="radio" name="radio" value="Text1" "onclick="removeField()">
        <label>Text1</label>
             <br>
        <input type="radio" name="radio" value="Text2" onclick="removeField()">
        <label>Text2</label>
            <br />
        <input type="radio" name="radio" value="Another"  onclick="addField()">
        <b>Another</b>
  ...

A few additional notes:

If label wraps the radio button, it becomes clickable and will toggle the radio button. Make sure to put the 'onclick' on the label tag if you do that.

The name 'radio' for a form field is not illegal, but I would suggest it's complicated. That radio button represents a property of your viewmodel (or of something) so I would suggest calling it something like 'SelectedText' or whatever.

BlackjacketMack
  • 5,472
  • 28
  • 32