选中复选框时,将值插入文本框

时间:2019-04-02 15:05:54

标签: c# asp.net sql-server visual-studio

我有一个使用SQL添加供应商的表格

SupplierID表中有SupplierNameSupplierProduceTypeSupplierPhoneSupplier

Form page

我的ID,姓名和电话没有问题,但是由于有1-5个选项可供选择,因此产品类型比较困难

如果要检查5个框,我希望文本框显示beef,pork,chicken,lamb,sauce(用逗号分隔),显然如果只选择了3个,我只希望这3个框显示

当我点击添加供应商按钮时,SQL代码将为:

sql = "INSERT INTO[Supplier](SupplierID, SupplierName, SupplierProduceType, SupplierPhone) Values('" + Convert.ToInt32(SupplierIDTB.Text) + "', '" + SupplierNameTB.Text + "', '" + SupplierProduceTypeTB.Text + "', '" + Convert.ToInt32(SupplierPhoneTB.Text) + "')";

因此,当sql代码从SupplierProduceTypeTB.Text读取时,它将读取“牛肉,猪肉,鸡肉,羊肉,调味料”(如果全部5个都选中),并将其输入到SupplierProduceType列中

到目前为止,我有:

protected void BeefCB_CheckedChanged(object sender, EventArgs e)
    {
        String input1 = "beef,";
        SupplierProduceTypeTB.Text += input1;
    }

但这什么都不做!

2 个答案:

答案 0 :(得分:0)

您只需要在代码中进行一些更改。

尝试以下代码

     protected void BeefCB_CheckedChanged(object sender, EventArgs e)
     {
           SupplierProduceType();
     }
     protected void PorkCB_CheckedChanged(object sender, EventArgs e)
     {
           SupplierProduceType();
     }
     protected void ChickenCB_CheckedChanged(object sender, EventArgs e)
     {
           SupplierProduceType();
     }
     protected void LambCB_CheckedChanged(object sender, EventArgs e)
     {
           SupplierProduceType();
     }
     protected void SauceCB_CheckedChanged(object sender, EventArgs e)
     {
           SupplierProduceType();
     }

     private void SupplierProduceType()
     {
           string produceType = string.Empty;
           if(BeefCB.Checked)
           {
               produceType = "Beef, ";
           }
           if(PorkCB_Checked)
           {
               produceType += "Pork, ";
           }
           if(ChickenCB_Checked)
           {
               produceType += "Chicken, ";
           }
           if(LambCB_Checked)
           {
               produceType += "Lamb, ";
           }
           if(SauceCB_Checked)
           {
               produceType += "Sauce, ";
           }
           SupplierProduceTypeTB.Text = produceType;
    }

+ =不适用于textbox.Text属性。

通过创建函数来尝试使用此函数,并在每个复选框checkchange方法中调用它。

答案 1 :(得分:0)

所以我的解决方法是在添加供应商按钮中执行if语句:

if (BeefCB.Checked) {
            String input1 = "beef,";
            string textboxValue = SupplierProduceTypeTB.Text;
            SupplierProduceTypeTB.Text = textboxValue + input1;
        }

        if (PorkCB.Checked) {
            String input1 = "pork,";
            string textboxValue = SupplierProduceTypeTB.Text;
            SupplierProduceTypeTB.Text = textboxValue + input1;
        }

        if (ChickenCB.Checked) {
            String input1 = "chicken,";
            string textboxValue = SupplierProduceTypeTB.Text;
            SupplierProduceTypeTB.Text = textboxValue + input1;
        }

        if (LambCB.Checked) {
            String input1 = "lamb,";
            string textboxValue = SupplierProduceTypeTB.Text;
            SupplierProduceTypeTB.Text = textboxValue + input1;
        }

        if (SauceCB.Checked) {
            String input1 = "sauce";
            string textboxValue = SupplierProduceTypeTB.Text;
            SupplierProduceTypeTB.Text = textboxValue + input1;
        }