2011-08-12 55 views
0

如何更改我的selecommand,并在页面的其余部分(使用分页,排序时)保留它?使用代码隐藏访问selectcommand

我有一个复选框页:

<input type="checkbox" name="checkbox_1" /> 
<input type="checkbox" name="checkbox_2" /> 
<input type="checkbox" name="checkbox_3" /> 
<asp:Button runat="server" Id="CustomButton" text="Create Report" PostBackUrl="report.aspx?"/> 

然后在report.aspx我要生成基于复选框的选择标准列表视图。

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

我希望能够对该列表视图进行排序和分页。这是什么,我想在后面的代码的想法:

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 

我不知道如果我即使在这个正确的方向前进,任何帮助表示赞赏。当我将分页或排序应用到列表视图时,我对PreRender函数中的sql命令所做的更改会保留吗?

回答

0

其实这比我想象的要容易得多。对不起,我猜只是一个新手错误。我最后只是在做:

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 

而且似乎这样做。其实很简单。

0

如果我正确理解您的问题,您希望打开一个新页面,并在新页面上的ListViewSqlDataSource的select语句中使用上一页的值,对吗?

首先,一些意见:

  1. 在第一页,你似乎是打算调用与查询字符串(PostBackUrl="report.aspx?)的第二页,但你似乎没有设置的查询字符串。
  2. 您的PreRender事件为ListView控件具有错误的签名。只需要一个参数,EventArgs

    保护小组ReportListView_PreRender(BYVALË作为EventArgs的)

  3. ListView似乎使用SqlDataSource,因为它是绑定源(DataSource="ReportListViewSDS")。更多关于下面的内容。
  4. 没有SqlCommand属性或ListView控件的方法。

既然你绑定ListViewSqlDataSource,它会是最简单的设置中选择命令,并在标记的参数,如:

<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> 

SelectCommand替换为<table>你的桌子的名字。根据需要,您可以调整所选列的名称以及所使用的参数。我简单地使用了3个复选框,因为这是您在发布的代码中所拥有的。

还要注意,没有验证的参数将由SqlDataSource来完成,因此,如果你想防止SQL注入攻击等安全隐患,你会想要做验证的Selecting事件SqlDataSource的。

更多信息可以在这里找到:

SqlDataSource Class

FormParameter Class

+0

苏里,我的问题不是很清楚。我熟悉标准列表视图,并使用“选择”命令和您提到的参数。但我的目标是选择查询将具有来自Request.From的条目(即前面的页面复选框)。我发现我让它变得更加困难。查看下面的解决方案。 – russds