Bulat Makhmutov Bulat Makhmutov - 3 years ago 83
ASP.NET (C#) Question

after click Search - chosen values are reset, not stored

I do not understand how implement logic for saving chosen parameters after click 'Search'

there is aspx file:

<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.
if I check 3 check-boxes - spare parts, first fills, special tools - click 'Search' >
response write result:
... and spareparts =1 and firstfills=1 and specialtools =1

if after that I uncheck first fills only - click 'Search' >
... and spareparts =0 and firstfills=0 and specialtools =0

Result which I need:
... and spareparts =1 and firstfills=0 and specialtools =1

Answer Source

Use CheckBoxList instead and tweak this for your requiement :

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);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download