简体   繁体   English

通过jQuery slideToggle显示div并通过updatepanel中的隐藏按钮运行服务器代码后,div单击不起作用

[英]div click not working after showing div by Jquery slideToggle and run server code by hidden button in updatepanel

I am using jquery slideToggle to show/hide div 我正在使用jQuery slideToggle显示/隐藏div

I call a button click event to run server code when the div opened 当div打开时,我调用按钮单击事件以运行服务器代码

The div should be in an updatepanel- to avoid full postback in page div应该在updatepanel中-以避免在页面中完全回发

javascript: javascript:

$(document).ready(
 function () {
     $(".header").click(function () {
         var $header = $(this),
    $span = $header.find(">:first-child"),
         //getting the next element
    $content = $header.next();
         //open up the content needed
         $content.slideToggle(500);
         if (this.id == "ContentPlaceHolder1_AAA_divheader") {
             document.getElementById('<%= btnPostback.ClientID %>').click();
         }

     });
 });



 <asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnPostback" EventName="Click" />
    </Triggers>
    <ContentTemplate>
        <div id="divheader" runat="server" class="header">
            <span class="ProName">
                <asp:Label ID="Label8" Text="<i class='fa fa-plus-square fa-lg'></i>" runat="server"></asp:Label>
                <asp:Label ID="Label9" runat="server">Title</asp:Label>
            </span>
        </div>
        <div id="divcontent" runat="server" class="content">
 <asp:Button runat="server" ID="btnPostback" Style="display: none" OnClick="LoadData" />        
            <div id="divIfA" runat="server">
            </div>
            <div id="divifB" runat="server">
            </div>               
        </div>
    </ContentTemplate>
    </asp:UpdatePanel>

c#: C#:

  public void LoadData(object sender, EventArgs e)
{
    if (Session["IfOpen"] == null)
        Session["IfOpen"] = "1";
    else
        if (Session["IfOpen"].ToString() == "1")
            Session["IfOpen"] = "0";
        else
            Session["IfOpen"] = "1";


    if (ID > 0 && Session["IfOpen"].ToString() == "1")       
    {
       //run my code to create user control in the divIfA/divIfB
    }
    if (Session["IfOpen"].ToString() == "1")
    {
        //The (partial) postback closed the div so I open It again
        ScriptManager.RegisterStartupScript(up1, up1.GetType(), "MyFunction", " $('#ContentPlaceHolder1_AAA_divcontent').slideToggle(500);", true);
    }

}

the problem: 问题:

When running this code , the div opened in the first click, and then the click event doesn't fire at all (also the content of the div become not clickable) 运行此代码时,div会在第一次单击时打开,然后单击事件根本不会触发 (而且div的内容也变得不可单击)

So I tried with the btnPostback click, without the updatepanel, and everything worked well (but with page refresh) 所以,我想 btnPostback点击, 无需在UpdatePanel,一切运作良好(但页面刷新)

I tried with UpdatePanel , without calling btnPostback Click , the div show/hide normally (of course without the data from server) 我尝试使用 UpdatePanel, 而不调用btnPostback Click,div正常显示/隐藏(当然没有来自服务器的数据)

I need the UpdatePanel and the server code also. 我还需要UpdatePanel和服务器代码。 Does anyone have an idea why is it happening? 有谁知道为什么会这样?

<asp:UpdatePanel ID="up1" runat="server" UpdateMode="conditional">
            <ContentTemplate>
                <div id="divheader" runat="server" class="header" onclick="headerClick(this);">
                    <span class="ProName">
                        <asp:Label ID="Label8" Text="<i class='fa fa-plus-square fa-lg'></i>" runat="server"></asp:Label>
                        <asp:Label ID="Label9" runat="server">Title</asp:Label>
                    </span>
                </div>
                <div id="divcontent" runat="server" class="content">
                        <asp:Button runat="server" ID="btnPostback"  CssClass="hidden-btn" OnClick="LoadData" />
                    <div id="divIfA" runat="server">
                    </div>
                    <div id="divifB" runat="server">
                    </div>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>

Javascript : Javascript:

function headerClick(obj)
        {
     var $header =$(obj).val();
    $span = $header.find(">:first-child"),
         //getting the next element
    $content = $header.next();
         //open up the content needed
         $content.slideToggle(500);
         if (obj.id == "ContentPlaceHolder1_AAA_divheader") {
             document.getElementById('<%= btnPostback.ClientID %>').click();
         }
}

Here I had added onClick method to Div just replace and try,it's working at my end. 在这里,我在Div上添加了onClick方法,只需替换并尝试,它就可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM