【问题标题】:change DropDownList and refresh gridview更改 DropDownList 并刷新 gridview
【发布时间】:2014-01-21 20:11:47
【问题描述】:

我有一个下拉列表:

<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的查询。提前致谢!

【问题讨论】:

  • EventHandler SelectedIndexChanged 添加到您的DropDownList(或ComboBox)并读取事件中的选定值并根据该值过滤数据网格。

标签: c# asp.net linq gridview


【解决方案1】:

首先,在 DropDownList 控件中,需要添加 AutoPostBack="true"

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

我希望这会有所帮助。

【讨论】:

  • 我就像添加 AutoPostBack="true" 一样简单。谢谢!
【解决方案2】:

下面的代码可以帮助你。

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多