回发后jQuery显示/隐藏不工作

时间:2012-11-06 16:36:08

标签: jquery c#-4.0 postback

我在使用jQuery显示隐藏div的回发后遇到问题。有人建议我将可见性值存储在隐藏字段中,但我遇到了一些问题。下面是代码:

<script type="text/javascript">
$(document).ready(function () {
    $(".slidingDiv").hide();
    $(".show_hide").show();
    $("goBtn").click(function () {
        $(".slidingDiv").slideToggle();
    });
    return false;
});  
</script>

<div class="create">
            <asp:TextBox ID="createTextbox" runat="server" Width="350px" Height="22px"></asp:TextBox>
            <asp:ImageButton ID="goBtn" ImageUrl="img/goBtn.gif" runat="server" BorderStyle="None" onclick="createButton_Click"/>
        </div>
 <div class="slidingDiv" runat="server">
        <asp:HiddenField ID="IsCreatePostbackVisible" runat="server" Visible="False" value="false"/>
            <asp:Literal ID="Literal1" runat="server"></asp:Literal><br />
        </div>

代码隐藏:

    protected void createButton_Click(object sender, ImageClickEventArgs e)
    {
        string randAlias = Base62Random();
        string aliasInput = createTextbox.Text;
        DateTime date = DateTime.Now;
        DateTime endDate = (DateTime.Now.AddYears(5));
        string UserID = HttpContext.Current.User.Identity.Name.ToUpper();
        string aliasType = "alias";
        if (aliasInput == "")
        {
            ClientScript.RegisterClientScriptBlock(GetType(), "Javascript", "<script>alert('You must enter a valid URL')</script>");
        }
        else
        {
            Literal1.Text = "<a href=http://go/" + randAlias + ">go/" + randAlias;
        }


        using (DataClasses1DataContext dc1 = new DataClasses1DataContext())
        {
            tblData tblData = new tblData();
            tblData.ALIAS_ID = randAlias;
            tblData.URL = aliasInput;
            tblData.START = date;
            tblData.END = endDate;
            tblData.USER = UserID;
            tblData.TYPE = aliasType;
            dc1.tblDatas.InsertOnSubmit(tblData);
            dc1.SubmitChanges();
        }
    }

2 个答案:

答案 0 :(得分:0)

您可能需要将点击事件“重新注册”到您的按钮。

我过去曾遇到类似的隐藏和回发问题。

如果你定义一个函数:

 function ClickListener() {
    $("#goBtn").click(function () {
            $(".slidingDiv").slideToggle();
            ClickListener();
        });
    }

然后希望它不止一次起作用。

编辑:我也遇到类似的问题,将document.ready()更改为document.live()已解决了问题。见这里:jQuery difference between live() and ready()?

答案 1 :(得分:0)

以下是我最终得到的答案:

<script type="text/javascript">
    $(document).ready(function () {
    $(".slidingDiv").hide();
    $(".show_hide").show();
    $("#goBtn").click(function () {
        $(".slidingDiv").slideToggle();
    })
    if (isPostBack){$(".slidingDiv").show();}
});  
</script>

代码背后:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = true;", true);}
        else 
        {ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = false;", true);}
    }