DropDown过滤掉ASCX控件

时间:2011-02-05 14:09:26

标签: c# asp.net asp.net-ajax updatepanel ascx

我的表单上有一个下拉列表,它应该过滤掉或显示整个项目或特定迭代的标签云。目前,我没有收到任何错误,但ASCX控件似乎没有更新。这是我的代码,任何帮助将不胜感激!

ASPX文件:

<asp:DropDownList ID="filteroptions" runat="server" onselectedindexchanged="filteroptions_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> 

<asp:UpdatePanel ID="UpdateIteration" runat="server">
                <ContentTemplate>
                        <TagCloud:TagCloudControl ID="TagCloudControl1" runat="server" />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="filteroptions" />
                </Triggers>
 </asp:UpdatePanel>

C#FILE:

protected void Page_Load(object sender, EventArgs e)
{

    ...

    filteroptions.DataSource = ds;
    filteroptions.DataTextField = "Iteration";
    filteroptions.DataValueField = "ProjectIterationID";
    filteroptions.DataBind();

    filteroptions.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Entire Project", "0"));

}

protected void filteroptions_SelectedIndexChanged(object sender, EventArgs e)
{
    string selected_iteration = filteroptions.SelectedValue;

    Session["iteration"] = selected_iteration;
}

ASCX CS文件:

string proj_id, proj_name, iteration;

protected void Page_Load(object sender, EventArgs e)
{
    proj_name = Request.QueryString["project"].ToString();
    proj_id = Request.QueryString["id"].ToString();

    if (String.IsNullOrEmpty((string)Session["iteration"]))
        iteration = "0";
    else
        iteration = (string)Session["iteration"]; 

    BindTagCloud();

}

private void BindTagCloud()
{

    int pro_id = Convert.ToInt32(proj_id);
    int iteration_id = Convert.ToInt32(iteration);

    ....

    if (iteration_id != 0)
    {
        ListView1.DataSource = tagCloudNegativeIteration;
        ListView1.DataBind();

        ListView2.DataSource = tagCloudPositiveIteration;
        ListView2.DataBind();

    }
    else
    {
        ListView1.DataSource = tagCloudNegative;
        ListView1.DataBind();

        ListView2.DataSource = tagCloudPositive;
        ListView2.DataBind();

    }

1 个答案:

答案 0 :(得分:0)

您用'...'替换了一些代码,因此这个答案可能不正确。

根据给出的代码,我认为问题出在.aspx文件的Page_Load方法中。似乎数据集与您的下拉列表的绑定也发生在Postback上。当Page收到它的回发时,它会将数据集绑定到下拉列表并将其selectedValue设置为第一个项目。完成此操作后,将处理事件并调用其侦听器。在您的方法'filteroptions_SelectedIndexChanged'中,检查selectedValue,它将具有当前第一个项目的值,而不是您选择的项目的值。

要解决此问题,请将下拉列表绑定到(!IsPostBack)中,如下所示:

protected void Page_Load(object sender, EventArgs e) {

    ...

    if (!IsPostBack)
    {
        filteroptions.DataSource = ds;
        filteroptions.DataTextField = "Iteration";
        filteroptions.DataValueField = "ProjectIterationID";
        filteroptions.DataBind();

        filteroptions.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Entire Project", "0"))l
    }
}

如果这个答案不正确,因为它是基于错误的假设,请提供完整的代码,我会和你一起思考。