【问题标题】:Modal Popup button click not fired second time模态弹出按钮单击未第二次触发
【发布时间】:2014-10-08 20:50:05
【问题描述】:

我有一个带有动态控件的模态弹出窗口。我需要在按钮点击中添加新的文本框。

JQuery:-

<script type="text/javascript">
    $(document).ready(function(){
         if($('#hdnclick').val()==1){          
        $('#modelPopup').dialog({
     autoopen:false,     
     title: "Add New Server",
     width:650,
     height:450,
     modal:true,        
     buttons:{    
      Close:function(){
      $(this).dialog('close');      
      }
     }     
     });                
          $('#btnadd').click(function(){
            alert('okay');
          });
        }
    });

    </script>  

Aspx 代码:-

  <asp:GridView ID="grdservices" runat="server" AutoGenerateColumns="false" ShowFooter="true">
          <Columns>
        <asp:BoundField DataField="S.No" HeaderText="s.no" />
        <asp:TemplateField HeaderText="service name">
         <ItemTemplate>
         <asp:TextBox ID="txtservicename" runat="server"></asp:TextBox>
     </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="description">
    <ItemTemplate>                                    
    <asp:TextBox ID="txtDescription" runat="server"></asp:TextBox>
     </ItemTemplate>
    <FooterStyle HorizontalAlign="right" />
    <FooterTemplate>    
   <asp:Button ID="btnadd" runat="server" Text="add new service" OnClick="btnadd_Click" OnRowCommand="ButtonClicked" />
    </FooterTemplate>
</asp:TemplateField>
</Columns>
/asp:GridView>

我的问题是第一次单击时触发了此“btnAddNewServic_Click”按钮单击,但第二次单击时未触发此“btnAddNewServic_Click”功能,即使第二次单击时未触发任何内容。谁能帮我解决这个问题..

输出:

添加新行:-

 protected void grdServices_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ButtonClicked")
            {
                hdnclick.Value = "1";
                AddNewRowToGrid();
            }
        }


private void AddNewRowToGrid()
        {
            int rowindex = 0;
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        TextBox Box1 = (TextBox)grdservices.Rows[rowindex].Cells[1].FindControl("txtservicename");
                        TextBox Box2 = (TextBox)grdservices.Rows[rowindex].Cells[2].FindControl("txtDescription");

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["S.No"] = i + 1;

                        dtCurrentTable.Rows[i - 1]["Column1"] = Box1.Text;
                        dtCurrentTable.Rows[i - 1]["Column2"] = Box2.Text;

                        rowindex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    grdservices.DataSource = dtCurrentTable;
                    grdservices.DataBind();
                }
            }
        }

【问题讨论】:

  • 向我们展示您添加新按钮或行的代码。
  • 我在添加新行的地方添加了代码..
  • 代码中的按钮在哪里你没有在代码中添加按钮请添加它,它会起作用
  • 我在gridview @Cracker 中添加了一个名为“btnadd”的按钮

标签: c# jquery asp.net


【解决方案1】:

使用这个

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
      if (e.CommandName == "ButtonClicked")
      {
          //Do Stuff
      }
  }

将 RowCommand 添加到 Button 并使用上面的代码获取“ButtonClicked”命令并使用您的方法将列添加到网格视图中

Reference

【讨论】:

  • 我在我的 aspx 页面中添加了 OnRowCommand。但是该模式弹出窗口不会触发任何代码隐藏。我需要解决这个@cracker。
  • 请从按钮中删除 OnClick="btnadd_Click"
  • 我删除了 Onclick="btnadd_Click" 并添加了这个 OnRowCommand="ButtonClicked".. 它被触发但不满足这个条件 if (e.CommandName == "ButtonClicked").. 它返回 ""
  • @dinesh.k 参考新的参考链接,您将了解如何使用它
  • 我错过了 Command Name ="ButtonClicked"..但是@Cracker 在我的第二次点击中它没有触发因为模式弹出......任何想法
【解决方案2】:

为按钮控件设置一个类名

<asp:Button ID="btnadd" runat="server" Text="add new service" OnClick="btnadd_Click" OnRowCommand="ButtonClicked" class="btnAdd" />

使用这个类名作为点击事件触发

$('#btnadd').click(function(){
            alert('okay');
          });

更改以下内容

$('.btnadd').click(function(){
            alert('okay');
          });

如果网格项目再次加载,您应该检查页面是否加载。我认为渲染控件是一个问题,因为您动态创建了一个控件,因此您也应该检查那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多