第一次打开jquery对话框时的奇怪问题(在asp.net gridview中)

oJM*_*86o 7 asp.net jquery

我在updatepanel中有一个gridview.gridview中的一个字段是ASP.net,linkbutton如下所示:

 <ItemTemplate>
       <asp:LinkButton ID="hlSortOrder" runat="server" CssClass="hlDialog" OnClick="LoadLog"
        Text='<%# DataBinder.Eval(Container, "DataItem.SortOrder") %>'></asp:LinkButton>
  </ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

当有人点击链接按钮时,我调用OnClick我创建的方法调用LoadLog.LoadLog如下所示:

protected void LoadLog(object sender, EventArgs e)
        {
            GridViewRow gr = (GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent;
            Label l = (Label)gr.FindControl("lblID");
            DataSet ds;

            ds = BL.GetRunoffAnswerLog(Convert.ToInt64(l.Text));

            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count == 0)
                {
                    gvLog.Visible = false;
                    gvLog.DataSource = null;
                    lblRowsCount.Text = "No log for this record!";
                }
                else
                {
                    lblRowsCount.Text = ds.Tables[0].Rows.Count.ToString() + " row(s) found for this record.";
                    gvLog.DataSource = ds.Tables[0];
                    gvLog.DataBind();
                    gvLog.Visible = true;
                }
            }
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openDialog", "$('#dialog').dialog({draggable: true, modal: true, height: 500, width: 750, title: 'Log', open: function (type, data) {$(this).parent().appendTo('form');}});", true);
        }
Run Code Online (Sandbox Code Playgroud)

基本上它获取网格视图行的句柄,从数据库中提取一些数据并将其分配给gvLog源.之后注意到最后一行:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openDialog", "$('#dialog').dialog({draggable: true, modal: true, height: 500, width: 750, title: 'Log', open: function (type, data) {$(this).parent().appendTo('form');}});", true);

我必须这样做才能打开我的对话框.当我第一次点击gridview中的一行时,第一次得到这个:

在此输入图像描述

请注意,它只显示标题......很奇怪.但是,一旦我再次单击该行,它将显示整个对话框:

在此输入图像描述

它只发生在第一次点击,如果我一直点击不同的行,它工作正常.我应该补充一点,我必须添加以下jquery代码:

 <script type="text/javascript">
        $(document).ready(function () {
            var prm = Sys.WebForms.PageRequestManager.getInstance();

            prm.add_endRequest(function () {
                $("#dialog").hide();
                // re-bind your jQuery events here 
           });
        ....more code...
Run Code Online (Sandbox Code Playgroud)

基于这个讨论:jQuery $(document).ready和UpdatePanels?

如果我没有那个代码,那么回复发生的那一刻,这个对话框所在的整个div总是显示在我的页面上,我不希望这样......

正如下面的一位成员所提到的那样.我相信正在发生的事情是你第一次点击链接按钮,客户端事件首先发生,打开实际打开的对话框,即使我在服务器端代码中引发此事件...正如你在上面看到的那样,只有当你点击单击"LoadLog"事件,我注册了这个jquery opendialog.但是看起来这仍然是第一次打开对话框,一旦你第二次点击它就会显示数据.

bri*_*ley 0

看到这个

尝试添加呼叫$('#dialog').dialog('open')

您的第一次调用$('#dialog').dialog([Parmas])可能是创建它但不是打开它。

像这样的东西:

 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openDialog", "$('#dialog').dialog({draggable: true, modal: true, height: 500, width: 750, title: 'Log', open: function (type, data) {$(this).parent().appendTo('form');}});$('#dialog').dialog('open');", true);
Run Code Online (Sandbox Code Playgroud)

华泰