如何使用单选按钮 [英] how to work on Radio button

查看:62
本文介绍了如何使用单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TrainingFeedback.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="100%" >
    <tr>
    <td>Ques</td>
    <td>Noopinion (0)</td>
    <td>Agree (1)</td>
    <td>disagree (2)</td>
    <td></td>
    </tr>
     <tr>
    <td>&nbsp;</td>
    <td>
        &nbsp;</td>
    <td>
        &nbsp;</td>
    <td>
        &nbsp;</td>
    <td>&nbsp;</td>
    </tr>
     <tr>
    <td>&nbsp;</td>
    <td>
        &nbsp;</td>
    <td>
        &nbsp;</td>
    <td>
        &nbsp;</td>
    <td>&nbsp;</td>
    </tr>
     <tr>
    <td>R u agree</td>
    <td><asp:RadioButton ID="RadioButton1" runat="server" GroupName="age1"

            AutoPostBack="True" Checked="True"

            oncheckedchanged="RadioButton1_CheckedChanged1" /></td>
    <td><asp:RadioButton ID="RadioButton2" runat="server" GroupName="age1"

            AutoPostBack="True" Checked="True"  /></td>
    <td><asp:RadioButton ID="RadioButton3" runat="server" GroupName="age1"

            AutoPostBack="True" Checked="True"  /></td>
    <td></td>
    </tr>
     <tr>
    <td>how much will provide the rating</td>
    <td><asp:RadioButton ID="RadioButton4" runat="server" GroupName="age2" AutoPostBack="True"  /></td>
    <td><asp:RadioButton ID="RadioButton5" runat="server" GroupName="age2" AutoPostBack="True"   /></td>
    <td><asp:RadioButton ID="RadioButton6" runat="server" GroupName="age2" AutoPostBack="True"  /></td>
    <td></td>
    </tr>
     <tr>
    <td>Training</td>
    <td>&nbsp;</td>
    <td><asp:RadioButton ID="RadioButton7" runat="server" GroupName="age3" OnCheckedChanged="RadioButtonage3_CheckedChanged" AutoPostBack="true" />Short</td>
    <td><asp:RadioButton ID="RadioButton8" runat="server" GroupName="age3" OnCheckedChanged="RadioButtonage3_CheckedChanged"  AutoPostBack="true"/>Long</td>
    <td>&nbsp;</td>
    </tr>
     <tr>
    <td>&nbsp;</td>
    <td>
        <asp:Button ID="Button1" runat="server" Text="Button" Height="48px"

            Width="109px" />
         </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>









IN .CS代码







IN .CS Code

public partial class WebForm1 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string agreeValue = string.Empty;
        if (RadioButton1.Checked)
            agreeValue = "No Opinion";
        else if (RadioButton2.Checked)
            agreeValue = "Agree";
        else if (RadioButton3.Checked)
            agreeValue = "Disagree";

        da = new SqlDataAdapter("Insert Into dfg(q1,q2,q3)values('" ++ "','" ++ "','" ++ "')", con);
        ds = new DataSet();
        da.Fill(ds);
    }
}
}



注意: - 我想将数据存储在数据库(sql)其实我不知道单选按钮如何工作

推荐答案

首先尝试了解两个按钮的用途然后再做你的工作...

RadioButtonList 控制这些控件的主要目的是相互排除,如果选择了另一个项目,则可以从N个项目中选择仅一个,前一个项目未经检查。 br />


CheckBoxList 控制这些控件的主要目的是用于多个项目的互斥可以从N个项目中选择。

从下面获取两个值并保存到数据库



First Try to Understand the purpose of both buttons then do your work...
RadioButtonList control the main aim of these controls is Mutual Exclusion that is one can select only one out of N items if another item is selected the previous one gets unchecked.

CheckBoxList control the main aim of these controls is Mutual Exclusion that is for multiple items can select from N items.
Get values from both of them like below and save to database

//If Item is radiobutton list
string valueFromRadioButtonlist = RadioButtonList1.SelectedItem.Text;

//if item is checkbox list
List<ListItem> selected = new List<ListItem>();
string valueFromCheckBoxList = "";
foreach (ListItem item in CheckBoxList1.Items)
    if (item.Selected) valueFromCheckBoxList += item.Text + ",";


你需要有一个scriptmanager,updatepanel .aspx

并为你的所有设置AutoPostback = true Radiobuttons。



.Aspx代码:

You need to have a scriptmanager, updatepanel in you .aspx
And set AutoPostback=true for all your Radiobuttons.

.Aspx code:
<asp:ScriptManager ID="ScriptManager1" runat="server">
          </asp:ScriptManager>

      <asp:UpdatePanel ID="up1" UpdateMode="Always" runat="server">
      <ContentTemplate>
      <asp:RadioButton ID="RadioButton1" runat="server" GroupName="Group1"

          oncheckedchanged="RadioButton1_CheckedChanged" Text="No Opinion" AutoPostBack="true" />
      <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Group1"

          oncheckedchanged="RadioButton2_CheckedChanged" Text="Agree" AutoPostBack="true"/>
      <asp:RadioButton ID="RadioButton3" runat="server" GroupName="Group1"

          oncheckedchanged="RadioButton3_CheckedChanged" Text="Disagree"/>
          </ContentTemplate>
      </asp:UpdatePanel>





.cs code:





.cs code:

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
       {
           if(RadioButton1.Checked)
           UpdateDB("No Opinion");
       }

       protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
       {
           if(RadioButton2.Checked)
           UpdateDB("Agree");
       }




private void UpdateDB(string agreeValue)
      {
        // Your database updation goes here 
         lbl1.Text = agreeValue;
      }


Suggestion



1. You can use Bit DataType for that Column in SQL Table.

If RadioButton is checked, store 1 in Database, else store 0.



2. Use Parameterized Query instead of Inline Queries to avoid SQL Injection Attack.
Suggestion

1. You can use Bit DataType for that Column in SQL Table.
If RadioButton is checked, store 1 in Database, else store 0.

2. Use Parameterized Query instead of Inline Queries to avoid SQL Injection Attack.


这篇关于如何使用单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆