FormParameter.FormField Property
Assembly: System.Web (in system.web.dll)
The FormField identifies a name/value pair that is passed in the Form collection. While the FormField property identifies the name of the pair, the FormParameter binds to its corresponding value at run time. If the expected form variable string name/value pair is not passed to the ASP.NET Web page in the collection, the Evaluate method binds the parameter to the value of the DefaultValue property, if it is set. If the DefaultValue is not set, the Evaluate method fails to bind the parameter to a value.
The following code example demonstrates how to display filtered data using an ObjectDataSource control and a FormParameter to retrieve data from a middle-tier business object and a GridView control to display the results.
The code example consists of a TextBox, a GridView control, the ObjectDataSource control, and a submit button. By default, the TextBox is populated with the name of one of the Northwind Traders employees. The GridView displays information about the employee identified by the name in the TextBox. To retrieve data on another employee, enter the full name of the employee in the TextBox and click the button.
The FilterExpression property specifies an expression used to filter the data retrieved by the SelectMethod. This property uses parameter placeholders that are evaluated to the parameters contained in the FilterParameters collection. In this example, the parameter placeholder is a FormParameter bound to the value of the TextBox control.
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %>
<%@ 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">
protected void ObjectDataSource1_Filtering(Object sender, ObjectDataSourceFilteringEventArgs e)
{
if (Textbox1.get_Text() == "")
{
e.get_ParameterValues().Clear();
e.get_ParameterValues().Add("FullName", "Nancy Davolio");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ObjectDataSource - VJ# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<p>Show all users with the following name.</p>
<asp:textbox id="Textbox1" runat="server" text="Nancy Davolio" />
<asp:gridview
id="GridView1"
runat="server"
datasourceid="ObjectDataSource1"
autogeneratecolumns="False">
<columns>
<asp:boundfield headertext="ID" datafield="EmpID" />
<asp:boundfield headertext="Name" datafield="FullName" />
<asp:boundfield headertext="Street Address" datafield="Address" />
</columns>
</asp:gridview>
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllEmployeesAsDataSet"
typename="Samples.AspNet.JSL.EmployeeLogic"
filterexpression="FullName='{0}'" OnFiltering="ObjectDataSource1_Filtering">
<filterparameters>
<asp:formparameter name="FullName" formfield="Textbox1" defaultvalue="Nancy Davolio" />
</filterparameters>
</asp:objectdatasource>
<p><asp:button id="Button1" runat="server" text="Search" /></p>
</form>
</body>
</html>
The following code example shows the middle-tier business object that the previous code example uses. This example consists of two basic classes: The EmployeeLogic class encapsulates business logic, and the NorthwindEmployee class is a model class that contains only the basic functionality required to load and persist data from the data tier. For simplicity, the EmployeeLogic class creates a static set of data, rather than retrieving the data from a data tier. It is also helpful for this example, because the sample relies on the user to provide the full name of a Northwind Traders employee to demonstrate filtering. For a complete working example, you must compile and use these classes with the ASP.NET code examples provided.
Security Note: |
|---|
| The FormParameter does not validate the value passed by the form element in any way; it uses the raw value. In most cases, you can validate the value of the FormParameter before it is used by a data source control by handling an event, such as the Selecting, Updating, Inserting, or Deleting event exposed by the data source control you are using. If the value of the parameter does not pass your validation tests, you can cancel the data operation by setting the Cancel property of the associated CancelEventArgs class to true. |
package Samples.AspNet.JSL ;
import System .* ;
import System.Collections .* ;
import System.Data .* ;
import System.Web.UI.WebControls .* ;
//
// EmployeeLogic is a stateless business object that encapsulates
// the operations you can perform on a NorthwindEmployee object.
//
public class EmployeeLogic
{
// Returns a collection of NorthwindEmployee objects.
public static ICollection GetAllEmployees()
{
ArrayList data = new ArrayList();
data.Add(new NorthwindEmployee(1, "Nancy", "Davolio",
"507 - 20th Ave. E. Apt. 2A"));
data.Add(new NorthwindEmployee(2, "Andrew", "Fuller",
"908 W. Capital Way"));
data.Add(new NorthwindEmployee(3, "Janet", "Leverling",
"722 Moss Bay Blvd."));
data.Add(new NorthwindEmployee(4, "Margaret", "Peacock",
"4110 Old Redmond Rd."));
data.Add(new NorthwindEmployee(5, "Steven", "Buchanan",
"14 Garrett Hill"));
data.Add(new NorthwindEmployee(6, "Michael", "Suyama",
"Coventry House Miner Rd."));
data.Add(new NorthwindEmployee(7, "Robert", "King",
"Edgeham Hollow Winchester Way"));
return data;
} //GetAllEmployees
public static NorthwindEmployee GetEmployee(Object anID)
{
ArrayList data = (ArrayList)GetAllEmployees();
int empID = Int32.Parse(String.valueOf(anID));
return (NorthwindEmployee)data.get_Item(empID);
} //GetEmployee
//
// To support basic filtering, the employees cannot
// be returned as an array of objects, rather as a
// DataSet of the raw data values.
public static DataSet GetAllEmployeesAsDataSet()
{
ICollection employees = GetAllEmployees();
DataSet ds = new DataSet("Table");
// Create the schema of the DataTable.
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn("EmpID", int.class.ToType());
dt.get_Columns().Add(dc);
dc = new DataColumn("FullName", String.class.ToType());
dt.get_Columns().Add(dc);
dc = new DataColumn("Address", String.class.ToType());
dt.get_Columns().Add(dc);
// Add rows to the DataTable.
DataRow row;
IEnumerator iterator = employees.GetEnumerator();
for (int iCtr = 0; iCtr < employees.get_Count(); iCtr++) {
iterator.MoveNext();
NorthwindEmployee ne =
(NorthwindEmployee)iterator.get_Current();
row = dt.NewRow();
row.set_Item("EmpID", ne.get_EmpID());
row.set_Item("FullName", ne.get_FullName());
row.set_Item("Address", ne.get_Address());
dt.get_Rows().Add(row);
}
// Add the complete DataTable to the DataSet.
ds.get_Tables().Add(dt);
return ds;
} //GetAllEmployeesAsDataSet
} //EmployeeLogic
public class NorthwindEmployee
{
public NorthwindEmployee(int anID, String aFirstName, String aLastName,
String anAddress)
{
ID = new Integer(anID);
firstName = aFirstName;
lastName = aLastName;
address = anAddress;
} //NorthwindEmployee
private Object ID;
/** @property */
public String get_EmpID()
{
return ID.toString();
} //get_EmpID
private String lastName;
/** @property */
public String get_LastName()
{
return lastName;
} //get_LastName
/** @property */
public void set_LastName(String value)
{
lastName = value;
} //set_LastName
private String firstName;
/** @property */
public String get_FirstName()
{
return firstName;
} //get_FirstName
/** @property */
public void set_FirstName(String value)
{
firstName = value;
} //set_FirstName
/** @property */
public String get_FullName()
{
return firstName + " " + lastName;
} //get_FullName
private String address;
/** @property */
public String get_Address()
{
return address;
} //get_Address
/** @property */
public void set_Address(String value)
{
address = value;
} //set_Address
} //NorthwindEmployee
Security Note: