Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将复选框列表项发送到方法?_C#_Asp.net - Fatal编程技术网

C# 如何将复选框列表项发送到方法?

C# 如何将复选框列表项发送到方法?,c#,asp.net,C#,Asp.net,我想从CheckBoxList发送两种颜色,如蓝色和红色到一个方法,该方法接受4个可选参数并打印最终的组合结果 这是复选框列表: Choose Two Colors: <asp:CheckBoxList ID="CheckBoxList1" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"> <asp:ListItem>Red</asp:Li

我想从CheckBoxList发送两种颜色,如蓝色和红色到一个方法,该方法接受4个可选参数并打印最终的组合结果

这是复选框列表:

 Choose Two Colors:
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
        <asp:ListItem>Red</asp:ListItem>
        <asp:ListItem>Blue</asp:ListItem>
        <asp:ListItem>Yellow</asp:ListItem>
        <asp:ListItem>Green</asp:ListItem>
    </asp:CheckBoxList>
现在,当有人从复选框列表中选择两种颜色时,我想用如下代码获得两种颜色:

 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (ListItem item in CheckBoxList1.Items)
    {
        if (item.Selected)
        {

            string selectedoptions;
            selectedoptions = item.Text;
        }

    }

}
然后将选定的字符串项作为命名参数发送给该方法

我们如何从字符串“selectedoptions”中获取选定的颜色,然后将其格式化

  ColorMix(Blue:"Blue",Red:"Red")

“蓝色”和“红色”是由用户选择的颜色。

这不是ASP.Net,但我确信同样适用。我不认为你在用正确的事件来做你想做的事情。每当用户单击
复选框列表
并更改所选项目时,会触发事件
复选框列表1\u SelectedIndexChanged
。即使未更改复选框,事件也会激发。当然,您可以在这里做需要做的事情,比如检查哪些复选框被选中,并将一些变量传递给一个方法,但是每次用户更改选择时,您都会这样做

我猜一个按钮将是一个更好的控件来激发你的颜色方法。这样,在按下此按钮之前,您不会在项目之间循环。然后,您可以循环浏览列表并获取选中的颜色。如果只检查了一(1)种颜色或三(3)种或更多颜色,则不清楚您想做什么,因为帖子表明您只想通过两种颜色

下面(c#code)是我与
CheckedListBox
一起使用的代码,其中项目被命名为红色、蓝色、绿色和黄色。我使用了一个
按钮\u单击
事件,而不是
复选框列表1\u选择的索引更改
事件。当按下按钮时,它只是在列表中循环并显示项目索引、名称及其选中状态

private void button1_Click(object sender, EventArgs e) {
  // Make a list of the checked colors you want to pass
  //List<string> checkedItems = new List<string>();
  int index;
  foreach (string item in checkedListBox1.Items) {
    index = checkedListBox1.Items.IndexOf(item);
    string checkState = checkedListBox1.GetItemCheckState(index).ToString(); 
    MessageBox.Show("Item on line " + index + " name: " + item +
                    " is currently: " + checkState);
  }
}
private void按钮1\u单击(对象发送者,事件参数e){
//列出要传递的选中颜色
//列表checkedItems=新列表();
整数指数;
foreach(checkedListBox1.Items中的字符串项){
index=checkedListBox1.Items.IndexOf(item);
string checkState=checkedListBox1.GetItemCheckState(index.ToString();
MessageBox.Show(“行上的项目”+索引+”名称:“+项目+
“当前为:“+checkState”);
}
}

希望这有帮助

请确认您的问题或错误!简单地说,我希望当有人从复选框列表中选择两种颜色时,我能够将项目作为命名参数发送给一个方法。非常感谢,这是一个非常好的方法
private void button1_Click(object sender, EventArgs e) {
  // Make a list of the checked colors you want to pass
  //List<string> checkedItems = new List<string>();
  int index;
  foreach (string item in checkedListBox1.Items) {
    index = checkedListBox1.Items.IndexOf(item);
    string checkState = checkedListBox1.GetItemCheckState(index).ToString(); 
    MessageBox.Show("Item on line " + index + " name: " + item +
                    " is currently: " + checkState);
  }
}