首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用代码后台访问selectcommand

使用代码后台访问selectcommand
EN

Stack Overflow用户
提问于 2011-08-13 01:25:22
回答 2查看 1.9K关注 0票数 0

如何更改我的选择命令,并在页面的其余部分保留它(当使用分页、排序时)?

我有一页的复选框:

代码语言:javascript
复制
<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上,我想根据复选框中的选择生成一个标准的列表视图。

代码语言:javascript
复制
 <asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
  <LayoutTemplate runat="server">
       ...<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />...
  </LayoutTemplate>
 <ItemTemplate>
     ...
 </ItemTemplate>
</asp:ListView>

我希望能够对列表视图进行排序和分页。这是我想要的代码背后的想法:

代码语言:javascript
复制
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命令所做的更改是否有效?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-07 04:51:16

实际上,这比我想象的要容易得多。抱歉,我想这只是个新手的错误。我最终只是简单地做了:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2011-08-14 02:53:54

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

首先,我有几点看法:

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

Protected Sub ReportListView_PreRender(ByVal e As EventArgs)

  • Your ListView似乎正在使用SqlDataSource作为其绑定源(DataSource="ReportListViewSDS")。有关这方面的更多信息,请参见下面的内容。

  • ListView控件没有SqlCommand属性或方法。

由于您要将ListView绑定到SqlDataSource,因此设置选择命令和标记中的参数将是最简单的,如下所示:

代码语言:javascript
复制
<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注入攻击和其他安全风险,则需要在SqlDataSourceSelecting事件中进行验证。

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

SqlDataSource Class

FormParameter Class

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7043969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档