亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

ASP.NET DEMO 13: 如何為 SqlDataSource 動(dòng)態(tài)綁

系統(tǒng) 3395 0
對(duì)于 xxxDataSource 來(lái)說(shuō),支持綁定參數(shù),包括 ControlParameter、CookieParameter、SessionParameter、ProfileParameter 和 QueryStringParameter。假如參數(shù)值直接來(lái)自于應(yīng)用程序變量或者通過(guò)某個(gè)方法返回呢?
查閱了關(guān)于參數(shù)基類 Parameter 類 似乎不支持此功能,有一個(gè)選擇就是擴(kuò)展自己的 Parameter,但是工作量比大,本身使用 xxxDataSource 就是為了快速開(kāi)發(fā)。

這里采用比較“原始”方法:直接使用Web服務(wù)器控件都支持的綁定語(yǔ)法 <%# expression%>

先看下面這個(gè) SqlDataSource ,其中的 SelectCommand 屬性,是通過(guò)動(dòng)態(tài)綁定實(shí)現(xiàn)的,categoryId 是一個(gè)私有類字段。

< asp:SqlDataSource ID ="SqlDataSource1" runat ="server" ConnectionString ="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient" SelectCommand ='<%# "SELECT*FROM[Products]WHERE[CategoryID] ="+categoryId%>'><%--動(dòng)態(tài)綁定SelectCommand命令--%>
</asp:SqlDataSource>

可以通過(guò)控件事件中改變類字段 categoryId 的值,然后調(diào)用 SqlDataSource1.DataBind() 計(jì)算此值,得出 SelectCommand

甚至可以綁定一個(gè)方法,處理一個(gè)比較復(fù)雜sql語(yǔ)句,并返回
< asp:SqlDataSource ID ="SqlDataSource3" runat ="server" ConnectionString ="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient" SelectCommand ='<%# GetSelectCommandText()% > '> <% -- 動(dòng)態(tài)綁定SelectCommand命令 -- %>
</ asp:SqlDataSource >

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

return sql;
}

測(cè)試實(shí)例通過(guò)一個(gè) DropDownList 改變 categoryId 的值
protected void DropDownList1_SelectedIndexChanged( object sender,EventArgse)
{
categoryId
= int .Parse(DropDownList1.SelectedValue);
SqlDataSource1.DataBind();
// 先執(zhí)行綁定數(shù)據(jù)源控件,計(jì)算SelectCommand
GridView1.DataBind();

SqlDataSource3.DataBind();
// 先執(zhí)行綁定數(shù)據(jù)源控件,計(jì)算SelectCommand
GridView2.DataBind();
}


其實(shí),都是 ASP.NET 1.x 中數(shù)據(jù)綁定的應(yīng)用而已,唯一需要注意的是,調(diào)用數(shù)據(jù)控件(如GridView)的 DataBind 方法之前一定要先調(diào)用數(shù)據(jù)源控件(如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" >

< script runat ="server" >

protected
int categoryId = 1 ;

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

}


protected
void DropDownList1_SelectedIndexChanged(objectsender,EventArgse)
{
categoryId
= int .Parse(DropDownList1.SelectedValue);
SqlDataSource1.DataBind();
// 先執(zhí)行綁定數(shù)據(jù)源控件,計(jì)算SelectCommand
GridView1.DataBind();

SqlDataSource3.DataBind();
// 先執(zhí)行綁定數(shù)據(jù)源控件,計(jì)算SelectCommand
GridView2.DataBind();
}


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

return sql;
}


</ script >

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

< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False" DataKeyNames ="ProductID"
DataSourceID
="SqlDataSource1" >
< Columns >
< asp:BoundField DataField ="ProductID" HeaderText ="ProductID" InsertVisible ="False"
ReadOnly
="True" SortExpression ="ProductID" />
< asp:BoundField DataField ="ProductName" HeaderText ="ProductName" SortExpression ="ProductName" />
</ Columns >
</ asp:GridView >
< asp:SqlDataSource ID ="SqlDataSource1" runat ="server" ConnectionString ="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient" SelectCommand ='<%# "SELECT*FROM[Products]WHERE[CategoryID] ="+categoryId%>'><%--動(dòng)態(tài)綁定SelectCommand命令--%>
</asp:SqlDataSource>

<asp:GridViewID="
GridView2"runat ="server" AutoGenerateColumns ="False" DataKeyNames ="ProductID"
DataSourceID
="SqlDataSource3" >
< Columns >
< asp:BoundField DataField ="ProductID" HeaderText ="ProductID" InsertVisible ="False"
ReadOnly
="True" SortExpression ="ProductID" />
< asp:BoundField DataField ="ProductName" HeaderText ="ProductName" SortExpression ="ProductName" />
</ Columns >
</ asp:GridView >
< asp:SqlDataSource ID ="SqlDataSource3" runat ="server" ConnectionString ="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient" SelectCommand ='<%# GetSelectCommandText()% > '> <% -- 動(dòng)態(tài)綁定SelectCommand命令 -- %>
</ asp:SqlDataSource >
</ div >
</ form >
</ body >
</ html >

ASP.NET DEMO 13: 如何為 SqlDataSource 動(dòng)態(tài)綁定變量參數(shù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 婷婷夜夜躁天天躁人人躁 | 中文字幕一区二区三区精彩视频 | 中文字幕亚韩 | 日韩欧美一卡二区 | 日日拍夜夜操 | 日韩亚洲成a人片在线观看 日韩亚洲第一页 | 国内精品久久久久久久aa护士 | 香蕉精品 | 91成年人视频 | 久久女人 | 亚洲综合色网 | 最新福利在线 | 国产精品成人麻豆专区 | 全黄h全肉边做边吃奶在线观看 | 色婷婷色99国产综合精品 | 国产级a爱做片免费观看 | 国产香蕉视频在线 | 青青爽 | 欧美成人性视频在线黑白配 | 好吊妞在线成人免费 | 91香蕉福利一区二区三区 | 夭天干天天做天天免费看 | 视色视频 | 国产精品久久久久一区二区 | 久久a毛片 | 澳门四虎影院 | 国产在线91区精品 | 日韩经典欧美精品一区 | 一区二区三区免费在线视频 | 免费刺激性视频大片区 | 国内免费一区二区三区视频 | 精品少妇一区二区三区视频 | 色爱综合网欧美 | 婷婷激情在线视频 | 国产日韩欧美综合 | 福利入口在线观看 | 一区二区三区免费在线视频 | 欧美精品免费在线 | 亚洲精品日本一区二区在线 | 欧美专区在线观看 | 亚洲欧美中文字幕专区 |