我不明白在点击“搜索”后如何保存所选参数的实现逻辑
有aspx文件:
<form class="search_css" runat="server">
<div class="search_div_search_box">
<table class="filter_component_css">
<tr>
<td>miap</td>
<td><asp:TextBox CssClass="search_format" ID="miap_textbox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>purchase order</td>
<td><asp:TextBox CssClass="search_format" ID="po_textbox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>material desc</td>
<td><asp:TextBox CssClass="search_format" ID="material_desc_textbox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>supplier</td>
<td><asp:TextBox CssClass="search_format" ID="supplier_textbox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>manufacturer</td>
<td><asp:TextBox CssClass="search_format" ID="manufacturer_textbox" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>spare parts</td>
<td><asp:CheckBox CssClass="search_format" ID="parts_checkbox" runat="server" OnCheckedChanged="spareParts_Checked" ViewStateMode="Enabled"/></td>
</tr>
<tr>
<td>first fills</td>
<td><asp:CheckBox CssClass="search_format" ID="fills_checkbox" runat="server" OnCheckedChanged="firstFills_Checked" ViewStateMode="Enabled"/></td>
</tr>
<tr><td>special tools</td>
<td><asp:Checkbox CssClass="search_format" ID="tools_checkbox" runat="server" OnCheckedChanged="specialTools_Checked" ViewStateMode="Enabled"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="buttonReset" CssClass="filter_component_css_3" runat="server" Text="Reset" OnClick="submitResetClick"/>
<asp:Button ID="buttonSearch" CssClass="filter_component_css_3" runat="server" Text="Search" OnClick="submitSearchClick"/>
</td>
</tr>
</table>
</div>
</form>
c#class:
<script runat="server">
//spare parts variable
int sp_id=0;
//first fills variable
int ff_id=0;
//special tools variable
int st_id=0;
void spareParts_Checked(Object sender, EventArgs e)
{
if (parts_checkbox.Checked)
{
sp_id = 1;
}
}
void firstFills_Checked(Object sender, EventArgs e)
{
if (fills_checkbox.Checked)
{
ff_id = 1;
}
}
void specialTools_Checked(Object sender, EventArgs e)
{
if (tools_checkbox.Checked)
{
st_id = 1;
}
}
protected void submitSearchClick(object sender, EventArgs e)
{
ViewState.Add("flag", true);
ViewState.Add("sp_id", sp_id);
ViewState.Add("ff_id", ff_id);
ViewState.Add("st_id", st_id);
GridViewS.DataSource = SqlDataSource_Search;
Response.Write("miap like '%" + miap_textbox.Text + "%' and pocode like '%" + po_textbox.Text + "%' and materialdescription like '%" + material_desc_textbox.Text + "%' and suppliername like '%" + supplier_textbox.Text + "%' and manufacturername like '%" + manufacturer_textbox.Text
+ "%' and spareparts =" + sp_id + " and firstfills=" + ff_id + " and specialtools =" + st_id + "");
SqlDataSource_Search.FilterExpression = "miap like '%" + miap_textbox.Text + "%' and pocode like '%" + po_textbox.Text + "%' and materialdescription like '%" + material_desc_textbox.Text + "%' and suppliername like '%" + supplier_textbox.Text + "%' and manufacturername like '%" + manufacturer_textbox.Text
+ "%' and spareparts =" +sp_id+ " and firstfills=" +ff_id+ " and specialtools =" + st_id + "";
GridViewS.DataBind();
}
</script>
e.g。 如果我检查3个复选框 - 备件,第一次填充,特殊工具 - 点击“搜索”&gt; 响应写入结果: ...和spareparts = 1和firstfills = 1和specialtools = 1
如果之后我取消选中第一次填充 - 点击“搜索”&gt; ...和spareparts = 0和firstfills = 0和specialtools = 0
我需要的结果: ...和spareparts = 1和firstfills = 0和specialtools = 1
答案 0 :(得分:0)
请使用CheckBoxList并根据您的要求进行调整:
<强> ASPX 强>
<asp:CheckBoxList ID="chklBidType" runat="server">
<asp:ListItem Text="spare parts" Value="1" />
<asp:ListItem Text="first fills" Value="2" />
<asp:ListItem Text="special tools" Value="3" />
</asp:CheckBoxList>
<强> CS 强>
protected void Button1_Click(object sender, EventArgs e)
{
string strBidType = "";
foreach(ListItem cBox in chklBidType.Items)
{
if(cBox.Selected)
{
strBidType += cBox.Value + " / ";
}
}
Response.Write(strBidType);
}