ControlParameter.ControlParameter() Constructor
Assembly: System.Web (in system.web.dll)
A ControlParameter object created with the ControlParameter constructor is initialized with default values for all its properties. The ControlID and PropertyName properties are initialized to String.Empty. Additionally, the Name property is initialized to String.Empty, the Type property is initialized to TypeCode.Object, the Direction property is initialized to Input, and the DefaultValue property is initialized to a null reference (Nothing in Visual Basic).
The following code example demonstrates how to create a ControlParameter object with the ControlParameter constructor. The ControlParameter object binds the SelectedValue property of a DropDownList control to a parameterized SQL query that retrieves data to be displayed in a DataGrid control.
<%@ Page Language="VJ#" %>
<!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)
{
SqlDataSource sqlSource = new SqlDataSource("Data Source=localhost;"
+ "Integrated Security=SSPI;Initial Catalog=Northwind;",
"SELECT FirstName, LastName FROM Employees WHERE Country = @country;");
ControlParameter country = new ControlParameter();
country.set_Name("country");
country.set_Type(System.TypeCode.String);
country.set_ControlID("DropDownList1");
country.set_PropertyName("SelectedValue");
// If the DefaultValue is not set, the DataGrid does not
// display anything on the first page load. This is because
// on the first page load, the DropDownList has no
// selected item, and the ControlParameter evaluates to
// String.Empty.
country.set_DefaultValue("USA");
sqlSource.get_SelectParameters().Add(country);
// Add the SqlDataSource to the page controls collection.
get_Page().get_Controls().Add(sqlSource);
DataGrid1.set_DataSource(sqlSource);
DataGrid1.DataBind();
} //Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList
runat="server"
AutoPostBack="True"
id="DropDownList1">
<asp:ListItem Value="USA">USA</asp:ListItem>
<asp:ListItem Value="UK">UK</asp:ListItem>
</asp:DropDownList>
<asp:DataGrid
runat="server"
id="DataGrid1" />
</form>
</body>
</html>