单选按钮和下拉列表命令以创建寄存器表单

时间:2014-02-19 06:01:59

标签: c# asp.net visual-studio-2010 visual-studio

在这段代码中,cp.sex =我选择单独的单选按钮,并且我给出了cp.product = dropdownlist1.selected项目,但它显示错误。让任何人告诉我,给DOB的产品代码。

    Com_psi cp = new Com_psi();
    DAL_psi dp = new DAL_psi();
    cp.Psiid = int.Parse(TextBox1.Text);
    cp.Name=TextBox2.Text;
    cp.DOB = int.Parse(TextBox3.Text);
    cp.Sex = RadioButton1.
    cp.Mobile = int.Parse(TextBox4.Text);
    cp.Address = TextBox5.Text;
    cp.Product = DropDownList1.SelectedItem;
    cp.Amount = int.Parse(TextBox6.Text);
    int result = dp.insertpsi(cp);

    public int Psiid { get; set; }
    public string Name { get; set; }
    public int DOB { get; set; }
    public string Sex { get; set; }
    public int Mobile { get; set; }
    public string Address { get; set; }
    public string Product { get; set; }
    public int Amount { get; set; }
                                                                            

1 个答案:

答案 0 :(得分:1)

您可以使用Conditinal Operator检查是否检查了RadioButton。 如果选中,则指定“男性”,如果未选中,则可以指定“女性”。

解决方案1:如果RadioButton1值为Male

cp.Sex = (RadioButton1.Checked)?"Male":"FeMale";

解决方案2:如果RadioButton1值为FeMale

cp.Sex = (RadioButton1.Checked)?"FeMale":"Male";

它符合:

if(RadioButton1.Checked==true)
{
cp.Sex = "Male";
}
else
{
cp.Sex="FeMale";
}

解决方案3:对于DropdownList,您可以使用DropDownList1.SelectedItem.Value

试试这个:

cp.Product = DropDownList1.SelectedItem.Value;