阻止ItemCommand在刷新时触发

时间:2012-01-18 11:38:59

标签: asp.net command refresh repeater

我有一个网页控件,其中包含一个带有按钮的转发器,该按钮使用命令将项目添加到嵌套转发器

control ->
  Rounds - [+] (add group) ->
    Groups

据我所知,我需要使用ItemCommand所以我可以传递我将添加组的轮次ID的参数,但是当我执行add命令然后刷新页面再次触发命令。

我不希望这种情况发生,有没有办法知道浏览器正在刷新,可能我可以设置cookie或会话变量?

使用重定向似乎不起作用,因为我使用会话变量来存储轮次列表,并确保我在任何时候只有一个这个变量的实例我将其删除并在页面第一时将其添加为新变量打开,这似乎导致列表重置。

以下是ItemCommand事件的代码。

        protected void RoundsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "AddGroup")
        {
            if (e.CommandArgument.ToString().AsID() > 0)
            {
                Control ctrl = e.Item.FindControl("RuleGroupListctrl");

                if (ctrl != null && ctrl is RuleGroupList)
                {
                    CompetitionRuleGroup grp = new CompetitionRuleGroup();

                    CompetitionRound currentRound = Rounds.FirstOrDefault(r => r.Round == e.CommandArgument.ToString().AsID());
                    grp.CompetitionID = currentRound.CompetitionID;
                    if (currentRound != null)
                        currentRound.CompetitionRuleGroups.Add(grp);
                }
            }
        }
    }

任何帮助都将不胜感激。

由于

2 个答案:

答案 0 :(得分:0)

2条建议。

  1. 您是否已将您的网页控件置于autopostback=false模式?
  2. 为什么不将所有page_load定义放在

    if (!page.ispostback())
    

  3. 除了第一个page_load之外,每次都会跳过page_load

答案 1 :(得分:0)

您可以重定向到ItemCommand处理程序

末尾的同一页面
Response.Redirect("YourPage.aspx");

什么叫做Post-Redirect-Get(PRG)-Pattern。

http://www.andypemberton.com/engineering/the-post-redirect-get-pattern/

http://en.wikipedia.org/wiki/Post/Redirect/Get