`

ASP.NET DEMO 13: 如何为 SqlDataSource 动态绑定变量参数

阅读更多
对于 xxxDataSource 来说,支持绑定参数,包括 ControlParameter、CookieParameter、SessionParameter、ProfileParameter 和 QueryStringParameter。假如参数值直接来自于应用程序变量或者通过某个方法返回呢?
查阅了关于参数基类 Parameter 类似乎不支持此功能,有一个选择就是扩展自己的 Parameter,但是工作量比大,本身使用 xxxDataSource 就是为了快速开发。

这里采用比较“原始”方法:直接使用Web服务器控件都支持的绑定语法 <%# expression%>

先看下面这个 SqlDataSource ,其中的 SelectCommand 属性,是通过动态绑定实现的,categoryId 是一个私有类字段。

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient"SelectCommand='<%#"SELECT*FROM[Products]WHERE[CategoryID]="+categoryId%>'><%--动态绑定SelectCommand命令--%>
</asp:SqlDataSource>

可以通过控件事件中改变类字段 categoryId 的值,然后调用 SqlDataSource1.DataBind() 计算此值,得出 SelectCommand

甚至可以绑定一个方法,处理一个比较复杂sql语句,并返回
<asp:SqlDataSourceID="SqlDataSource3"runat="server"ConnectionString="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient"SelectCommand='<%#GetSelectCommandText()%>'><%--动态绑定SelectCommand命令--%>
</asp:SqlDataSource>

privatestringGetSelectCommandText()
{
stringsql="SELECT*FROM[Products]";
if(DropDownList1.SelectedValue!=""){
sql
+="WHERE[CategoryID]="+int.Parse(DropDownList1.SelectedValue);
}

returnsql;
}

测试实例通过一个 DropDownList 改变 categoryId 的值
protectedvoidDropDownList1_SelectedIndexChanged(objectsender,EventArgse)
{
categoryId
=int.Parse(DropDownList1.SelectedValue);
SqlDataSource1.DataBind();
//先执行绑定数据源控件,计算SelectCommand
GridView1.DataBind();

SqlDataSource3.DataBind();
//先执行绑定数据源控件,计算SelectCommand
GridView2.DataBind();
}


其实,都是 ASP.NET 1.x 中数据绑定的应用而已,唯一需要注意的是,调用数据控件(如GridView)的 DataBind 方法之前一定要先调用数据源控件(如SqlDataSource)的 DataBind() 方法。

完整代码:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><%@PageLanguage="C#"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<scriptrunat="server">

protected
intcategoryId=1;

protected
voidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack){
SqlDataSource1.DataBind();
SqlDataSource3.DataBind();
}

}


protected
voidDropDownList1_SelectedIndexChanged(objectsender,EventArgse)
{
categoryId
=int.Parse(DropDownList1.SelectedValue);
SqlDataSource1.DataBind();
//先执行绑定数据源控件,计算SelectCommand
GridView1.DataBind();

SqlDataSource3.DataBind();
//先执行绑定数据源控件,计算SelectCommand
GridView2.DataBind();
}


privatestringGetSelectCommandText()
{
stringsql
="SELECT*FROM[Products]";
if(DropDownList1.SelectedValue!=""){
sql
+="WHERE[CategoryID]="+int.Parse(DropDownList1.SelectedValue);
}

returnsql;
}


</script>

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>DataBindForSelectCommand2</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource2"AutoPostBack="true"
DataTextField
="CategoryName"DataValueField="CategoryID"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient"SelectCommand="SELECT[CategoryID],[CategoryName]FROM[Categories]">
</asp:SqlDataSource>

<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="ProductID"
DataSourceID
="SqlDataSource1">
<Columns>
<asp:BoundFieldDataField="ProductID"HeaderText="ProductID"InsertVisible="False"
ReadOnly
="True"SortExpression="ProductID"/>
<asp:BoundFieldDataField="ProductName"HeaderText="ProductName"SortExpression="ProductName"/>
</Columns>
</asp:GridView>
<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient"SelectCommand='<%#"SELECT*FROM[Products]WHERE[CategoryID]="+categoryId%>'><%--动态绑定SelectCommand命令--%>
</asp:SqlDataSource>

<asp:GridViewID="
GridView2"runat="server"AutoGenerateColumns="False"DataKeyNames="ProductID"
DataSourceID
="SqlDataSource3">
<Columns>
<asp:BoundFieldDataField="ProductID"HeaderText="ProductID"InsertVisible="False"
ReadOnly
="True"SortExpression="ProductID"/>
<asp:BoundFieldDataField="ProductName"HeaderText="ProductName"SortExpression="ProductName"/>
</Columns>
</asp:GridView>
<asp:SqlDataSourceID="SqlDataSource3"runat="server"ConnectionString="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient"SelectCommand='<%#GetSelectCommandText()%>'><%--动态绑定SelectCommand命令--%>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
分享到:
评论

相关推荐

    在ASP.NET 2.0中操作数据:使用SqlDataSource控件查询数据(源码)

    在ASP.NET 2.0中操作数据:使用SqlDataSource控件查询数据(源码)

    在ASP.NET 2.0中操作数据:使用SqlDataSource控件查询数据

    在ASP.NET 2.0中操作数据:使用SqlDataSource控件查询数据

    ASP.NET SqlDataSource To Access

    ASP.NET SqlDataSource To AccessASP.NET SqlDataSource To Access

    ASP.NET高级编程:三层架构概述.pptx

    走过Asp.net学习入门阶段后,真正开始着手开发一个Web项目时,才发现错综复杂的数据与关联根本就不是SqlDataSource和AccessDataSource数据源控件能简单解决的,而恰恰是被忽视了的一个ObjectDataSource数据源控件才...

    ASP.NET教学讲义(吐血推荐)

    第一章:asp.net和web窗体 第二章:WEB服务器控件 第三章:HTML控件和验证控件 第四章:ASP.NET内置对象 第五章:ASP.NET状态管理 第六章:站点设计 第七章:ASP.NET的安全性 第八章:数据绑定 第九章:...

    ASP.NET源码:某学院学生信息管理系统源码,主要功能有:

    ASP.NET源码:某学院学生信息管理系统源码,主要功能有: 用户管理 学生管理 教师管理 课程管理 班级管理 ... 主要用到的Asp.net2.0中的GridView、SqlDataSource、AequiredFieldValidator、DetailsView以及Master等。

    ASP.NET.4揭秘

    9.3 在sqldatasource控件中使用asp.net参数308 9.3.1 使用asp.net参数对象308 9.3.2 使用asp.net的controlparameter对象311 9.3.3 使用asp.net的cookieparameter对象315 9.3.4 使用asp.net的formparameter对象317 ...

    asp.net知识库

    利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的内容是否合法 asp.net报表解决方法 SQLDMO类的使用 SQL过程自动C#封装,支持从表到基本存储过程生成 使用SQLDMO控制 SQL Server 使用SQL...

    ASP.NET程序设计:留言板的实现.ppt

    Heading, T_Vip.hy_Username, T_bbsNote.tz_Time FROM T_bbsNote INNER JOIN T_Vip ON T_bbsNote.hy_VipID = T_Vip.hy_VipID"&gt; &lt;/asp:SqlDataSource&gt; 任务实施 步骤8.创建存储过程,保存所发表的留言信息。代码如下...

    asp.net教学讲义

    第一章:asp.net和web窗体 6 1.1 NET应用开发架构简介 6 1.1.1. NET框架结构 6 1.1.2 http协议简介 6 1.1.3 静态网页与动态网页 8 1.1.4 客户端代码与服务器端代码 8 1.1.5 ASP.NET简介 8 1.2 Web 窗体与ASP.NET页面...

    ASP.NET三层架构

    走过Asp.net学习入门阶段后,真正开始着手开发一个Web项目时,才发现错综复杂的数据与关联根本就不是SqlDataSource和AccessDataSource数据源控件能简单解决的,而恰恰是被忽视了的一个ObjectDataSource数据源控件才...

    ASP.NET教学讲义,完整章节

    第一章:asp.net和web窗体 4 1.1 NET应用开发架构简介 4 1.2 Web 窗体与ASP.NET页面 7 1.3 ASP.NET 页的结构 11 1.4 Page事件(页面事件) 16 1.2.2 处理回送 20 1.2.3 ASP.NET应用程序示例 23 第二章:WEB服务器...

    vs2005 asp.net中使用SqlDataSource拖拉的方式实现数据增删改查操作

    教学视频 在vs2005 asp.net中 不写一行代码 通过使用SqlDataSource 用拖拉的方式实现数据增删改查操作

    ASP.NET 控件的使用

    9.3 在SqlDataSource控件中使用ASP.NET参数 271 9.3.1 使用ASP.NET参数对象 272 9.3.2 使用ASP.NET的Control-Parameter对象 274 9.3.3 使用ASP.NET的Cookie-Parameter对象 277 9.3.4 使用ASP.NET的Form-Parameter...

    ASP.NET2.0数据库入门之SqlDataSource

    当使用SqlDataSource控件选择数据时,可以从两个属性:ConnectionString和SelectCommand开始,如下所示:<asp:SqlDataSource ID=”MySourceControlName” Runat=”server”ConnectionString=”Server=MyServer ;...

    ASP.NET4高级程序设计第4版 带目录PDF 分卷压缩包 part1

    1.1.2 要点2:ASP.NET是编译执行的,而不是解释执行的 1.1.3 要点3:ASP.NET支持多语言 1.1.4 要点4:ASP.NET运行在公共语言运行库内 1.1.5 要点5:ASP.NET是面向对象的 1.1.6 要点6:ASP.NET支持所有的...

    在ASP.NET 2.0中操作数据之四十七:用SqlDataSource控件插入、更新、删除数据

    导言:  正如在教程概述插入、更新和删除数据里讨论的...对SqlDataSource控件来说,同样如此!  对ObjectDataSource控件来说,为了实现插入、更新和删除,我们需要指定调用那个方法来实现插入、更新和删除功能。对Sql

    ASP.NET4高级程序设计(第4版) 3/3

    1.1.2 要点2:ASP.NET是编译执行的,而不是解释执行的 3 1.1.3 要点3:ASP.NET支持多语言 4 1.1.4 要点4:ASP.NET运行在公共语言运行库内 6 1.1.5 要点5:ASP.NET是面向对象的 7 1.1.6 要点6:ASP.NET支持...

    在ASP.NET 2.0中操作数据之四十六:使用SqlDataSource控件检索数据

    在Displaying Data With the ObjectDataSource 这篇教程里,我们探讨了怎样用ASP.NET 2.0的新控件–ObjectDataSource控件在表现层展示数据。  本教程到目前为止用这种层次结构来处理数据。然而绕过这种体系结构,...

Global site tag (gtag.js) - Google Analytics