简体   繁体   English

如果选中复选框,则传递值

[英]Pass a value if a checkbox is checked

I have a form that has some textboxes (45) with CheckBoxes next to each, and a button. 我有一个包含一些文本框(45)和每个复选框旁边的复选框的表单。 When the button is pressed I want to pass the values of each Textbox that has a checked CheckBox to a void in another class and the ones that are not checked pass a null value. 当按下按钮时,我想将每个具有选中的CheckBox的文本框的值传递给另一个类中的void,而未选中的文本框的值则传递一个null值。

What I have at the moment is: Form1 我现在所拥有的是: Form1

private void button_Click(object sender, EventArgs e)
{
String Value1;
if (value1CheckBox.Checked)
{
    Value1 = value1TextBox.Text;
}
else
{
    Value1 = null;
}

String Value2;
if (value2CheckBox.Checked)
{
    Value2 = value1TextBox.Text;
}
else
{
    Value2 = null;
}

etc...

Form2 form2 = new Form2();
form2.insertSQL(Value1, Value2, etc...);
}

Form2 表格2

private void insertSQL(String Value1, String Value2, String etc...)
{
    /*
    Code to insert to SQL database
    */
}

But that seams very inefficient and I'm sure there must be a better way to pass the values if the boxes are checked. 但这接缝效率很低,而且我敢肯定,如果选中了这些框,则必须有一种更好的方法来传递值。 Any advice on a better way to do this would be appreciated, also sorry if I have used the wrong terminology I am very new to programing. 如果有关于更好的方法的任何建议,将不胜感激,如果我使用了错误的术语,对编程我还是很陌生,也将感到抱歉。

A good idea would be to store all your check boxes and text boxes into a Dictionary. 一个好主意是将所有复选框和文本框存储到字典中。 The particular dictionary you would need to create would be: 您需要创建的特定词典是:

Dictionary checkToText = new Dictionary(); 字典checkToText = new Dictionary();

Then, put each CheckBox/TextBox pair mapping into the dictionary. 然后,将每个CheckBox / TextBox对映射放入字典中。

Finally, in the button_Clicked method, create a loop that loops through each key/value pair in your dictionary: 最后,在button_Clicked方法中,创建一个循环,循环遍历字典中的每个键/值对:

foreach (KeyValuePair<CheckBox, TextBox> pair in checkToText)
{
//do what you need to do
//pair.Key for checkbox
//pair.Value for textbox
}

I think the limitation you are getting at is that unchecked checkboxes do not get passed as part of a form post. 我认为您遇到的限制是未选中的复选框不会作为表单发布的一部分传递。

You can assemble a javascript that, onclick, will assemble some JSON and put it into a hidden value, then deserialize the JSON from the hidden value (in your server code). 您可以组装一个JavaScript,在onclick上将组装一些JSON并将其放入隐藏值中,然后从隐藏值中反序列化JSON(在服务器代码中)。 That will allow you to set null for the textboxes without having to loop over the form fields on the server-side. 这将使您可以为文本框设置null,而不必在服务器端遍历表单字段。

Maintainability is going to be driver for your decision whether to punt this to the server-side or do it on the client-side, like I've described. 就像我已经描述过的那样,可维护性将成为您决定将其扩展到服务器端还是在客户端上的决定性因素。

Also, don't forget to clean the data before creating SQL (as your server-side function name suggests). 同样,不要忘记在创建SQL之前清理数据(正如服务器端函数名称所建议的那样)。 You can't trust that data! 您不能相信这些数据!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM