欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

ASP.NET DEMO 13: 如何為 SqlDataSource 動態綁

系統 3057 0
對于 xxxDataSource 來說,支持綁定參數,包括 ControlParameter、CookieParameter、SessionParameter、ProfileParameter 和 QueryStringParameter。假如參數值直接來自于應用程序變量或者通過某個方法返回呢?
查閱了關于參數基類 Parameter 類 似乎不支持此功能,有一個選擇就是擴展自己的 Parameter,但是工作量比大,本身使用 xxxDataSource 就是為了快速開發。

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

先看下面這個 SqlDataSource ,其中的 SelectCommand 屬性,是通過動態綁定實現的,categoryId 是一個私有類字段。

< asp:SqlDataSource ID ="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:SqlDataSource ID ="SqlDataSource3" runat ="server" ConnectionString ="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True"
ProviderName
="System.Data.SqlClient" SelectCommand ='<%# GetSelectCommandText()% > '> <% -- 動態綁定SelectCommand命令 -- %>
</ asp:SqlDataSource >

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

return sql;
}

測試實例通過一個 DropDownList 改變 categoryId 的值
protected void DropDownList1_SelectedIndexChanged( object sender,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" >

< 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();
// 先執行綁定數據源控件,計算SelectCommand
GridView1.DataBind();

SqlDataSource3.DataBind();
// 先執行綁定數據源控件,計算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%>'><%--動態綁定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()% > '> <% -- 動態綁定SelectCommand命令 -- %>
</ asp:SqlDataSource >
</ div >
</ form >
</ body >
</ html >

ASP.NET DEMO 13: 如何為 SqlDataSource 動態綁定變量參數


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 色五月视频 | 国产精品丝袜视频 | 午夜小视频网站 | 青青草一区 | 欧美一级永久免费毛片在线 | 亚洲第一成人在线 | 成人黄色免费在线观看 | 玖玖爱视频在线观看 | 黄网站在线观看 | 中文字幕乱码一区二区三区 | 欧美一级片免费看 | 亚洲黑人在线观看 | 日韩精品视频免费 | 免费看黄网址 | 久久两性视频 | 中文字幕免费 | 亚洲精品国产综合一线久久 | 日本一在线中文字幕天堂 | 久久伊人久久 | 日韩成人免费在线视频 | 黄瓜av | 欧美日韩在线一区 | 国产普通话自拍 | 免费一级片 | 日本高清免费网站 | 国产欧美日韩在线 | 女猛烈无遮挡性视频免费 | 久久久久99| 精品免费视频 | 欧美一区不卡 | 看中国国产一级毛片真人视频 | gogo全球大胆高清人露出91 | 欧美一级毛片欧美大尺度一级毛片 | 最新一区二区三区 | 全免一级毛片 | 又黄又爽免费无遮挡在线观看 | 一级做a爰片久久毛片人呢 达达兔午夜起神影院在线观看麻烦 | 日韩视频在线一区 | 亚洲人成人 | 毛片a| 91精品国产免费久久 |