ItemCommand不触发对直放站或GridView的第一次点击 [英] ItemCommand not firing on first click in Repeater or GridView

查看:130
本文介绍了ItemCommand不触发对直放站或GridView的第一次点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经快把我逼疯了,现在2天 - 希望有人见过这个

This has been driving me crazy for 2 days now - hope someone has seen this before.

我有这样的问题,即中继器或网格视图内的控制的第一次点击未能触发ItemCommand事件,所有后续工作的点击。该控件被加载到占位符上Base.aspx像这样

I have this issue where the first click of a control within a repeater or grid view fails to fire the ItemCommand event, all subsequent clicks work. The controls are being loaded into a placeholder on Base.aspx like so

    private void LoadUserControl()
    {
        string controlPath = LastLoadedControl;

        if (!string.IsNullOrEmpty(controlPath))
        {
            ph.Controls.Clear();

            UserControl uc = (UserControl)LoadControl(controlPath);
            ph.Controls.Add(uc);
        }

    }

我不知道如果这是一个视图状态的问题,并在页面事件,我应该使用的DataBind()。我已经尝试了不同的页面事件数据绑定,从而实现页面和控件上的视图状态无结果的变化。

I'm wondering if this is a viewstate issue, and in which page events I should use DataBind(). I've experimented with databind in different page events, enabling viewstate on page and control without variation in result.

这里的GridView的模板,但我也有一个Repeater控件相同的行为,所以我不相信它的控制是这个问题。

here's the GridView template , but I have also the same behaviour with a repeater control so I don't believe its the control which is this issue.

<ItemTemplate>
       <asp:RadioButton ID="rbEnable" GroupName="MyGroup" runat="server" Text="Enabled" Checked="<%# ((EducateMe.BaseTypes.AbstractLink)Container.DataItem).IsActive == true %>" />
       <asp:RadioButton ID="rbDisable" runat="server" GroupName="MyGroup" Text="Disabled" Checked="<%# ((EducateMe.BaseTypes.AbstractLink)Container.DataItem).IsActive != true %>" />
       <asp:Button ID="btnEnable" runat="server" CommandArgument="<% # Container.DataItemIndex %>" CommandName="Enable" ToolTip="Enable" Text="Save" />
       <asp:Button ID="btnDisable" runat="server" CommandArgument="<% # Container.DataItemIndex %>" Visible="false" CommandName="Disable" ToolTip="Disable" Text="Disable" />
    </ItemTemplate>

一些进一步的信息可能是相关的:

Some further info which might be relevant:

我已经注意到是在该用户的Page_Load事件是在那里我重新绑定控件。这可能是事业为控制状态被改写,但如果我在ASCX添加如果(!的IsPostBack)这一领域,这code节犯规火都像它一个aspx页面上。这将是正确的部分重新绑定我觉得控制。

What I've noticed is in the Page_Load event of the usercontrol is where I'm rebinding the control. This is probably the cause as the control state gets rewritten, but if I add a if(!IsPostback) to this area in the ascx, this code section doesnt fire at all like it does on an aspx page. That would be the correct section to rebind the control I think.

推荐答案

MikeW,结果
几个小时后,用这种摆弄的时间,我发现你的问题的根源。结果
它有什么特别做的中继器或GridView的。结果
这是一个简单的动态控制,上一回发的问题。

MikeW,
After hours and hours of fiddling with this, I found the root of your issue.
It has nothing particular to do with the repeater or the gridview.
It is simply a dynamic-controls-on-a-postback issue.

要解决您的问题:结果
令人惊讶的是,只有一行缺少在code。结果
权当您加载控件,分配的东西它的ID,像这样:

To solve your issue:
Amazingly, just one line is missing in your code.
Right when you load the control, assign something to its ID, like so:

UserControl uc = (UserControl)LoadControl(controlPath);
uc.ID = "mycontrol";
ph.Controls.Add(uc);

这样的话,当你回传,页面知道哪个控制是哪个。

That way, when you postback, the page knows which control is which.

为了更容易地说明这一点,让我们简化问题。结果
下面是点击一个按钮的情况下两次动态创建另一个按钮中的一行:

To explain this more easily, let's simplify the problem.
Here's a scenario of clicking a button twice in a row that dynamically creates another button:


  • 请一个按钮,Button1的,点击它会动态加载另一个按钮,将Button2的时候。假设将Button2的行动是显示当前时间一样简单。

  • 现在,单击Button1。

  • 要保留Button2的,你必须使用的ViewState 来告诉页面有一个动态控制。像你这样,我们就可以记住控制的路径,或者它的类的名称。

  • 右键时,页面回,在的Page_Load ,我们来看看的ViewState 来看看,有一个控制我们试图挽留,所以我们加载它在那里(这相当于你上面的 LoadUserControl()功能)。

  • 现在,Button2的是可见的,并且点击时,它会做它的作用就好了。

  • 请不要点击Button2的,只需单击Button1试(相当于你的情况两个不同的动态控件之间切换)。

  • 现在,猜猜会发生什么:的Page_Load 将从的ViewState 加载Button2的。和的Button1点击活动将加载另一个Button2的情况下,清除后的占位符。

  • 因为你没给它分配一个ID,它会自己分配之一的UniqueID ,和这两个Button2s将有类似的 ctl02 ctl03

  • 现在,点击Button2的。

  • 您会说:这很好,我们覆盖旧的。是的,但不能没有一个ID。

  • 因为你没给它一个ID,以确定它的回传,它会利用的UniqueID ,这是顺序产生。

  • 现在,该页面正在寻找的 ctl03 ,它不存在,因此点击不火。

  • 但是,现在,我们有一个全新的Button2,用的UniqueID ctl02

  • 单击此新的Button2将工作得很好,怎么就回发,它是唯一Button2的,所以它会不约而同地有一个的UniqueID ctl02

  • Make a single button, Button1, which when clicked will load another button dynamically, Button2. Assume Button2's action is as simple as displaying the current time.
  • Now, click Button1.
  • To retain Button2, you have to utilize ViewState to tell the page there's a dynamic control. Like you did, we can memorize the control's path, or the name of its class.
  • Right when the page posts back, in Page_Load, we look at the ViewState to see that there is a control we're trying to retain, so we load it right there (this is equivalent to your LoadUserControl() function above).
  • By now, Button2 is visible, and when clicked, it will do its action just fine.
  • Don't click Button2, just click Button1 again (which is equivalent to your case switching between two different dynamic controls).
  • Guess what happens now: Page_Load will load Button2 from the ViewState. And the Click event of Button1 will load ANOTHER Button2 instance, after clearing the placeholder.
  • Since you didn't assign it an ID, it will assign itself one in UniqueID, and those two Button2s will have something like ctl02 and ctl03
  • Now click Button2.
  • You would say "that's fine, we're overwriting the old one". Yes, but not without an ID.
  • Since you didn't give it an ID to identify it on the postback, it will utilize the UniqueID, which is sequentially generated.
  • Now the page is looking for ctl03, which doesn't exist, so the Click doesn't fire.
  • But, by now, we have a brand new Button2, with UniqueID ctl02.
  • Clicking this new Button2 will work just fine, coz on the postback, it's the only Button2, so it would coincidentally have a UniqueID of ctl02.

那么,如何分配一个ID使其工作?结果
这样一来,将产生有相同的ID,所以就回发,它可以找到它的寻找,它在的Page_Load 是否生成的每个新的控制或在其他按钮的点击事件。

So, how does assigning an ID make it work?
That way, each new control generated will have the same ID, so on the postback, it can find what it's looking for, whether it was generated in Page_Load or on another button's Click event.

希望这可以解释为什么它的工作原理,但就你关心,只是给它分配一个ID,一切都会好的。结果
我认为这将是有趣的分享其背后的机制,为什么是这样的话。 =)

Hope that explains why it works, but as far as you care, just assign it an ID and all will be good.
I thought it would be interesting to share the mechanism behind it and why that's the case. =)

这篇关于ItemCommand不触发对直放站或GridView的第一次点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆