c# - How should I be using GridViews? -


recent problems i've had making me question whole philosophy , assumptions regarding gridviews.

currently, have used along lines of following model.

in markup:

<asp:updatepanel id="pnlsearch" updatemode="conditional" runat="server">     <contenttemplate>             <asp:textbox id="txtsearch" runat="server"                  ontextchanged="txtsearch_textchanged"></asp:textbox>     </contenttemplate> </asp:updatepanel>  <asp:updatepanel id="pnlgridview" updatemode="conditional" runat="server">     <contenttemplate>         <asp:gridview id="gridview1" enableviewstate="false" allowpaging="true" pagesize="20" datasourceid="mydatasource" runat="server" autogeneratecolumns="false">             <columns>                 <asp:boundfield datafield="col_1" headertext="happy data" sortexpression="col_1" />                 <asp:boundfield datafield="col_2" headertext="happy related data" sortexpression="col_2" dataformatstring="{0:m/dd/yyyy}" />             </columns>         </asp:gridview>     </contenttemplate>     <triggers>         <asp:asyncpostbacktrigger controlid="txtsearch" eventname="textchanged" />     </triggers> </asp:updatepanel> <asp:sqldatasource id="mydatasource" runat="server"></asp:sqldatasource> 

pretty basic stuff. gridview. data source. text box searching results. include updatepanels because i'm convinced part of problems.

over in code behind, this:

protected void page_load(object sender, eventargs e) {      if (!ispostback)     {         mydatasource.connectionstring = configurationmanager.connectionstrings["oracleconnectionstring"].connectionstring;         mydatasource.providername = configurationmanager.connectionstrings["oracleconnectionstring"].providername;         gridview1.emptydatatext = "no comments found.";         populategridview();     } }  protected void populategridview() {     string strquery = @"select  col_1,                           col_2                      some_table                     col_3 = :important_filter";      mydatasource.selectcommand = strquery;     mydatasource.selectparameters.clear();     mydatasource.selectparameters.add(":important_filter", request.querystring["filter"]);     gridview1.databind();     gridview1.pageindex = 0; }  protected void txtsearch_textchanged(object sender, eventargs e) {     string strquery = @"select  col_1,                           col_2                      some_table                     col_3 = :important_filter , lower(col_2) :search";     mydatasource.selectcommand = strquery;     mydatasource.selectparameters.clear();     mydatasource.selectparameters.add(":important_filter", request.querystring["filter"]);     mydatasource.selectparameters.add(":search", "%" + txtsearch.text.trim().tolower() + "%");     gridview1.databind();     gridview1.pageindex = 0; } 

nothing fancy. connect data source connection string config file (as necessary in multi instance environment), wire query , databind. on searches, change query , rebind.

the problem?

the above doesn't work. why? data source loses it's query, connection string, provider name, etc on post back. gridview commits suicide. same thing happens when try change pages...either on initial load or search populated.

i "solved" problem last week manually adding datasource control state , repopulating values control state, seems hackish.

what not understanding?

you have update mode set conditional need call update on 'pnlgridview'

gridview1.databind(); gridview1.pageindex = 0; pnlgridview.update(); 

i revist how using datasource

 <asp:sqldatasource id="productsdatasource" runat="server"     connectionstring="<%$ connectionstrings:oracleconnectionstring %>"     selectcommand="select  col_1, col_2  some_table  col_3 = @important_filter">    <selectparameters>      <asp:controlparameter controlid="textbox?not sure coming from" propertyname="selectedvalue" name="important_filter" type="string" defaultvalue="" />    </selectparameters>  </asp:sqldatasource> 

you use server side event handler , set important filter manually.

protected void sqldatasource_selecting(object sender, sqldatasourceselectingeventargs e) {     e.inputparameters["important_filter"] = "whatever";  } 

and add event markup sqldatasource.

<asp:sqldatasource id="mydatasource" runat="server"     onselecting="sqldatasource_selecting" /> 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -