更改DropDownList并刷新gridview

时间:2014-01-21 20:11:47

标签: c# asp.net linq gridview

我有一个DropDownList:

<asp:DropDownList class="form-control" runat="server" ID="ddlChangeStatus">
   <asp:ListItem Text="Under Review" value="1" />
   <asp:ListItem Text="Approved" value="2" /> 
   <asp:ListItem Text="Rejected" value="3" /> 
   <asp:ListItem Text="Logged" value="4" /> 
   <asp:ListItem Text="Completed" value="5" /> 
</asp:DropDownList>

当它发生变化时,我想用DropDownList的新值刷新gridview。

protected void Page_Load(object sender, EventArgs e)
{
    using (dbPSREntities5 myEntities = new dbPSREntities5())
    {
        int theStatus = Convert.ToInt32(ddlChangeStatus.SelectedValue); <--- this is the value of the DropDownList

        var allDepartments = (from tbProject in myEntities.tbProjects
                              // inner join to department lookup table
                              from refDepartments in myEntities.refDepartments.Where(x => x.refDepartmentID == tbProject.refDepartmentID) // to do a left join instead of an inner, append .DefaultIfEmpty() after this where clause
                              from refBuildings in myEntities.refBuildings.Where(x => x.refBuildingID == tbProject.refBuildingID).DefaultIfEmpty()
                              //from tbBreadCrumb in myEntities.tbBreadCrumbs.Where(x => x.ProjectID == tbProject.ProjectID)
                              from tbBreadCrumb in myEntities.tbBreadCrumbs.Where(x => x.ProjectID == tbProject.ProjectID && x.BreadCrumbID == myEntities.tbBreadCrumbs.Where(y => y.ProjectID == tbProject.ProjectID).Max(y => y.BreadCrumbID) && x.StatusID == theStatus) <---- Here is where the DropDownList value alters the query
                              from refBreadCrumb in myEntities.refBreadCrumbs.Where(x => x.refBreadCrumbID == tbBreadCrumb.StatusID)
                              // select new anon type
                              select new
                              {
                                  ProjectID = tbProject.ProjectID,
                                  Status = refBreadCrumb.BreadCrumbValue,
                                  DateSubmitted = tbBreadCrumb.CreateDateTime,
                                  refDepartmentID = tbProject.refDepartmentID,
                                  refBuildingValue = refBuildings.refBuildingValue,
                                  ProjectContactFullName = tbProject.ProjectContactFirstName + " " + tbProject.ProjectContactLastName,
                                  ProjectWorkType = tbProject.ProjectWorkType,
                                  refDepartmentValue = refDepartments.refDepartmentValue,
                              }); // I chose to turn the result into a list to demonstrate something below, you can leave it as an enumerable.

        // bind to your listview, make sure control name is accurate and ItemTemplates are defined for each data column.
        projectsListView.DataSource = allDepartments;
        projectsListView.DataBind();
    }
}

不确定最好的方法。我尝试调用Page_Load()但是没有用。基本上只是希望用户创建一个提交到页面加载的新选择,该选择运行刷新gridview的查询。提前谢谢!

2 个答案:

答案 0 :(得分:1)

以下代码可以为您提供帮助。

protected void Page_Load(object sender, EventArgs e)
    {

if(!IsPostback)
 // Bind the data to Gridview as ur business logic
}

protected void ddlChangeStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
 // Bind the data to Gridview as ur business logic
}

答案 1 :(得分:1)

首先,在DropDownList控件中,您需要添加AutoPostBack =“true”

其次,您可以在DropDownList_SelectedIndexChanged事件中的Page_Load事件中完成您正在执行的操作

我希望这会有所帮助。