DataList/PagedDataSource/DataTable-如何允许过滤器控制参数? [解决了] [英] DataList / PagedDataSource / DataTable - How to allow for filter Control Parameter? [SOLVED]

查看:69
本文介绍了DataList/PagedDataSource/DataTable-如何允许过滤器控制参数? [解决了]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用Visual Web Developer 2008 Express.以下是C#中的页面.

在使用PagedDataSource添加分页之前,我已经为我的DataList使用了dropdownlist和搜索框过滤器.这些参数在aspx页中的代码内容如下:

Hi,

I use Visual Web Developer 2008 Express. The below is for page in C#.

Prior to adding paging using PagedDataSource I had dropdownlist and search box filters for my DataList. The guts of the code in aspx page for those parameters is below:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ProductCode], [ImgUrl], [ImageField], [Featuring], [ProdCategoryID], [Title] FROM [TableSG1] WHERE (([ProdCategoryID] = @ProdCategoryID) AND ([Featuring] LIKE ''%'' + @Featuring + ''%'')) ORDER BY [Featuring]">

        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="ProdCategoryID"
                PropertyName="SelectedValue" Type="String" />

            <asp:ControlParameter ControlID="TextBox1" Name="Featuring" PropertyName="Text"
                Type="String" />




当使用PagedDataSource创建寻呼机时,我做了一个没有参数的教程,所有代码都放在了页面后面的代码中.它的胆量如下:




When creating the pager using PagedDataSource I did a tutorial which did not have parameters and all the code went in the code behind page. The guts of it is below:

private void BindGrid()
     {
         string sql = "SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] DESC";
         SqlDataAdapter da = new SqlDataAdapter(sql, "Data Source=tcp:s02.winhost.com;Initial Catalog=DB_5890_maindb1;User ID=DB_5890_maindb1_user;Password=xxxxx;Integrated Security=False;");
         DataTable dt = new DataTable();
         da.Fill(dt);

         pds.DataSource = dt.DefaultView;
         pds.AllowPaging = true;
         pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
         pds.CurrentPageIndex = CurrentPage;
         lnkbtnNext.Enabled = !pds.IsLastPage;
         lnkbtnPrevious.Enabled = !pds.IsFirstPage;

         DataList1.DataSource = pds;
         DataList1.DataBind();

         doPaging();
}



寻呼机很棒,但是现在我需要添加过滤器.如果将"SelectParameters"代码(如上)添加到我的aspx页面中,并且如果我只是将"WHERE"子句添加到后面代码中的"SELECT"子句中,则该页面将不会加载,我收到一条错误消息,指出我需要添加一个标量变量.

我通过谷歌搜索找到了一些示例,但还不够具体.

如果有人愿意帮助我,我就需要帮助.

======================
解决方案-尤里卡!



The pager is great but now I need to add my filters. The page wont load if I add the "SelectParameters" code (above) to my aspx page and if I simply add the "WHERE" clause to the "SELECT" clause in the code behind I get an error stating that I need to add a scalar variable.

I have found some examples through googling but nothing specific enough.

I need assistance if anyone would be kind enough to help.

=======================
SOLUTION - Eureka!

SqlConnection conn = new SqlConnection("Data Source=tcp:s02.winhost.com;Initial Catalog=DB_5890_maindb1;User ID=DB_5890_maindb1_user;Password=XXXXXX;Integrated Security=False;");
SqlCommand cmd = new SqlCommand("SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] WHERE [ProdCategoryID] = @ProdCategoryID ORDER BY [Featuring]", conn);

SqlParameter param = new SqlParameter();
param.ParameterName = "@ProdCategoryID";

param.Value = DropDownList1.SelectedItem.Value;
cmd.Parameters.Add(param);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

推荐答案

ConnectionStrings:ConnectionString%> SelectCommand ="SELECT [ProductCode],[ImgUrl],[ImageField],[Featureing],[ProdCategoryID],[Title]来自[TableSG1] WHERE(([[ProdCategoryID] = @ProdCategoryID)和([Featureing] LIKE''% " + @Featuring +''%''))ORDER BY [Featuring]> < SelectParameters> < asp:ControlParameter ControlID ="DropDownList1" Name ="ProdCategoryID" PropertyName ="SelectedValue" Type ="String"/> < asp:ControlParameter ControlID ="TextBox1" Name =功能"PropertyName =" Text 输入="String"/>
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [ProductCode], [ImgUrl], [ImageField], [Featuring], [ProdCategoryID], [Title] FROM [TableSG1] WHERE (([ProdCategoryID] = @ProdCategoryID) AND ([Featuring] LIKE ''%'' + @Featuring + ''%'')) ORDER BY [Featuring]"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="ProdCategoryID" PropertyName="SelectedValue" Type="String" /> <asp:ControlParameter ControlID="TextBox1" Name="Featuring" PropertyName="Text" Type="String" />




当使用PagedDataSource创建寻呼机时,我做了一个没有参数的教程,所有代码都放在了页面后面的代码中.它的胆量如下:




When creating the pager using PagedDataSource I did a tutorial which did not have parameters and all the code went in the code behind page. The guts of it is below:

private void BindGrid()
     {
         string sql = "SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] DESC";
         SqlDataAdapter da = new SqlDataAdapter(sql, "Data Source=tcp:s02.winhost.com;Initial Catalog=DB_5890_maindb1;User ID=DB_5890_maindb1_user;Password=xxxxx;Integrated Security=False;");
         DataTable dt = new DataTable();
         da.Fill(dt);

         pds.DataSource = dt.DefaultView;
         pds.AllowPaging = true;
         pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
         pds.CurrentPageIndex = CurrentPage;
         lnkbtnNext.Enabled = !pds.IsLastPage;
         lnkbtnPrevious.Enabled = !pds.IsFirstPage;

         DataList1.DataSource = pds;
         DataList1.DataBind();

         doPaging();
}



寻呼机很棒,但是现在我需要添加过滤器.如果将"SelectParameters"代码(如上)添加到我的aspx页面中,并且如果我只是将"WHERE"子句添加到后面代码中的"SELECT"子句中,则该页面将不会加载,我收到一条错误消息,指出我需要添加一个标量变量.

我通过谷歌搜索找到了一些示例,但还不够具体.

如果有人愿意帮助我,我就需要帮助.

======================
解决方案-尤里卡!



The pager is great but now I need to add my filters. The page wont load if I add the "SelectParameters" code (above) to my aspx page and if I simply add the "WHERE" clause to the "SELECT" clause in the code behind I get an error stating that I need to add a scalar variable.

I have found some examples through googling but nothing specific enough.

I need assistance if anyone would be kind enough to help.

=======================
SOLUTION - Eureka!

SqlConnection conn = new SqlConnection("Data Source=tcp:s02.winhost.com;Initial Catalog=DB_5890_maindb1;User ID=DB_5890_maindb1_user;Password=XXXXXX;Integrated Security=False;");
SqlCommand cmd = new SqlCommand("SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] WHERE [ProdCategoryID] = @ProdCategoryID ORDER BY [Featuring]", conn);

SqlParameter param = new SqlParameter();
param.ParameterName = "@ProdCategoryID";

param.Value = DropDownList1.SelectedItem.Value;
cmd.Parameters.Add(param);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);


<code><code><code><code>hfhf :omg: :mad: :rolleyes: :) :laugh: ;P :sigh: :(( :-O :omg: :confused: X| :cool::thumbsup::thumbsup::rose::suss::~:mad: :rolleyes: :-\ :doh: :( ;) :-D


这篇关于DataList/PagedDataSource/DataTable-如何允许过滤器控制参数? [解决了]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆