ControlParameter.PropertyName 属性

定义

获取或设置由 ControlID 对象绑定到的 ControlParameter 属性标识的控件的属性名。

public:
 property System::String ^ PropertyName { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.ControlPropertyNameConverter))]
public string PropertyName { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.ControlPropertyNameConverter))>]
member this.PropertyName : string with get, set
Public Property PropertyName As String

属性值

一个 string,表示 ControlParameter 绑定到的控件的属性名。

属性

示例

下面的代码示例演示如何使用 ControlParameter 对象将 控件中显示的 ListBox 数据绑定到声明性方案中控件的 DropDownList 选定值。 控件 DropDownList 派生自 ListControl 控件。 对象 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>

下面的代码示例演示如何设置 ControlIDPropertyName 属性,以标识对象绑定到的控件 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>

注解

属性 PropertyName 标识对象的公共属性 Control ,该属性由 ControlID 对象在运行时绑定到的属性 ControlParameter 标识。 PropertyName 可以设置为简单字符串,例如“SelectedValue”,或使用语法标识复杂控件属性的表达式 Eval

PropertyName虽然 属性是可选的,但通常ControlID同时设置 和 PropertyName 属性,Evaluate以便方法正确绑定到控件。 如果设置 ControlID 属性而不是 PropertyName 属性,该方法 Evaluate 将尝试使用 ControlValuePropertyAttribute 特性来标识默认 PropertyName 属性。 (控件作者负责指定此属性。) 如果此操作失败, EvaluateArgumentException 引发异常。

下表标识了哪些 ASP.NET 控件使用 ControlValuePropertyAttribute 属性修饰属性。

控制 属性
Calendar SelectedDate
CheckBox Checked
DataList SelectedValue
DetailsView SelectedValue
FormView SelectedValue
GridView SelectedValue
Label Text
ListControl SelectedValue
Menu SelectedValue
TextBox Text
TreeView SelectedValue

适用于

另请参阅