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 以值填入 控制項。 控制項 SelectedValueListBox 屬性可用來篩選控制項擷 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 會識別物件在執行時間系結至之 屬性所 ControlID 識別物件的 Control 公用屬性 ControlParameterPropertyName 可以設定為簡單的字串,例如 「SelectedValue」,或使用語法來識別複雜的控制項屬性的運算式 Eval

PropertyName雖然 屬性是選擇性的,但 通常會 ControlID 同時設定 和 PropertyName 屬性, Evaluate 讓 方法正確地系結至 控制項。 如果您設定 ControlID 屬性,但不是 PropertyName 屬性,方法 Evaluate 會嘗試使用 ControlValuePropertyAttribute 屬性來識別預設 PropertyName 屬性。 (控制作者必須負責指定此屬性。) 如果失敗, Evaluate 則會 ArgumentException 擲回例外狀況。

下表會識別哪些 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

適用於

另請參閱