我想從CheckBoxList發送2種顏色,如藍色和紅色,這種方法需要4個可選參數並打印最終的組合結果。如何將CheckBoxList項目發送到方法?
這是CheckBoxList的:
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>
這是一個需要4個可選參數的方法:
static string ColorMixer(string Blue = "NotDefined", string Yellow = "NotDefined", string Red = "NotDefined",
string Green = "NotDefined")
{
string blue = Blue;
string yellow = Yellow;
string red = Red;
string green = Green;
string results;
if (blue.Equals("Blue")&&yellow.Equals("Yellow"))
{
results = "GREEN";
return results;
}
if (red.Equals("Red")& green.Equals("Green"))
{
results = "Brown";
return results;
}
the rest of codes goes here ....
else
{
results = "Result is Unspecified";
}
return results;
}
現在,當有人從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")
其中「藍」和「紅」是用戶選擇的顏色。
請確認您的問題或您得到的錯誤! – Null
只需要當有人從checkboxlist中選擇兩種顏色時,我就可以將項目作爲命名參數發送給方法。 – Mohsen