==========
我的简历
==========
技术文章分类
最新技术文章
|
|
发表文章
广东程序员人才招聘网
注册
登陆
|
SqlDataSource控件参数
发表日期:2008-4-24 0:29:53
人气:444
在SqlDataSource控件中可以使用以下Asp.Net参数........
1).Parameter---------------表示一个任意的静态值.
2).ControlParameter-----------------表示控件值或页面的属性值.
3).Cookieparameter------------------表示浏览器的Cook值.
4).FormParameter---------------表示一个Html表单字段的值.
5).ProfileStringPamameter--------------表示一个配置文件属性值.
6).QueryStringParameter--------------------表示查询字符串字段中的值.
7).Sessionparameter---------------表示一个存储在会话状态中的项目值.
代码..页面中一个DropDownList控件,和一个DetailView控件,当在DropDownList控件选择一个字段时,会在DetailView控件
显示
_____________方法(1).
新建ShowControlParameter.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SqlDataSource</title>
<style type="text/css">
.floater
{
float:left;
border:solder 1px back;
padding:5px;
margin:5px;
}
</style>
<script language="javascript" type="text/javascript">
// <!CDATA[
// ]]>
</script>
</head>
<body >
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server" Height="159px" Width="266px">
<asp:DropDownList ID="DropDownList1" runat="server" Width="156px" DataSourceID="SqlDataSource1" DataTextField="CategoryID" DataValueField="CategoryID">
</asp:DropDownList>
<asp:Button ID="btnContext" runat="server" OnClick="btnContext_Click" Text="确定" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>"
SelectCommand="SELECT [CategoryID] FROM [Categories]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" Width="264px">
</asp:GridView>
</asp:Panel>
</form>
</body>
</html>
然后在ShowControlParameter.aspx.cs中编程并输入以下代码..
protected void btnContext_Click(object sender, EventArgs e)
{
SqlConnection sqlconn = new SqlConnection("server=.;database=NorthWind;uid=sa;pwd=sa");
sqlconn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from Categories where CategoryID =" + this.DropDownList1.SelectedValue + " ", sqlconn);
DataSet ds = new DataSet();
sda.Fill(ds, "Categories");
GridView1.DataSource = ds.Tables["Categories"];
GridView1.DataBind();
}
_____________方法(2).
新建ShowControlParameter.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SqlDataSource</title>
<style type="text/css">
.floater
{
float:left;
border:solder 1px back;
padding:5px;
margin:5px;
}
</style>
<script language="javascript" type="text/javascript">
// <!CDATA[
function HR1_onclick() {
}
// ]]>
</script>
</head>
<body >
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
<asp:DropDownList ID="ddlMoveies" runat="server" Width="103px" DataTextField="CategoryID" DataValueField="CategoryID" DataSourceID="SqlDataSource2"></asp:DropDownList>
<asp:DetailsView ID="dtlMovers" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="CategoryID" DataSourceID="SqlDataSource3">
<Fields>
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>" SelectCommand="SELECT [CategoryID], [CategoryName], [Picture] FROM [Categories]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>" SelectCommand="SELECT [CategoryName], [Picture], [CategoryID] FROM [Categories] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:ControlParameter Name="CategoryID" ControlID="ddlMoveies" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
<br />
</form>
</body>
</html>
|
|