我正在使用ASP.Net 4.5,并且使用“选择方法”面临网格视图的数据绑定问题。
网格也可以绑定自定义分页上的数据。
问题在于过滤数据。我有一个asp按钮,通过点击这个我能够使用存储过程获取过滤数据。但这次网格无法在SelectMethod中为该网格调用我在aspx页面中提到的方法。
<asp:GridView ID="gEmployee" runat="server"
AutoGenerateColumns="False"
OnRowCommand="gEmployee_Command" CellPadding="4"
OnRowDataBound="gEmployee_RowDataBound"
AllowCustomPaging="true" PageSize="10" PagerSettings-Visible="false"
CssClass="mGrid"
AllowSorting ="true"
AlternatingRowStyle-CssClass="alt"
ItemType="ModelEntityLayer.ClsEmployee" SelectMethod="BindGrid"
OnSorting ="gEmployee_OnSorting" HeaderStyle-Wrap="false" DataKeyNames="PropClockNumber" >
上面的代码显示了我为网格视图设置的属性。
public IQueryable<ClsEmployee> BindGrid()
{
int intTotalPages = 0;
objBALEmployee = new ClsEmployeeBAL();
List<ClsEmployee> employee = null;
if ((string)Session["QueryResult"] != null)
{
employee = objBALEmployee.GetPageWiseData(gEmployee.PageIndex, gEmployee.PageSize, ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : ViewState["ClockNumber"].ToString(), ViewState["SortOrder"] != null ? ViewState["SortOrder"].ToString() : "asc", ref intTotalPages, ((string)Session["QueryResult"]));
Session.Remove("QueryResult");
}
else
{
if (Convert.ToInt32(ViewState["IsEditSearch"]) != 1)
{
employee = objBALEmployee.GetPageWiseData(gEmployee.PageIndex, ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : ViewState["ClockNumber"].ToString(), ViewState["SortOrder"] != null ? ViewState["SortOrder"].ToString() : "asc", gEmployee.PageSize, ref intTotalPages);
}
else
{
employee = objBALEmployee.GetPageWiseData(gEmployee.PageIndex, gEmployee.PageSize, ViewState["SortExpression"] != null ? ViewState["SortExpression"].ToString() : ViewState["ClockNumber"].ToString(), ViewState["SortOrder"] != null ? ViewState["SortOrder"].ToString() : "asc", ref intTotalPages, dlEmployee.SelectedValue+" = '"+txtSearch.Text.Trim()+"'");
}
}
var lstEmployee = employee.AsQueryable();
if (intTotalPages > 0)
{
CustomPager.TotalPages = intTotalPages %gEmployee.PageSize == 0 ? intTotalPages /gEmployee.PageSize : intTotalPages / gEmployee.PageSize + 1;
CustomPager.Visible = true;
lblNoRecordsFound.Visible = false;
NoRecords.Visible = false;
}
else
{
CustomPager.Visible = false;
lblNoRecordsFound.Visible = true;
NoRecords.Visible = true;
}
ExportData1.DisableBtn(gEmployee);
ExportData.ExportCheck = false;
return lstEmployee;
}
上面的代码显示了我为网格中的selectMethod设置的bindGrid
方法。
protected void btnGo_Click(object sender,EventArgs e)
{
ViewState["IsEditSearch"] = "1";
hiddenSearchField.Value = dlEmployee.SelectedValue;
hiddenSearchValue.Value = txtSearch.Text.Trim();
gEmployee.SelectMethod = "BindGrid";
}
上面的代码是为按钮点击而编写的。
虽然我已经在点击代码上再次写了gEmployee.SelectMethod = "BindGrid";
,但BindGrid
没有被解雇。所以我无法将新过滤的数据绑定到网格。
请指导我。
感谢。
答案 0 :(得分:2)
我认为你需要在btnGo_Click
末尾添加一行,明确地在网格上调用DataBind():
protected void btnGo_Click(object sender, EventArgs e)
{
ViewState["IsEditSearch"] = "1";
hiddenSearchField.Value = dlEmployee.SelectedValue;
hiddenSearchValue.Value = txtSearch.Text.Trim();
gEmployee.SelectMethod = "BindGrid";
gEmployee.DataBind();
}