如何删除EntityDataSource中的WhereParameters?

时间:2011-06-29 15:35:44

标签: asp.net entity-framework gridview

我有一个带有EntityDataSource的简单GridView。我正在尝试在后面的代码中添加Where参数,这有效,但我无法清除它们。

ASPX:

Id: <asp:TextBox runat="server" Text="" ID="IDValue"></asp:TextBox>

  <asp:GridView ID="GridView1" runat="server"  
    AutoGenerateColumns="False" 

    DataSourceID="EntityDataSource1" AllowSorting="True" AllowPaging="True">

    <Columns>
      <asp:TemplateField HeaderText="Action">
        <ItemTemplate>
          <asp:HyperLink runat="server" ID="link" NavigateUrl='<%#Bind("CompanyId") %>'>Edit</asp:HyperLink>
        </ItemTemplate>
        <HeaderTemplate>Action</HeaderTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="CompanyId" HeaderText="CompanyId" ReadOnly="True" 
        SortExpression="CompanyId" />
      <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" 
        SortExpression="Name" />
      <asp:BoundField DataField="BillingAddress" HeaderText="BillingAddress" 
        ReadOnly="True" SortExpression="BillingAddress" />
      <asp:BoundField DataField="BillingCity" HeaderText="BillingCity" 
        ReadOnly="True" SortExpression="BillingCity" />
    </Columns>
  </asp:GridView>

  <br />

  <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=OrderITEntities" DefaultContainerName="OrderITEntities" 
    EnableFlattening="False" EntitySetName="Companies"  EntityTypeFilter="Company"
    Select="it.[CompanyId], it.[Name], it.[BillingAddress], it.[BillingCity]"
    AutoGenerateOrderByClause="true"
    AutoGenerateWhereClause="true"
   >

    <OrderByParameters>
      <asp:Parameter DefaultValue="CompanyId" />
    </OrderByParameters>

  </asp:EntityDataSource>

  <asp:Button runat="server" Text="Submit" />

并在后面的代码中:

protected override void OnInit(EventArgs e)
{
  EntityDataSource1.WhereParameters.Clear();
  base.OnInit(e);
}


protected void Page_Load(object sender, EventArgs e)
{
  EntityDataSource1.WhereParameters.Clear();

  if (IDValue.Text.Length > 0)
  {
    ControlParameter cp = new ControlParameter();
    cp.ControlID = "IDValue";
    cp.DbType = System.Data.DbType.Int32;
    cp.Name = "CompanyId";
    cp.PropertyName = "Text";
    EntityDataSource1.WhereParameters.Add(cp);
  }
}

请注意

  EntityDataSource1.WhereParameters.Clear();

即使调试器说Count为0,似乎也没有清除Where参数。

2 个答案:

答案 0 :(得分:1)

尝试在Page_Load中的代码后调用GridView1.DataBind()。 Init事件首先触发控件,最后一页触发,因此可能在初始页面初始化事件处理程序之前绑定它。

HTH。

答案 1 :(得分:0)

有效! 背后的新代码是:

  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      EntityDataSource1.WhereParameters.Clear();

      if (IDValue.Text.Length > 0)
      {
        ControlParameter cp = new ControlParameter();
        cp.ControlID = "IDValue";
        cp.DbType = System.Data.DbType.Int32;
        cp.Name = "CompanyId";
        cp.PropertyName = "Text";
        EntityDataSource1.WhereParameters.Add(cp);
      }

      GridView1.DataBind();
    }
  }