当选中 Repeater 中的复选框时,OnCheckedChanged 事件不会触发
我是一个相对较新的开发人员,并且只全职从事此工作大约 6 个月,所以提前感谢您阅读和/或回答我的问题
我有一个数据绑定中继器。在这个中继器中,我有一个 gridview、SQLDS 和 2 个复选框。两个复选框都有一个 OnCheckedChanged 事件,并且 AutoPostback 设置为 true。转发器也有一个 OnItemDataBound 事件。
下面是我的代码布局的示例:
<asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Panel>
<asp:UpdatePanel>
<ContentTemplate>
<asp:Checkbox ID="Checkbox1" Autopostback="True" OnCheckedChanged="CheckBox1_CheckedChanged">
<asp:Checkbox ID="Checkbox2"Autopostback="True" OnCheckedChanged="CheckBox2_CheckedChanged">
<asp:Gridview ID="Gridview1" DataSourceID="SqlDataSource1">
<asp:SQLDataSource ID="SQLDataSource1" SelectCommand="SP1" SelectCommandType="StoredProcedure">
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
我在 AJAX TabPanel 中使用 C#
protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
if (Checkbox1.Checked == true)
{
if (Checkbox2.Checked == true)
SqlDataSource1.SelectCommand = "SP1";
else
SqlDataSource1.SelectCommand = "SP2";
}
else
SqlDataSource1.SelectCommand = "SP3";
}
protected void Checkbox2_CheckedChanged(object sender, EventArgs e)
{
if (Checkbox2.Checked == true)
{
if (Checkbox1.Checked == true)
SqlDataSource1.SelectCommand = "SP3";
else
SqlDataSource1.SelectCommand = "SP2";
}
else
SqlDataSource1.SelectCommand = "SP1";
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//Uses FindControl to Databind the GV and hides it if GV.Rows.Count==0
}
完成所有这些操作。我有另一个页面,该代码可以完美运行,但它不在该其他页面的转发器内。
本质上,我有一个带有 gridview 的页面加载,两个复选框更改 SP 填充 gridview 的内容。我遇到的问题是,当您取消选中复选框(它们开始选中)时,它 1. 仅重新检查自身,并且 2. 不会触发 CheckedChanged 事件。
任何帮助将不胜感激。
I am a relatively new developer, and have only been doing this for about 6 months full time, so thank you in advance for reading and/or responding to my question
I have a databound repeater. Inside this repeater, I have a gridview, SQLDS, and 2 checkboxes. Both checkboxes have a OnCheckedChanged event and AutoPostback is set to true. The repeater has an OnItemDataBound event as well.
Here is a sample of how my code is laid out:
<asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Panel>
<asp:UpdatePanel>
<ContentTemplate>
<asp:Checkbox ID="Checkbox1" Autopostback="True" OnCheckedChanged="CheckBox1_CheckedChanged">
<asp:Checkbox ID="Checkbox2"Autopostback="True" OnCheckedChanged="CheckBox2_CheckedChanged">
<asp:Gridview ID="Gridview1" DataSourceID="SqlDataSource1">
<asp:SQLDataSource ID="SQLDataSource1" SelectCommand="SP1" SelectCommandType="StoredProcedure">
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
And the C#
protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
if (Checkbox1.Checked == true)
{
if (Checkbox2.Checked == true)
SqlDataSource1.SelectCommand = "SP1";
else
SqlDataSource1.SelectCommand = "SP2";
}
else
SqlDataSource1.SelectCommand = "SP3";
}
protected void Checkbox2_CheckedChanged(object sender, EventArgs e)
{
if (Checkbox2.Checked == true)
{
if (Checkbox1.Checked == true)
SqlDataSource1.SelectCommand = "SP3";
else
SqlDataSource1.SelectCommand = "SP2";
}
else
SqlDataSource1.SelectCommand = "SP1";
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//Uses FindControl to Databind the GV and hides it if GV.Rows.Count==0
}
I'm doing all of this within an AJAX TabPanel. I have another page where this code works perfectly, but it's not inside a repeater on that other page.
Essentially, I have a page load with a gridview, and the two checkboxes change what SP fills the gridview. The problem I'm having is that when you uncheck the checkbox (they start checked), it 1. Just rechecks itself and 2. Doesn't hit the CheckedChanged event.
Any help would be GREATLY appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要为复选框设置 AutoPostBack=True 属性,并通过 UpdatePanel 控件的 Triggers 属性注册/分配添加到 UpdatePanel 的所有控件的事件处理程序。
You need to set
AutoPostBack=True
attribute for the checkboxes and also register/assign event handlers of all controls which are added into the UpdatePanel throughTriggers
property of UpdatePanel control.复选框上的自动回发导致 SQLDatasource 仅恢复到原始存储过程,因为它会加载页面并忽略 oncheckchanged 事件。
我所做的是在页面加载中获取所有数据绑定事件并将它们放入 if (!IsPostBack) 子句中,这样当 Autopostback 命中时,它不会将 SQLDS 重新绑定到原始数据价值。
这样,当自动回发发生时,它无需执行任何操作,并且会按预期触发 OnCheckedChanged 事件。
感谢大家的阅读和回复。
The Autopostback on the checkboxes is causing the SQLDatasource to just revert to the original Stored Procedure, as it hits page load and just ignores the oncheckchanged events.
What I did is I took all of my databinding events in pageload and put them inside of a
if (!IsPostBack)
clause, so that when the Autopostback hits, it doesn't rebind the SQLDS to the original value.This way, when the autopostback occurs, there's nothing for it to do, and it hits the OnCheckedChanged events as it's supposed to.
Thank you everyone for reading and responding.
ASP.NET Repeater 有一个事件会导致客户端之间发生往返。该事件称为 ItemCommand。查看此链接:- http:// /msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspx
希望这有帮助
ASP.NET Repeater has an event that causes a round trip from client to occur. The event is called ItemCommand. Check out this link :- http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspx
Hope this helps
我发现了类似的问题并解决了
http://www.codeproject.com/Questions/88960/OnCheckedChanged-Event-for-the-checkbox-inside-the/?cmt=3621
问候
i found a similar issue and solved
http://www.codeproject.com/Questions/88960/OnCheckedChanged-Event-for-the-checkbox-inside-the/?cmt=3621
Regards
首先,您需要将
UpdatePanel
置于转发器之外,并添加runat="server"
。您还应该将数据源控件放在转发器之外,并将其重用于转发器内的所有 GridView。First, you need to bring the
UpdatePanel
outside of the repeater, and addrunat="server"
. You should also bring the datasource control outside of the repeater too, and reuse it for all of the GridViews inside of the repeater.实际上,您的数据绑定方法可能在 if(!ISPOSTBACK) { } 之外调用
条件,所以在 ispostback 块中调用这个方法,我希望你的问题能够得到解决。
Actually your databinding method probably called outside the if(!ISPOSTBACK) { }
condition, so call this method inside the ispostback block, i hope your problem will be solved.