ASP.NET 动态命令按钮仅每隔一段时间触发一次
与此问题类似ASP.Net动态命令按钮事件未触发 但问题略有不同。
下面提供的是我的代码的(非常)精简版本。
protected void Page_Load(object sender, EventArgs e)
{
RenderDataItems();
}
private void RenderDataItems()
{
pnlDataItems.Controls.Clear()
DataTable dt = MyClass.GetAllData();
foreach (DataRow dr in dt.Rows)
{
Button b = new Button();
b.Command += new CommandEventHandler(SelectItem);
b.CommandArgument = dr["ID"].ToString();
b.ID = "btnData" + dr["ID"].ToString();
if (hdnDataListID.Value == dr["ID"].ToString())
{
b.Text = "Selected Item";
}
else
{
b.Text = "Pick This Item";
}
pnlDataItems.Controls.Add(b);
}
}
private void SelectItem(object sender, CommandEventArgs e)
{
hdnDataListID.Value = e.CommandArgument.ToString();
RenderDataItems();
}
private void EditSelectItem(int id)
{
MyClass mc = new MyClass(id);
hdnDataListID.Value = mc.ID.ToString();
RenderDataItems();
}
SelectItem
方法仅由 RenderDataItems
方法中呈现的按钮控件调用。 EditSelectItem
由动态创建的单独控件调用,但不需要 RenderDataItems
方法中的按钮进行所需的更改。
我已经运行了调试器并单步执行代码以查看发生了什么。加载页面时,从 PageLoad
调用 RenderDataItems
并使用具有“Pick This Text”的所有按钮填充面板(因为 HiddenField 控件的值 (<代码>hdnDataListID)尚未设置)。
第一次单击其中一个按钮时,PageLoad
中的 RenderDataItems
会触发,然后是按钮的初始填充,HiddenField 的值设置为 ID,并且第二个 RenderDataItems
调用发生在 SelectItem
方法中。按钮被清除并重新创建。正确的按钮具有“所选项目”文本。
第二次单击其中一个按钮时,PageLoad
中的 RenderDataItems
触发,然后是按钮的初始填充,但 SelectItem
方法从不火灾。
我第三次单击其中一个按钮时,会出现与第一次相同的功能。第四个模仿第二个。第五个模仿第一个。等等,等等。
当从不包含在面板中的控件中使用 EditSelectItem
方法时(它是一个 DataSource 绑定的 GridView 行,其中带有调用此方法的按钮),它的作用完全符合我的预期,并正确设置了选定/未选定按钮,每次都会调用 RenderDataItems
和 EditSelectItem
方法。
有什么想法吗?
PS 我已经删除了此页面上的所有 AJAX。
Similar to this question here ASP.Net Dynamic Command Button Event Not Firing but with a slightly different problem.
Provided below is a (very) condensed version of my code.
protected void Page_Load(object sender, EventArgs e)
{
RenderDataItems();
}
private void RenderDataItems()
{
pnlDataItems.Controls.Clear()
DataTable dt = MyClass.GetAllData();
foreach (DataRow dr in dt.Rows)
{
Button b = new Button();
b.Command += new CommandEventHandler(SelectItem);
b.CommandArgument = dr["ID"].ToString();
b.ID = "btnData" + dr["ID"].ToString();
if (hdnDataListID.Value == dr["ID"].ToString())
{
b.Text = "Selected Item";
}
else
{
b.Text = "Pick This Item";
}
pnlDataItems.Controls.Add(b);
}
}
private void SelectItem(object sender, CommandEventArgs e)
{
hdnDataListID.Value = e.CommandArgument.ToString();
RenderDataItems();
}
private void EditSelectItem(int id)
{
MyClass mc = new MyClass(id);
hdnDataListID.Value = mc.ID.ToString();
RenderDataItems();
}
The method SelectItem
is only called by the button controls rendered within the RenderDataItems
method. The EditSelectItem
is called by a separate control that is created dynamically, but does not require the altering that the buttons in the RenderDataItems
method requires.
I've run the debugger and stepped through the code to see what happens. When the page is loaded, the RenderDataItems
is called from the PageLoad
and populates the panel with all of the buttons having "Pick This Text" (because the HiddenField control's value (hdnDataListID
) has not been set).
The first time I click one of the buttons, the RenderDataItems
from PageLoad
fires, followed by the initial population of the buttons, the HiddenField's value is set to the ID, and the second RenderDataItems
call happens from within the SelectItem
method. The buttons are cleared, and recreated. The correct button has the "Selected Item" text.
The second time I click one of the buttons, the RenderDataItems
from PageLoad
fires, followed by the initial population of the buttons, but the SelectItem
method never fires.
The third time I click one of the buttons, the same functionality happens as the 1st time. The 4th mimics the 2nd. The 5th mimics the 1st. And so on, and so on.
When using the EditSelectItem
method from the controls not contained within the panel (it's a DataSource bound GridView row with buttons that calls this method), it does exactly as I'd expect and properly sets the selected / unselected buttons, with calls to both the RenderDataItems
and the EditSelectItem
method every time.
Any ideas?
P.S. I've already removed all of my AJAX on this page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该为您的 Button b 提供一个 ID。
You should give your Button b an ID.