ControlParameter.ControlID 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定 ControlParameter 对象绑定到的控件名称。
public:
property System::String ^ ControlID { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.ControlIDConverter))]
public string ControlID { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.ControlIDConverter))>]
member this.ControlID : string with get, set
Public Property ControlID As String
属性值
表示 Web 服务器控件名称的 string
。
- 属性
示例
下面的代码示例演示如何使用 ControlParameter 对象将控件中显示的 ListBox 数据绑定到声明性方案中控件的 DropDownList 选定值。 对象ControlParameter将添加到SelectParameters窗体上控件的SqlDataSource集合中,并对应于属性中的SelectCommand“@Title”占位符文本。
<!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>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<p><asp:dropdownlist
id="DropDownList1"
runat="server"
autopostback="True">
<asp:listitem selected="True">Sales Representative</asp:listitem>
<asp:listitem>Sales Manager</asp:listitem>
<asp:listitem>Vice President, Sales</asp:listitem>
</asp:dropdownlist></p>
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
selectcommand="SELECT LastName FROM Employees WHERE Title = @Title">
<selectparameters>
<asp:controlparameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>
</selectparameters>
</asp:sqldatasource>
<p><asp:listbox
id="ListBox1"
runat="server"
datasourceid="SqlDataSource1"
datatextfield="LastName">
</asp:listbox></p>
</form>
</body>
</html>
<!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>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<p><asp:dropdownlist
id="DropDownList1"
runat="server"
autopostback="True">
<asp:listitem selected="True">Sales Representative</asp:listitem>
<asp:listitem>Sales Manager</asp:listitem>
<asp:listitem>Vice President, Sales</asp:listitem>
</asp:dropdownlist></p>
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
selectcommand="SELECT LastName FROM Employees WHERE Title = @Title">
<selectparameters>
<asp:controlparameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>
</selectparameters>
</asp:sqldatasource>
<p><asp:listbox
id="ListBox1"
runat="server"
datasourceid="SqlDataSource1"
datatextfield="LastName">
</asp:listbox></p>
</form>
</body>
</html>
下面的代码示例演示如何设置 ControlID 和 PropertyName 属性以标识对象绑定到的控件 ControlParameter 。 该示例使用 ListBox 值填充控件。 控件 SelectedValue 的属性 ListBox 用于筛选控件检索 SqlDataSource 的数据,并由控件显示 GridView 。
<%@ Page language="c#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack) {
GridView1.DataBind();
}
else {
ListBox1.Items.Add(new ListItem("Nancy Davolio", "1",true));
ListBox1.Items.Add(new ListItem("Janet Leverling", "3",true));
ListBox1.Items.Add(new ListItem("Margaret Peacock","4",true));
ListBox1.Items.Add(new ListItem("Michael Suyama", "6",true));
ListBox1.Items.Add(new ListItem("Robert King", "7",true));
ListBox1.Items.Add(new ListItem("Anne Dodsworth", "9",true));
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<p>Show Orders For:</p>
<p>
<asp:ListBox
id="ListBox1"
runat="server"
AutoPostBack="True">
</asp:ListBox></p>
<asp:SqlDataSource
id="OdbcDataSource1"
runat="server"
ProviderName="System.Data.Odbc"
DataSourceMode="DataSet"
SelectCommand="SELECT OrderID, ShipName FROM Orders WHERE EmployeeID = ?;"
ConnectionString="dsn=MyOdbcDSN;">
<SELECTPARAMETERS>
<asp:ControlParameter
PropertyName="SelectedValue"
ControlID="ListBox1"
Name="empID">
</asp:ControlParameter>
</SELECTPARAMETERS>
</asp:SqlDataSource>
<p>
<asp:GridView
id="GridView1"
runat="server"
DataSourceID="OdbcDataSource1">
</asp:GridView></p>
</form>
</body>
</html>
<%@ Page language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Private Sub Page_Load(sender As Object, e As EventArgs)
If (IsPostBack) Then
GridView1.DataBind()
Else
Dim li As ListItem
li = New ListItem("Nancy Davolio", "1",True)
ListBox1.Items.Add(li)
li = New ListItem("Janet Leverling", "3",True)
ListBox1.Items.Add(li)
li = New ListItem("Margaret Peacock","4",True)
ListBox1.Items.Add(li)
li = New ListItem("Michael Suyama", "6",True)
ListBox1.Items.Add(li)
li = New ListItem("Robert King", "7",True)
ListBox1.Items.Add(li)
li = New ListItem("Anne Dodsworth", "9",True)
ListBox1.Items.Add(li)
End If
End Sub ' Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<p>Show Orders For:</p>
<p>
<asp:ListBox
id="ListBox1"
runat="server"
AutoPostBack="True">
</asp:ListBox></p>
<asp:SqlDataSource
id="OdbcDataSource1"
runat="server"
ProviderName="System.Data.Odbc"
DataSourceMode="DataSet"
SelectCommand="SELECT OrderID, ShipName FROM Orders WHERE EmployeeID = ?;"
ConnectionString="dsn=MyOdbcDSN;">
<SELECTPARAMETERS>
<asp:ControlParameter
PropertyName="SelectedValue"
ControlID="ListBox1"
Name="empID">
</asp:ControlParameter>
</SELECTPARAMETERS>
</asp:SqlDataSource>
<p>
<asp:GridView
id="GridView1"
runat="server"
DataSourceID="OdbcDataSource1">
</asp:GridView></p>
</form>
</body>
</html>
注解
该 ControlID 属性是一个必需属性,用于标识 Control 对象在运行时绑定到的 ControlParameter 实例。
通常,ControlID为方法设置Evaluate属性PropertyName以正确绑定到控件。 如果未设置 ControlID 该属性,该方法 Evaluate 会在调用时引发异常 ArgumentException 。