AutoPostBack 与 jQuery 冲突
我想做这个,但是在asp.net中。我该如何在 asp.net/背后的代码中编写这个?我已经有了
protected void chkBox_CheckedChanged(object sender, EventArgs e)
{
if (chkBox.Checked){}
else{}
}
,
<asp:Checkbox ID="chkBox" AutoPostBack="True" runat="server"
onCheckedChanged="chkBox_CheckedChanged" />
所以我需要帮助填写其余的内容。不过说真的,非常感谢!
更新2: 此方法有效,但消息/div 的显示时间不够长,无法阅读文本。发现这是由于 AutoPostBack="true"
造成的。 AutoPostBack 完成后如何调用该事件(我猜这就是解决问题所需的)?
function displayDiv(checkbox) {
if (checkbox.checked) {
$("#message1").stop(true, true).show().fadeOut(10000);
$("#message2").hide();
}
else {
$("#message1").stop(true, true).hide();
$("#message2").stop(true, true).show().fadeOut(10000);
}
}
<asp:CheckBox ID="chkNotifyMe" AutoPostBack="True" runat="server" OnCheckedChanged="chkNotifyMe_CheckedChanged" onclick="displayDiv(this)" />
<div id="message1" class="message" ><span>Successfully <small></small></span></div><br />
<div id="message2" class="message" ><span>Removed<small></small></span></div>
(所有 css 都是一样的)
如此接近,我可以品尝它:D 再次感谢!
最终解决方案 好吧,我将其添加到我的页面中以通过 jQuery 调用 AutoPostBack,并在发布后显示我的消息
function pageLoad() {
<%# autoLaunchJS %>
$("#chkNotifyMe").click(chkNotifyMe_clicked);
}
function chkNotifyMe_clicked(){
var add = $get("chkNotifyMe").checked == true;
PageMethods.WishList(add, <%#ID%>, OnSucceeded, OnFailed);
}
function OnSucceeded(){
refreshStartPage();
if($get("chkNotifyMe").checked){
$("#messageSuccess").stop(true, true).show().fadeOut(5000);
$("#messageRemove").hide();
}
else{
$("#messageSuccess").stop(true, true).hide();
$("#messageRemove").stop(true, true).show().fadeOut(5000);
}
}
I want to do this but in asp.net. How would I write this in asp.net/the code behind? I already have
protected void chkBox_CheckedChanged(object sender, EventArgs e)
{
if (chkBox.Checked){}
else{}
}
with
<asp:Checkbox ID="chkBox" AutoPostBack="True" runat="server"
onCheckedChanged="chkBox_CheckedChanged" />
so I need help filling in the rest. Seriously though, thanks so much!
UPDATE 2:
This method works but the message/div doesn't show long enough to read the text. Found out it's due to AutoPostBack="true"
. How do I call the event after the AutoPostBack is done (I'm guessing that's what I need to solve the problem)?
function displayDiv(checkbox) {
if (checkbox.checked) {
$("#message1").stop(true, true).show().fadeOut(10000);
$("#message2").hide();
}
else {
$("#message1").stop(true, true).hide();
$("#message2").stop(true, true).show().fadeOut(10000);
}
}
<asp:CheckBox ID="chkNotifyMe" AutoPostBack="True" runat="server" OnCheckedChanged="chkNotifyMe_CheckedChanged" onclick="displayDiv(this)" />
<div id="message1" class="message" ><span>Successfully <small></small></span></div><br />
<div id="message2" class="message" ><span>Removed<small></small></span></div>
(all css is the same)
So close I can taste it :D Thanks again!
FINAL SOLUTION
Alright I added this to my page to call the AutoPostBack through jQuery and after it posts, to display my message
function pageLoad() {
<%# autoLaunchJS %>
$("#chkNotifyMe").click(chkNotifyMe_clicked);
}
function chkNotifyMe_clicked(){
var add = $get("chkNotifyMe").checked == true;
PageMethods.WishList(add, <%#ID%>, OnSucceeded, OnFailed);
}
function OnSucceeded(){
refreshStartPage();
if($get("chkNotifyMe").checked){
$("#messageSuccess").stop(true, true).show().fadeOut(5000);
$("#messageRemove").hide();
}
else{
$("#messageSuccess").stop(true, true).hide();
$("#messageRemove").stop(true, true).show().fadeOut(5000);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于服务器控件,您可以同时拥有客户端代码和服务器逻辑。如果您打算做的就是显示该 div,那么它就是纯粹的视图逻辑,并且肯定与 javascript 有关,并且回发到服务器并刷新视图将是浪费。
你可以稍微改变你的 JavaScript 来做到这
一点
You can have client code and server logic at the same time for a server control. If the display of that div is all you are planning to do, then it's pure view logic and it definitely something to do with javascript and posting back to server and refreshing the view would be wasteful.
You can slightly change your javascript to do this
and
使用该 css 创建一个标签
,然后在您的检查更改事件中设置代码
虽然实际上为什么不将其保留为客户端调用,除非您需要做更多的事情?
Create a label with that css
and then set in your check changed event the code
Though really why not just leave it as a client call unless you need to do more than that?