Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm using Visual Web Developer 2010 Express. This is my first attempt at creating a .NET page. I have a Gridview on a page that displays the results of a GridQuery. This works. I have a checkbox control (cb_Filter) on the same page that if checked, should add to the where clause of the GridQuery, (where Column5 IS NULL). How do I check the state of the check box so it will run the query with or without the "filter".

share|improve this question

2 Answers 2

up vote 0 down vote accepted

really depends on your data access, if you have made a datasource on page, then you'll have two and the code will change which data source you have. if it is using the old ado.net then you will have two commands that run in seperate methods that get the sql query, and a similar thing will work for linq. Let me know how you bind to the grid and i will give you an example.

share|improve this answer
    
Hopefully I'm understanding your question, I connect to the database using a "ConnectionString". I don't see any reference to ado.net so I assume it is the former. –  Patrick Beesley Feb 2 '11 at 21:18
    
@patrick in any case you need a connection string. Do you drop a datasource control on to the form? what does your code look like that gets your data into the grid? –  Daniel Casserly Feb 2 '11 at 21:23
    
<asp:SqlDataSource ID="GridQuery" runat="server" ConnectionString="<%$ ConnectionStrings:NGProdConnectionString %>" SelectCommand= "SELECT Column1, column2 FROM TABLE"> <FilterParameters> <asp:ControlParameter ControlID="cb_Filter" Name="p_Filter" PropertyName="Checked" /> </FilterParameters> </asp:SqlDataSource> –  Patrick Beesley Feb 2 '11 at 21:38
    
ok so you have a datasource control. You need to create another of these, one with the where clause and one without and then in the code behind on the checkbox changed event(autopostback to true) give it a bit of if (check.Cheked) grid.datasource = datasource1; else grid.datasource = datasource 2 –  Daniel Casserly Feb 2 '11 at 21:41
    
My GridView is called GridView1; its datasourceID = GridQuery1. In which section does the conditional (if cb_filter.Checked THEN GridQuery1 ELSE GridQuery2) go? –  Patrick Beesley Feb 2 '11 at 22:10

If you want the grid results to change dynamically, autopostback should be True for the checkbox. This will cause the page to reload when the value changes. Then, in your page load routine you do something like this:

If cb_filter.Checked Then
   'set the checked datasource or SQL string here
Else
   'set the unchecked datasource or SQL string here
End If
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.