2011-05-25 130 views

回答

0

下面可能會給你的想法是如何工作的,請檢查:

<html xmlns="http://www.w3.org/1999/xhtml" > 
    <head runat="server"> 
    <title>ASP.NET Example</title> 
</head> 
<body> 
     <form id="FORM1" runat="server"> 

      <p>Show all employees with the following title: 
      <asp:DropDownList 
       id="DropDownList1" 
       runat="server" 
       AutoPostBack="True"> 
       <asp:ListItem>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:NorthwindConnection %>" 
       SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees" 
       FilterExpression="Title='{0}'" OnFiltering="SqlDataSource1_Filtering"> 
       <FilterParameters> 
        <asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/> 
       </FilterParameters> 
      </asp:SqlDataSource><br /> 

      <asp:GridView 
       id="GridView1" 
       runat="server" 
       DataSourceID="SqlDataSource1" 
       AutoGenerateColumns="False"> 
       <columns> 
        <asp:BoundField Visible="False" DataField="EmployeeID" /> 
        <asp:BoundField HeaderText="First Name" DataField="FirstName" /> 
        <asp:BoundField HeaderText="Last Name" DataField="LastName" /> 
       </columns> 
      </asp:GridView> 
       <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 

     </form> 
    </body> 
</html> 


服務器端:

protected void SqlDataSource1_Filtering(object sender, SqlDataSourceFilteringEventArgs e) 
    { 
     Label1.Text = e.ParameterValues[0].ToString(); 
    } 
+0

我想替換SqlDataSource1.FilterExpression上的撇號。我試過這個SqlDataSource1.FilterExpression.Replace(「{0}」,e.ParameterValues [0] .ToString()。替換(「'」,「''」); 但不起作用 – 2011-05-25 11:34:06

+0

在Page_Load上使用FilterExpression,如: SqlDataSource1.FilterExpression =「city ='」+ DropDownList1.SelectedValue.ToString()。Replace(「'」,「''」); +「'」; – Saurabh 2011-05-25 11:40:21

+0

does not work。I need to Like Like% text%text where quote。 – 2011-05-26 13:08:42

相關問題