触发 ItemCommand 后无法重新绑定中继器(通过 updatepanel 使用 asyncpostback)
场景如下:
我在 UpdatePanel 内有一个名为 updPanel 的转发器。
在这个转发器中,我有两个按钮,它们触发一个 ItemCommand。
在页面加载中,我有这个方法:
if (!IsPostBack)
{
Bind();
}
然后,在 Bind() 上:
public void Bind()
{
rptList.DataSource = Model.GetData(Version);
rptList.DataBind();
}
然后,在 ItemCommand 事件上:
public void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//Some code to get info about the DataItems
if (e.CommandName.Equals("MyCommandName1"))
{
//Do Stuff
}
else if (e.CommandName.Equals("MyCommandName2"))
{
//Do Other Stuff
}
Bind();
}
我有一个按钮(在同一更新面板内,但在中继器外部),除了 Bind() 之外什么也不做。
当我单击 ItemCommand 时,该命令会执行,但屏幕不会更新(其中一个项目按钮应在单击后更改其图标)。如果我刷新页面,或单击“绑定”按钮,中继器将按预期显示数据。
为什么在 ItemCommand 之后没有绑定?
提前致谢!
编辑:我的中继器:
<asp:Repeater ID="rptList" runat="server">
<HeaderTemplate>
<h3>
<b class="rollback">Rollback</b> | <b class="emteste">Em Teste</b> | <b class="aindanaoincluido">
Ainda não Incluído</b> | <b class="parcialmenteok">Atividade Parcialmente OK</b>
| <b class="todosok">Atividade OK em Todas as Lojas</b> | <b class="publicado">Atividade
Publicada</b>
</h3>
</HeaderTemplate>
<ItemTemplate>
<div class="item" style="display: inline-block">
<p style="font-size: 12px">
<asp:ImageButton ToolTip="Marcar como Rollback" ImageUrl="/_layouts/Extensions/Versioning/img/rollback.png"
CommandName="MarkAsRollback" ID="btnItemRollback" CssClass="itembutton" runat="server" />
<asp:ImageButton ToolTip="Esta atividade possui código. Clique para sinalizar como apenas procedure/config"
ImageUrl="/_layouts/Extensions/Versioning/img/code.png" CommandName="MarkAsProc"
ID="btnItemProc" CssClass="itembutton" runat="server" />
<asp:Image ToolTip="Esta atividade não possui scripts de banco/zips." ImageUrl="/_layouts/Extensions/Versioning/img/noscript.png"
runat="server" CssClass="itembutton" ID="btnItemScript" />
<asp:Label ID="labelWI" Text="" runat="server"></asp:Label>
<span style="color: #4A82CB">
<%# DataBinder.Eval(Container.DataItem, "SystemAssignedTo") %>
- </span><span style="color: Navy">
<%# DataBinder.Eval(Container.DataItem, "SystemTitle") %>
</span>
<asp:HiddenField ID="workItemID" runat="server" />
</p>
</div>
<hr class="item" noshade style="color: #4CBDCB; height: 2px; background-color: #4CBDCB" />
</ItemTemplate>
</asp:Repeater>
编辑:为了简化,这是我得到的行为:
单击中继器外部的按钮:图标更改。数据库更新。
单击转发器中项目内的按钮:图标不会更改。数据库更新。
再次单击转发器中项目内的按钮:图标发生变化。数据库更新。但现在相应的项目是错误的,因为我已经再次更改了标志
这似乎与事情发生的顺序有关。
编辑:我忘了提及,我使用完整的回发进行了测试,一切正常。
Here's the scenario:
I have a repeater inside an UpdatePanel called updPanel.
Inside this repeater I have two Buttons, which fire an ItemCommand.
In the page load I have this method:
if (!IsPostBack)
{
Bind();
}
Then, on Bind():
public void Bind()
{
rptList.DataSource = Model.GetData(Version);
rptList.DataBind();
}
Then, on ItemCommand event:
public void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//Some code to get info about the DataItems
if (e.CommandName.Equals("MyCommandName1"))
{
//Do Stuff
}
else if (e.CommandName.Equals("MyCommandName2"))
{
//Do Other Stuff
}
Bind();
}
I have a button (inside the same update panel, but outside the repeater) which does nothing but Bind().
When I click an ItemCommand, the command executes, but the screen is not updated (One of the item buttons should change its icon after clicking). If I refresh the page, or click the "Bind" button, the repeater shows the data as expected.
Why isn't it binding after ItemCommand?
Thanks in advance!
EDIT: My Repeater:
<asp:Repeater ID="rptList" runat="server">
<HeaderTemplate>
<h3>
<b class="rollback">Rollback</b> | <b class="emteste">Em Teste</b> | <b class="aindanaoincluido">
Ainda não Incluído</b> | <b class="parcialmenteok">Atividade Parcialmente OK</b>
| <b class="todosok">Atividade OK em Todas as Lojas</b> | <b class="publicado">Atividade
Publicada</b>
</h3>
</HeaderTemplate>
<ItemTemplate>
<div class="item" style="display: inline-block">
<p style="font-size: 12px">
<asp:ImageButton ToolTip="Marcar como Rollback" ImageUrl="/_layouts/Extensions/Versioning/img/rollback.png"
CommandName="MarkAsRollback" ID="btnItemRollback" CssClass="itembutton" runat="server" />
<asp:ImageButton ToolTip="Esta atividade possui código. Clique para sinalizar como apenas procedure/config"
ImageUrl="/_layouts/Extensions/Versioning/img/code.png" CommandName="MarkAsProc"
ID="btnItemProc" CssClass="itembutton" runat="server" />
<asp:Image ToolTip="Esta atividade não possui scripts de banco/zips." ImageUrl="/_layouts/Extensions/Versioning/img/noscript.png"
runat="server" CssClass="itembutton" ID="btnItemScript" />
<asp:Label ID="labelWI" Text="" runat="server"></asp:Label>
<span style="color: #4A82CB">
<%# DataBinder.Eval(Container.DataItem, "SystemAssignedTo") %>
- </span><span style="color: Navy">
<%# DataBinder.Eval(Container.DataItem, "SystemTitle") %>
</span>
<asp:HiddenField ID="workItemID" runat="server" />
</p>
</div>
<hr class="item" noshade style="color: #4CBDCB; height: 2px; background-color: #4CBDCB" />
</ItemTemplate>
</asp:Repeater>
EDIT: To simplify, this is the behavior i'm getting:
Clicking on a button outside the repeater: Icon changes. DB Updates.
Clicking on a button inside an item in the repeater: Icon doesn't change. DB Updates.
Clicking again on a button inside an item in the repeater: Icon changes. DB Updates. But now the corresponding item is wrong because I already changed the flag again
It seems to have to do with the order in which things happen.
EDIT: I forgot to mention, I tested with a full postback and everything works ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问您的问题 - 您希望在单击项目按钮后看到哪些更改?您是否正在更新转发器中的按钮控件或更改数据源?
我怀疑您的控件正在反弹,但您没有看到任何更改,因为您正在覆盖已更改的按钮。在代码中设置一些断点或记录消息(但不要将 response.write 与 ASP.Net AJAX 一起使用 - 它会破坏异步回发中的内容)并查看实际发生的情况。是否正在执行bind()方法?
Question for you - what changes are you making that you expect to see after clicking your item button? Are you updating the button control in the repeater or making a change to the datasource?
I suspect that your control is being rebound but you don't see any changes because you're overwriting the button that you've changed. Set some breakpoints or logging messages in your code (don't use response.write with ASP.Net AJAX, though - it will break things in async postbacks) and see what's actually happening. Is the bind() method being executed?
您什么时候加载
myDataSource
?如果您在
Page_Load
中加载数据(该操作在rptList_ItemCommand
之前执行),则bind()
将绑定一组旧数据到中继器。这可以解释你的第三个总结点:您能给我们提供有关您的数据源和中继器外部按钮的事件处理程序的更多详细信息吗?
When are you loading
myDataSource
?If you're loading data into it in
Page_Load
, which is executed beforerptList_ItemCommand
, thenbind()
would be binding an old set of data to the repeater. This would explain your third summary point:Can you give us more details about your datasource and the event handler for the button outside the repeater?