Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have created 4 radio button..
XML
<div class="reg-child">
  <div class="reg-child-text">Body type :</div>
    <div class="red-child-input" id="change_basic_bodytype">
     <input type="radio"  value="Slim"  runat="server" name="Body type" id="rado_slim" /><label for="rado_slim" >Slim</label>
     <input type="radio"  value="Average" runat="server" name="Body type" id="rado_average" /><label for="rado_average" >Average</label>
      <input type="radio"  value="Athletic" runat="server" name="Body type" id="rado_athletic" /><label for="rado_athletic" >Athletic</label>
         <input type="radio"  value="Heavy" runat="server" name="Body type" id="rado_heavy" /><label for="rado_heavy" >Heavy</label>
    </div>
 </div>


when i select the above value and click submit button,the particular value passed into other textbox..this condition is successfully working..when i click the submit button without selecting a radiobutton..a "undefined" value saved in database the old data automatically deleted and that position "undefined" ..can u solve this problem
Posted
Comments
Richard C Bishop 3-Apr-14 15:05pm    
Use validation to ensure required fields are not null.
Sergey Alexandrovich Kryukov 3-Apr-14 15:07pm    
What does it mean: "value passed into other textbox"? Data goes to server side, whatever it is... It comes under the name defined by the attribute "value". No matter what the problem is, you don't show any server-side and no client-side code. So, what's the use of your HTML code sample? It remains totally unclear what is screwed up.
—SA

Try this code...

this the aspx code
ASP.NET
    <asp:radiobuttonlist id="rbtn_option" runat="server" xmlns:asp="#unknown">
        RepeatDirection="Horizontal">
        <asp:listitem>item1</asp:listitem>
        <asp:listitem>item2</asp:listitem>
        <asp:listitem>item3</asp:listitem>
        <asp:listitem>item4</asp:listitem>
    </asp:radiobuttonlist>

    <asp:textbox id="txt_val" runat="server" xmlns:asp="#unknown"></asp:textbox>
    <asp:button id="btn_submit" runat="server" onclick="btn_submit_Click" xmlns:asp="#unknown">
        Text="Submit" />
</asp:button>


this the behind code
C#
protected void btn_submit_Click(object sender, EventArgs e)
{
    if (rbtn_option.SelectedIndex < 0)
        return;

    txt_val.Text = rbtn_option.SelectedValue;
}



I hope it will help you.,

thank you and God bless..
 
Share this answer
 
While submitting, check if Radio Buttons are selected or not. If selected, then read the value and save to database, else don't save.

I am sure, you are not checking this and directly saving the value.
 
Share this answer
 
Hi rajeeshmenoth,

Try this

For DEMO: Chick Here

HTML
ASP.NET
<div class="reg-child">
  <div class="reg-child-text">Body type :
    <input type="text"  runat="server" name="Body type" id="textbox" /></div>
    <div class="red-child-input" id="change_basic_bodytype">
     <input type="radio"  value="Slim"  runat="server" name="Body_type" id="rado_slim" /><label for="rado_slim" >Slim</label>
     <input type="radio"  value="Average" runat="server" name="Body_type" id="rado_average" /><label for="rado_average" >Average</label>
      <input type="radio"  value="Athletic" runat="server" name="Body_type" id="rado_athletic" /><label for="rado_athletic" >Athletic</label>
         <input type="radio"  value="Heavy" runat="server" name="Body_type" id="rado_heavy" /><label for="rado_heavy" >Heavy</label>
        
    </div>
    <input type="button" id="btnStatus" value="Radio Button status" />
    <input type="button" id="btnReset" value="Radio Reset" />
 </div>


JQ
JavaScript
$(document).ready(function() {
   $('#btnStatus').click(function(){
       var Checked_value = $('input:radio[name=Body_type]:checked').val();
      if(Checked_value !=undefined)
      {
       $('#textbox').val(Checked_value);
      }
      else
      {
          $('#textbox').val('');
          alert("select any option");
      }
      
   });
    // for reset
     $('#btnReset').click(function(){
         $('input:radio[name=Body_type]:checked').prop('checked', false);
         $('#textbox').val('');
     });
});


For DEMO: Chick Here

I hope this may help you
 
Share this answer
 
v2
If you do not select any value, then the selected value of radio buttons is 'undefinedd'

Solution:

Either mark any radiobutton selected by default: every time there will be a value
OR Add a check that if user do not select any of the radiobutton, validation will be shown
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900