【问题标题】:DropDown to filter out ASCX controlDropDown 过滤掉 ASCX 控件
【发布时间】:2011-02-05 14:09:26
【问题描述】:

我的表单上有一个下拉列表,它应该过滤掉或显示整个项目或特定迭代的标签云。目前,我没有收到任何错误,但 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# 文件:

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();

    }

【问题讨论】:

  • 我个人不会使用更新面板,因为更新面板让事情变得非常简单非常困难。相反,我会发出一个请求并用响应填充您希望标记云所在的区域。

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


【解决方案1】:

您将某些代码替换为“...”,因此此答案可能不正确。

根据给出的代码,我认为问题出在 .aspx 文件的 Page_Load 方法中。似乎数据集与您的下拉列表的绑定也发生在回发上。当页面收到它的回发时,它将数据集绑定到下拉列表并将其 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
    }
}

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

【讨论】:

  • 嘿TurBas,我把下拉列表的绑定放在(!ISPostBack)中,就像你说的那样,事情终于开始发生了。不过这很奇怪,因为当我从列表中选择某些迭代时会得到不同的结果。有时ascx控件根本不显示,有时显示标签云,但不正确,有时显示正确。我不知道为什么会这样:S
  • 嘿 MiziaQ,很高兴我的回答提供了一些帮助。我无法帮助您解决您在评论中描述的问题,因为您发布的代码并未涵盖该问题。
  • 我真的被TurBas困在这里。查看此链接以查看完整的 BindTagCloud 方法:stackoverflow.com/questions/4917019/tagcloud-size-problem。我应该在某处清除下拉选择吗?每次我从组合框中选择一个迭代时,我都会得到一个不同的标签云 - 有时 ascx 控件根本不显示......
  • 嘿 MiziaQ。我已经看到了 tagcloud-size-problem 的问题。很抱歉,我目前无法就您提供的信息为您提供帮助。我相信我上面的回答是正确的,解决了你的问题,可以被接受为答案。其他问题不涉及这个答案。我愿意为您提供更多帮助。请通过电子邮件与我联系(可以在我的网站上找到)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
相关资源
最近更新 更多