AutoPostBack 与 jQuery 冲突

发布于 2024-11-05 06:22:40 字数 2228 浏览 0 评论 0原文

我想做这个,但是在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 技术交流群。

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

发布评论

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

评论(2

陈甜 2024-11-12 06:22:40

对于服务器控件,您可以同时拥有客户端代码和服务器逻辑。如果您打算做的就是显示该 div,那么它就是纯粹的视图逻辑,并且肯定与 javascript 有关,并且回发到服务器并刷新视图将是浪费。

你可以稍微改变你的 JavaScript 来做到这

function displayDiv(checkBox) {
        if (checkBox.checked) {
                            $("#message1").stop(true,true).show().fadeOut(4000);
            $("#message2").hide();
        }
        else {
            $("#message1").stop(true,true).hide();
            $("#message2").stop(true,true).show().fadeOut(4000);
        }

    }

一点

<asp:Checkbox ID="chkBox" AutoPostBack="True" runat="server"
      onCheckedChanged="chkBox_CheckedChanged" onclick="displayDiv(this)" />

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

function displayDiv(checkBox) {
        if (checkBox.checked) {
                            $("#message1").stop(true,true).show().fadeOut(4000);
            $("#message2").hide();
        }
        else {
            $("#message1").stop(true,true).hide();
            $("#message2").stop(true,true).show().fadeOut(4000);
        }

    }

and

<asp:Checkbox ID="chkBox" AutoPostBack="True" runat="server"
      onCheckedChanged="chkBox_CheckedChanged" onclick="displayDiv(this)" />
如歌彻婉言 2024-11-12 06:22:40

使用该 css 创建一个标签

<asp:Label id="lblChkchnged" runat="server" CssClass="message" Visible="False" />

,然后在您的检查更改事件中设置代码

lblChkchnged.visible = true;
lblChkchnged.text = chkBoc.Checked ? "Checked" : "Unchecked";

虽然实际上为什么不将其保留为客户端调用,除非您需要做更多的事情?

Create a label with that css

<asp:Label id="lblChkchnged" runat="server" CssClass="message" Visible="False" />

and then set in your check changed event the code

lblChkchnged.visible = true;
lblChkchnged.text = chkBoc.Checked ? "Checked" : "Unchecked";

Though really why not just leave it as a client call unless you need to do more than that?

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