如何更改我的选择命令,并在页面的其余部分保留它(当使用分页、排序时)?
我有一页的复选框:
<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命令所做的更改是否有效?
发布于 2011-09-07 04:51:16
实际上,这比我想象的要容易得多。抱歉,我想这只是个新手的错误。我最终只是简单地做了:
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这似乎起到了作用。实际上非常简单。
发布于 2011-08-14 02:53:54
如果我正确理解了您的问题,那么您希望打开一个新页面,并在新页面上的ListView的SqlDataSource的select语句中使用前一页的值,对吗?
首先,我有几点看法:
PostBackUrl="report.aspx?)调用第二页,但您似乎没有设置查询字符串。PreRender事件的ListView控件具有错误的签名。它只需要一个参数,EventArgs:Protected Sub ReportListView_PreRender(ByVal e As EventArgs)
ListView似乎正在使用SqlDataSource作为其绑定源(DataSource="ReportListViewSDS")。有关这方面的更多信息,请参见下面的内容。
ListView控件没有SqlCommand属性或方法。由于您要将ListView绑定到SqlDataSource,因此设置选择命令和标记中的参数将是最简单的,如下所示:
<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注入攻击和其他安全风险,则需要在SqlDataSource的Selecting事件中进行验证。
更多信息可以在这里找到:
SqlDataSource Class
FormParameter Class
https://stackoverflow.com/questions/7043969
复制相似问题