使用具有LIKE运算符的Linq to Sql Object数据源过滤Gridview结果

时间:2009-04-27 09:25:02

标签: asp.net linq-to-sql .net-3.5 gridview

我有一个绑定到LINQ to Sql数据源的gridview。我想使用LIKE运算符过滤gridview中的结果。即我有一个用于搜索用户名的文本框,我想选择所有用户名为[textbox value]的用户。

以下是我的代码:

<h1>Manage Users</h1>
    搜索用户     用户名:     
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataSourceID="LinqDataSource1">
    <Columns>
        <asp:BoundField DataField="UserName" HeaderText="User Name" ReadOnly="True" 
            SortExpression="UserName" />
        <asp:BoundField DataField="FullName" HeaderText="Full Name" ReadOnly="True" 
            SortExpression="FullName" />
        <asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" 
            SortExpression="Email" />
       <asp:BoundField DataField="LastLoginDate" HeaderText="Last Login" ReadOnly="True" 
            SortExpression="LastLoginDate" DataFormatString="{0:dd MMMM yyyy}"/>
       <asp:HyperLinkField Text="Edit" DataNavigateUrlFields="UserId" DataNavigateUrlFormatString="~/Pages/UsersMaintenance/CreateEditUser.aspx?UserId={0}" />
    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AirProducts.BusinessLogic.AirProductsDataContext" 

    Select="new (UserId,UserName, Details.FullName,Membership.Email,Membership.LastLoginDate)" 
    TableName="Users" Where="UserName == @UserName" >
    <WhereParameters>
        <asp:ControlParameter ControlID="txtUsername" Name="UserName" 
            PropertyName="Text" Type="String" />
    </WhereParameters>
</asp:LinqDataSource>

3 个答案:

答案 0 :(得分:2)

在LINQ表达式的 Where 子句中调用 String.Contains 方法替换equals运算符:

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AirProducts.BusinessLogic.AirProductsDataContext" 

        Select="new (UserId,UserName, Details.FullName,Membership.Email,Membership.LastLoginDate)" 
        TableName="Users" Where="UserName.Contains(@UserName)" >
        <WhereParameters>
                <asp:ControlParameter ControlID="txtUsername" Name="UserName" 
                        PropertyName="Text" Type="String" />
        </WhereParameters>
</asp:LinqDataSource>

答案 1 :(得分:0)

我无法让它在LinqDataSource的标记中工作,但能够将其捕获到此处,另外作为奖励我得到了智能感知支持。

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    AirProductsDataContext db = new AirProductsDataContext();
    e.Result = from v in db.Users orderby v.UserName ascending
               where v.UserName.Contains(txtUsername.Text)
               select new {v.UserId,v.UserName, v.UserDetail.FullName,v.Membership.Email,v.Membership.LastLoginDate};
}

答案 2 :(得分:0)

您可以获得您的要求的信息,请参阅此链接 www.delicious.com/sridharnetha

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
  if (e.CommandName.Equals("AddNew"))
  {
   TextBox txtNewName=(TextBox)GridView1.FooterRow.FindControl("txtNewName"); 
   DropDownList cmbNewGender = (DropDownList)GridView1.FooterRow.FindControl("cmbNewGender"); 
   TextBox txtNewCity = (TextBox)GridView1.FooterRow.FindControl("txtNewCity"); 
   TextBox txtNewState = (TextBox)GridView1.FooterRow.FindControl("txtNewState"); 
   DropDownList cmbNewType = (DropDownList)GridView1.FooterRow.FindControl("cmbNewType");

   customer.Insert(txtNewName.Text, cmbNewGender.SelectedValue, txtNewCity.Text, txtNewState.Text, cmbNewType.SelectedValue) ; 
   FillCustomerInGrid();
  }
}