ASP.Net 中继器项目命令未被触发

发布于 2024-08-03 04:47:15 字数 1122 浏览 7 评论 0原文

好吧,我已经使用中继器数百次了,没有任何问题,但今天出了问题。我有一个转发器,并且正在订阅 itemCommand 事件,但是当我的命令运行时,页面会回发,但事件不会被触发。

为了解决这个问题,我必须在每次回发时进行数据绑定。

我的转发器看起来像这样:

<asp:Repeater id="MyRepeater" runat="server" onitemcommand="MyRepeater_ItemCommand">
<ItemTemplate>
    <li>
    <asp:Label id="Label" runat="server" />
    <asp:LinkButton id="LinkButton1" runat="server" commandname="Complete" commandargument='<%# Eval("MyID") %>' text='<%# Eval("Title") %>' />
    </li>
</ItemTemplate>
</asp:Repeater>

我的代码隐藏如下:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    SetupPage();
    }
}

private void SetupPage()
{
    // Do other stuff

    MyRepeater.DataSource = Repository.GetStuff()
    MyRepeater.DataBind();
}


protected void MyRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{


// Do all my stuff here
}

除非我注释掉 if (!IsPostBack) 行,否则不会调用 MyRepeater_ItemCommand。一旦被注释掉并且中继器在每次回发时获得数据绑定,它就可以正常工作。我在很多其他页面上都这样做过,但在这个页面上似乎不起作用。

其他人遇到过这种行为或有解决方案吗?

OK, I've used repeaters literally hundreds of times without problems but something has gone awry today. I have a repeater and I'm subscribing to the itemCommand event, but when my command runs, the page posts back but the event isn't fired.

To get around this I'm having to do my databinding on each postback.

My repeater looks like this:

<asp:Repeater id="MyRepeater" runat="server" onitemcommand="MyRepeater_ItemCommand">
<ItemTemplate>
    <li>
    <asp:Label id="Label" runat="server" />
    <asp:LinkButton id="LinkButton1" runat="server" commandname="Complete" commandargument='<%# Eval("MyID") %>' text='<%# Eval("Title") %>' />
    </li>
</ItemTemplate>
</asp:Repeater>

and my codebehind like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    SetupPage();
    }
}

private void SetupPage()
{
    // Do other stuff

    MyRepeater.DataSource = Repository.GetStuff()
    MyRepeater.DataBind();
}


protected void MyRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{


// Do all my stuff here
}

MyRepeater_ItemCommand is not getting called unless I comment out the if (!IsPostBack) line. Once that is commented out and the repeater is getting databound on each postback it works OK. I've done this in so many other pages but on this on it just doesn't seem to work.

Anyone else come across this behaviour or have a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

掌心的温暖 2024-08-10 04:47:15

最有可能的是,您已禁用页面的 ViewState。

原因是,当您执行回发时,中继器中的所有控件通常都会从视图状态中的数据重建。然后根据控件的ID识别应该接收事件的对象,并路由事件。

如果禁用视图状态,则在回发期间不会重建控件树,因此内存中不存在应接收事件的控件。于是事件就消亡了。

如果您确实想禁用视图状态,但仍想接收事件,我有一个解决方法(而且它一点也不脏)。我长期以来一直在考虑写一篇关于它的博客文章,所以如果你愿意,我可以从我的日常琐事中抽出一点时间来描述它。

编辑:解决方法如下所述: http://petesdotnet.blogspot.dk/2009/08 /asp.html

Most likely, you have disabled ViewState for the page.

The reason is that when you execute the postback, all the controls in the repeater are rebuild from the data in the viewstate normally. Then the object that should receive the event is identified based on the ID of the control, and the event is routed.

If you disable the viewstate, the control tree is not rebuild during postback, and therefore the control that should receive the event does not exist in memory. So the event dies.

If you really want to disable the viewstate, but still want to receive the event, I have a workaround (and it's not dirty at all). I've long been thinking about writing a blog entry about it, so if you want, I can take a bit time off my normal chores, and describe it.

Edit: The workaround is described here: http://petesdotnet.blogspot.dk/2009/08/asp.html

醉殇 2024-08-10 04:47:15

删除 if (!IsPostBack) 因为这会阻止转发器重新绑定,
并且项目命令事件在回发后找不到该行。

Remove if (!IsPostBack) as this is preventing the repeater from rebinding,
and the item command event could not find the row after postback.

み格子的夏天 2024-08-10 04:47:15

我有同样的问题,除了使用更新面板之外,我的模式中有一个必需的字段验证器。我发现中继器中的 LinkBut​​tons 触发了 requiredFieldValidor 事件,然后我在中继器的 LinkBut​​tons 中添加了 CausesValidation="false" 。按预期工作。

I have the same problem and aside from using update panel, I have a required field validator in my modal. I found out that the LinkButtons in my repeater triggers the requiredFieldValidor event and then I added CausesValidation="false" in the LinkButtons of my repeater. Works as expected.

老街孤人 2024-08-10 04:47:15

当我使用 ImageButton 时,我在中继器中遇到这个问题......
我在网上搜索了这个解决方案,当 LinkBut​​ton 工作时,但 ImageButton 不工作......

然后我想,LinkBut​​ton 工作吗?所以我会使用它:)

<asp:LinkButton  CommandName="Filter" CommandArgument='<%# Eval("ID") %>' Text="" runat="server" >
<asp:image imageurl='<%#Eval("Img") %>' runat="server"/>

</asp:LinkButton> 

所以,图像位于 标签内,

玩得开心:)

I have this problem in a repeater when I use ImageButton ...
I have search the net for this solution when LinkButton work, but not ImageButton ...

Then I think, LinkButton work? so i will use it :)

<asp:LinkButton  CommandName="Filter" CommandArgument='<%# Eval("ID") %>' Text="" runat="server" >
<asp:image imageurl='<%#Eval("Img") %>' runat="server"/>

</asp:LinkButton> 

So, the image is inside the <A> tag

have fun :)

疧_╮線 2024-08-10 04:47:15

我删除了 linkbutton 中的 PostBackUrl 属性并触发了 ItemCommand。我认为回发首先运行。

I removed PostBackUrl property in linkbutton and ItemCommand fired. I think postback runs first.

滥情哥ㄟ 2024-08-10 04:47:15

这可能是您在页面上设置了验证。因此,为“链接”按钮设置一个新属性 Causevaliation =“false”。我确信它会解决问题

That may be you have set Validations on your page. So set an new attribute, causevaliation = "false" to Link button. M sure it will solve the problem

梦一生花开无言 2024-08-10 04:47:15

我遇到了类似的问题 - 结果是一些谨慎的验证控件在页面的其他地方触发。我只花了一天时间就弄清楚了......

I had a similar issue - turned out some discreet validation controls were firing elsewhere on the page. It only took me a day to figure it out ...

吃兔兔 2024-08-10 04:47:15

我对此并不肯定,但您可能必须为引发 ItemCommand 事件的按钮设置 CommandName 和可选的 CommandArgument 属性。否则,ASP.NET 会假设页面上没有您想要触发事件的按钮。你可以尝试一下。

另外,如果您不区分命令名称,为什么不使用每个按钮的 Click 事件呢?只需订阅中继器的 ItemCreatedItemDataBound 中的内容即可。

I am not positive about this, but you might have to set the CommandName and optionally CommandArgument properties for the button causing the ItemCommand event. Otherwise ASP.NET would assume that there is no button on the page, which you'd like to fire the event. You can try that.

Plus, if you're not differentiating between command names, why not use each button's Click event instead? Just subscribe to those in the repeater's ItemCreated or ItemDataBound.

暗喜 2024-08-10 04:47:15

尝试使用 Page_init 而不是 Page_load ,这应该可以解决问题。

Try using Page_init instead of Page_load and that should fix the problem.

你在我安 2024-08-10 04:47:15

试试这个:

protected void Page_Load(object sender, EventArgs e)
{
    SetupPage();
}

如果你使用nested-repeater,你应该重新绑定你的内部重复

Try this:

protected void Page_Load(object sender, EventArgs e)
{
    SetupPage();
}

If you use nested-repeater, you should rebind your inner repe

慈悲佛祖 2024-08-10 04:47:15

这是您必须在后面的代码中使用的代码..

在 PageLoad 事件之后,

 protected void Page_Load(object sender, EventArgs e)
 {

 }


 protected void Page_LoadComplete(object sender, EventArgs e)
 {
      // Bind Your Repeater here
      rptUser();
 }

现在您可以触发您的 Itemcommand..如果您得到输出,请将答案标记为正确,谢谢

Here Is the code you have to use in code behind..

after PageLoad event,

 protected void Page_Load(object sender, EventArgs e)
 {

 }


 protected void Page_LoadComplete(object sender, EventArgs e)
 {
      // Bind Your Repeater here
      rptUser();
 }

now you can fire your Itemcommand..if you get Output please mark answer as right thanks

这样的小城市 2024-08-10 04:47:15

另一件事可能是(因为它刚刚发生在我身上):如果您的数据绑定是在页面预渲染时发生的,则它不会处理 item 命令。将其切换为 load 或 init 就可以了。

One other thing that it could be (as it just happened to me): if your databind takes place when your page is prerendered, it won't process the item command. Switch it to load or init and you'll be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文