c# - Keeping Data in GridView after PostBack -


i have gridview associated sqldatasource.

when click button, change selectcommand , use databind update gridview.

after postback, want latest data remain, don't want gridview loaded original selectcommand.

i know done called viewstate, didn't manage implement in right way.

i tried both enableviewstate="true" , enableviewstate="false" on grid itself, no luck.

code

<asp:gridview id="gridview1" ...... datasourceid="userssource" >

<asp:sqldatasource id="userssource" ... selectcommand="select * users"></asp:sqldatasource> 

on startup, gridview filled results of select * users.

now click button, following:

userssource.selectcommand = "select * users user_id = 1"; gridview1.databind(); 

the gridview filled new data.

now let's say, click on header sort table, there postback, , after it, data in table contain results of first query.

what needs done here?

more info

declaration:

<%@ page language="c#" autoeventwireup="true" codefile="admin.aspx.cs" inherits="admin" %>

there's nothing in page load method.

this design. problem here sqldatasource gets re-created when page loads not maintains selectcommand on postback rather reverts 1 set when datasource created.

so need tell the sqldatasource had selectcommand when last loaded correctly.

just store sql command in viewstate , enable encryption protect contents setting viewstateencryptionmode on page directive. [ choose security standard ]

<%@ page viewstateencyptionmode="always" %> 

in button click event store new command in view state:

viewstate["currentcommand"] = "select * users user_id = 1"; userssource.selectcommand = viewstate["currentcommand"]; gridview1.databind(); 

now in page load event, set it:

if( viewstate["currentcommand"] !=null)     userssource.selectcommand = viewstate["currentcommand"]; 

refer post microsoft: http://connect.microsoft.com/visualstudio/feedback/details/105069/sqldatasource-selectcommand-not-persisting-on-postback


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -