开发者

access selectcommand using codebehind

How can I change my selecommand, and keep it through the remainder of the page (when using pagination, sorting)?

I have a page of checkboxes:

<input type="checkbox" name="checkbox_1" />
<input type="checkbox" name="checkbox_2" />
<input type="checkbo开发者_如何学运维x" name="checkbox_3" />
<asp:Button runat="server" Id="CustomButton" text="Create Report" PostBackUrl="report.aspx?"/>

Then on report.aspx I want to generate a standard listview based on the selections in the checkbox.

 <asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
  <LayoutTemplate runat="server">
       ...<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />...
  </LayoutTemplate>
 <ItemTemplate>
     ...
 </ItemTemplate>
</asp:ListView>

I want to be able to sort and paginate that listview. This is an idea of what i want in the code behind:

Protected Sub ReportListView_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)

     ' What's the correct way to reference the listview?  
     ' When I use the below code i get "ReportListView is not declared...."

     ' ReportListView.SqlCommand = "SELECT " & checkbox1 & ", " & checkbox2 & " WHERE..."

End Sub

I'm not sure if I'm even going in the right direction with this, any help is appreciated. Will the changes i make to the sql command in the PreRender function hold when I have applied pagination or sorting to the listview?


If I understand your question correctly, you want to open a new page and use the prior page's values in the select statement for the ListView's SqlDataSource on the new page, correct?

First, a few observations:

  1. In your first page, you appear to be intending to call the second page with a query string (PostBackUrl="report.aspx?), but you don't appear to set the query string.
  2. Your PreRender event for the ListView control has the wrong signature. It only takes one argument, EventArgs:

    Protected Sub ReportListView_PreRender(ByVal e As EventArgs)

  3. Your ListView appears to be using a SqlDataSource as it's binding source (DataSource="ReportListViewSDS"). More about that below.
  4. There is no SqlCommand property or method for the ListView control.

Since you're binding the ListView to a SqlDataSource, it'd be simplest to set the Select command and the parameters in the markup, like this:

<asp:SqlDataSource ID="ReportListViewSDS" runat="server"
     SelectCommand="SELECT checkbox1, checkbox2, checkbox3 FROM <table> WHERE checkbox1 = @parm1 AND checkbox2 = @parm2 AND checkbox3 = @parm3">
    <SelectParameters>
        <asp:FormParameter FormField="checkbox_1" Name="parm1" />
        <asp:FormParameter FormField="checkbox_2" Name="parm2" />
        <asp:FormParameter FormField="checkbox_3" Name="parm3" />
    </SelectParameters>
</asp:SqlDataSource>

Replace <table> in the SelectCommand with the name of your table. You can adjust the names of the columns you're selecting, as well as the parameters you're using, as desired. I simply used 3 checkboxes as that's what you had in the code you posted.

Also note, NO VALIDATION of the parameters will be done by the SqlDataSource, so if you want to prevent SQL Injection attacks and other security risks, you'll want to do validation in the Selecting event of the SqlDataSource.

More information can be found here:

SqlDataSource Class

FormParameter Class


Actually this was much easier than I thought. Sorry, just a newbie mistake i guess. i ended up simply doing:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim SqlCommand As String
    ' create the sql command using the Request.Form vars i wanted.
    ' ...
    ' Run the sql command, I can access the listview directly, just like a global variable:
    ReportListView.SelectCommand = SqlCommand
    ReportListView.DataBind()
 End Sub

And that seemed to do it. Actually very easy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜