c# - ASP.NET GridView won't populate from changing SqlDataSource.SelectCommand if Textbox is empty -


i'm using textbox , dropdownlist filter database searches. i'm using sqldatasource blank selectcommand , setting command in codebehind depending on user has typed in , selected in dropdownlist. if statements in codebehind work if txtfindbook != "".

before explain anymore here code:

default.aspx

<asp:content id="bodycontent" contentplaceholderid="maincontent" runat="server">  <div class="jumbotron">     <h1>find book...</h1>     <p>         <asp:textbox id="txtfindbook" runat="server" width="700px"></asp:textbox>          <asp:dropdownlist id="ddlgenres" runat="server"></asp:dropdownlist>          <asp:button id="btnfindbook" runat="server" text="search" onclick="btnfindbook_click" height="36px"  />      <p>enter search terms in box above, click "search" begin search.</p>     <asp:label id="label1" runat="server" text=""></asp:label> </div>  <div class="searchresults">     <asp:gridview id="gvsearchresults" runat="server" autogeneratecolumns="false" datakeynames="id" datasourceid="sqldatasourcesearchresults">         <columns>             <asp:boundfield datafield="id" headertext="id" readonly="true" insertvisible="false" sortexpression="id"></asp:boundfield>             <asp:boundfield datafield="bookid" headertext="bookid" sortexpression="bookid"></asp:boundfield>             <asp:boundfield datafield="title" headertext="title" sortexpression="title"></asp:boundfield>             <asp:boundfield datafield="author" headertext="author" sortexpression="author"></asp:boundfield>             <asp:boundfield datafield="isbn_10" headertext="isbn_10" sortexpression="isbn_10"></asp:boundfield>             <asp:boundfield datafield="isbn_13" headertext="isbn_13" sortexpression="isbn_13"></asp:boundfield>             <asp:boundfield datafield="dewey" headertext="dewey" sortexpression="dewey"></asp:boundfield>             <asp:boundfield datafield="genre" headertext="genre" sortexpression="genre"></asp:boundfield>             <asp:checkboxfield datafield="ischeckedout" headertext="ischeckedout" sortexpression="ischeckedout"></asp:checkboxfield>             <asp:boundfield datafield="checked_out_to_whome" headertext="checked_out_to_whome" sortexpression="checked_out_to_whome"></asp:boundfield>             <asp:boundfield datafield="due_date" headertext="due_date" sortexpression="due_date"></asp:boundfield>         </columns>     </asp:gridview>     <asp:sqldatasource runat="server" id="sqldatasourcesearchresults" connectionstring='<%$ connectionstrings:defaultconnection %>' selectcommand="">         <selectparameters>             <asp:controlparameter controlid="txtfindbook" propertyname="text" name="title" type="string"></asp:controlparameter>             <asp:controlparameter controlid="ddlgenres" propertyname="selectedvalue" defaultvalue="select genre" name="genre" type="string"></asp:controlparameter>         </selectparameters>     </asp:sqldatasource> </div> 

default.aspx.cs

public partial class _default : page {     static string connstring = system.configuration.configurationmanager.connectionstrings["defaultconnection"].connectionstring;     sqlconnection conn = new sqlconnection(connstring);      protected void page_load(object sender, eventargs e)     {         if (!ispostback)         {             /*there no point in populating             dropdownlist everytime there             post back. */              populateddlgenres();         }      }     protected void btnfindbook_click(object sender, eventargs e)     {         if (txtfindbook.text != "" && ddlgenres.selecteditem.text != "select genre")         {             sqldatasourcesearchresults.selectcommand = "select * [books] (([title] '%' + @title + '%') , ([genre] = @genre)) order [title]";             label1.text = "if statement 1.";         }          else if (txtfindbook.text == "" && ddlgenres.selecteditem.text != "select genre")         {             label1.text = "if statement 2.";             sqldatasourcesearchresults.selectcommand = "select * [books] ([genre] = @genre)";         }          else if (txtfindbook.text == "" && ddlgenres.selecteditem.text == "select genre")         {             sqldatasourcesearchresults.selectcommand = "select * [books]";             label1.text = "if statement 3.";         }          else if(txtfindbook.text != "" && ddlgenres.selecteditem.text == "select genre")         {             sqldatasourcesearchresults.selectcommand = "select * [books] ([title] '%' + @title + '%') order [title]";             label1.text = "original.";         }     }      private void populateddlgenres()     {         try         {              using (sqlconnection con = new sqlconnection(connstring))             {                 sqlcommand cmd = new sqlcommand("select * genres", con);                 con.open();                 ddlgenres.datasource = cmd.executereader();                 ddlgenres.datatextfield = "genretext";                 //ddlgenres.datavaluefield = ""; //we aren't using because text it's own value.                 ddlgenres.databind(); //must go last!              }          }         catch (exception ex)         {             // handle error         }          ddlgenres.items.insert(0, new listitem("select genre", "0"));      } } 

now if add txtfindbook.text = "a"; @ beginning of if statements 2 & 3 populate gvsearchresults on postback.

is there someway can still have if statements 2 & 3 work while keeping txtfindbook.text blank?

modify sqldatasource adding default value of null title property defined on , should trick. not need labels, didn't understand label thing anyway.

<asp:sqldatasource runat="server" id="sqldatasourcesearchresults" connectionstring='<%$ connectionstrings:defaultconnection %>' selectcommand="">     <selectparameters>         <asp:controlparameter controlid="txtfindbook" propertyname="text" defaultvalue="null" name="title" type="string"></asp:controlparameter>         <asp:controlparameter controlid="ddlgenres" propertyname="selectedvalue" defaultvalue="select genre" name="genre" type="string"></asp:controlparameter>     </selectparameters> </asp:sqldatasource> 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -