Today I was building a GridView and wanted to set the SelectParameters of the SqlDataSource to use a value that was being sent in the query string. But then I realized that the value I needed was not being sent in the query string and I didn't want to change the calling page to send the parameter that I needed. What I wanted to do was to run a query in Page_Load to get the value I needed, store it in a variable, and then assign that variable to some SelectParameter. But I'd never set the value of a SelectParameter to a value stored in a variable.
When I did a search, I found
this solution that uses a generic
. That looked promising, but I couldn't get it to work. I figured I could copy my variable into a Session variable, and then use a , but that seemed like overkill to create a Session variable just for that page.
The solution ended up being pretty simple. What I did was:
- create a TextBox control on the page
- set the TextBox Text property to the value of my variable obtained from the query in Page_Load
- use a ControlParameter for the SelectParameter of the data source and specify that TextBox control as the control from which to get the value
In my case, it turned out that I could keep the control visible on the page because it was useful information, but if that isn't option for you then you could just set the control's visibility to false.
No comments:
Post a Comment