【问题标题】:Popup bootstrap model on GridView edit click using RowCommand event使用 RowCommand 事件在 GridView 编辑单击时弹出引导模式
【发布时间】:2017-09-25 16:04:12
【问题描述】:

我有一个 Bootstrap 模型弹出窗口:

<asp:UpdatePanel ID="upModal" runat="server">
<ContentTemplate>

<!-- Bootstrap Modal Dialog -->
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">ADD NEW BANQUET</h4>
            </div>
            <div class="modal-body" style="padding-left: 0px;padding-right:0px">
              <div class="col-lg-12" style="padding-left: 0px;padding-right:0px">
                  <div class="form-group col-lg-6">
                    <label>Banquet ID:</label>
                    <u><asp:Label ID="lblID" CssClass="form-control" Text="AUTO ID" runat="server"></asp:Label></u>
                  </div>
                  <div class="form-group col-lg-6">
                    <label>Banquet Name:</label>
                    <asp:TextBox ID="txtName" CssClass="form-control" runat="server"></asp:TextBox>
                  </div>
              </div>

            <div class="modal-footer" style="padding-left: 0px;padding-right:0px">
              <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" 
                  CssClass="btn btn-success btn-lg btn-block" Text="SAVE" UseSubmitBehavior="false"
                  data-dismiss="modal" />
                </div>
            </div>
          </div>
    </div>
</div>
<!-- /.modal -->

</ContentTemplate>
</asp:UpdatePanel>
<!-- /.upModel -->

而 GridView 是:

<div class="col-lg-12 table-responsive">
    <asp:GridView ID="gvBanquet" CssClass="table table-striped table-bordered table-hover" runat="server" 
        AutoGenerateColumns="false" OnRowCommand="gvBanquet_RowCommand" AllowPaging="True" PageSize="5" 
        EmptyDataText="No record found!" OnPageIndexChanging="gvBanquet_PageIndexChanging" ShowHeaderWhenEmpty="true">
        <Columns>
            <asp:TemplateField HeaderText="Banquet Name">
                <ItemTemplate>
                    <asp:Label ID="lblID" runat="server" Visible="false" Text='<% #Eval("bqtID") %>'></asp:Label>
                    <asp:Label ID="lblName" runat="server" Text='<% #Eval("bqtName") %>' ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField>
                <HeaderTemplate>
                    <center>Events</center>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Button ID="btnEdiit" runat="server" CssClass="btn btn-danger btn-sm" CommandName="EditRow" Text="Edit" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle BackColor="#5cb85c" BorderColor="#4CAE4C" ForeColor="White"></HeaderStyle>
        <PagerStyle Font-Size="Larger" ForeColor="Black" HorizontalAlign="Center" />
    </asp:GridView>
</div>
<!-- /.col-lg-12 -->

这是 RowCommand 事件:

protected void gvBanquet_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
    int index = row.RowIndex;

    string id = GetTextFromGridViewLabel(gvBanquet,index,"lblID");
    string name = GetTextFromGridViewLabel(gvBanquet, index, "lblName");

    switch (e.CommandName)
    {
        case "EditRow":
            lblID.Text = id;
            txtName.Text = name;
            break;
        default:
            break;
    }
}

我的问题是当我单击 GridView 中的编辑按钮时,Bootstrap 模型没有从 GridView 行中弹出 idname

【问题讨论】:

    标签: javascript c# jquery asp.net gridview


    【解决方案1】:

    这里可以是一个解决方案并按照以下步骤操作:

    • 在另一个 UpdatePanel 中也添加 GridView。
    • 在第一个 UpdatePanel upModel 中触发 GridView RowCommand 事件,如下所示:

      <ContentTemplate>
      <Triggers>
          <asp:AsyncPostBackTrigger ControlID="gvBanquet"  EventName="RowCommand" />  
      </Triggers>
      
    • 最后在 RowCommand 事件中运行StringBuilder 代码下方:

      case "EditRow":
          lblID.Text = id;
          txtName.Text = name;
      
          System.Text.StringBuilder sb = new System.Text.StringBuilder();
          sb.Append(@"<script type='text/javascript'>");
          sb.Append("$('#myModal').modal('show');");
          sb.Append(@"</script>");
          ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "MyModal", 
          sb.ToString(), false);
      
          break;
      

    它会在编辑点击时显示弹出窗口:

    【讨论】:

      【解决方案2】:

      我会尝试通过从 GridView 本身获取值来保存 PostBack。这仅在 GridView 中存在 Modal 中需要的所有或几乎所有值时才有效。如果不是,您确实会使用 PostBack。

      在下面的 sn-p 中,模态绑定到 GridView tr 并在单击时打开。然后脚本将循环所有单元格并将它们的值放入模态文本框。出于演示目的,我在 GridView 行中添加了一个属性,以便您可以发送不想在 GridView 中显示但在模式中需要的值。

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
          <Columns>
              <asp:TemplateField>
                  <ItemTemplate>
                      <%# Eval("Column 1") %>
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField>
                  <ItemTemplate>
                      <%# Eval("Column 2") %>
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField>
                  <ItemTemplate>
                      <%# Eval("Column 3") %>
                  </ItemTemplate>
              </asp:TemplateField>
          </Columns>
      </asp:GridView>
      
      <div id="myModal" style="display: none">
          <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
          <br />
          <br />
          <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
          <br />
          <br />
          <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
          <br />
          <br />
          <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
      </div>
      
      <script type="text/javascript">
          $("#<%= GridView1.ClientID %> tr").click(function () {
              $(this).find("td").each(function (index, element) {
                  if (index == 0) {
                      $("#<%= TextBox1.ClientID %>").val(element.innerHTML);
                  } else if (index == 1) {
                      $("#<%= TextBox2.ClientID %>").val(element.innerHTML);
                  } else if (index == 2) {
                      $("#<%= TextBox3.ClientID %>").val(element.innerHTML);
                  }                
              });
      
              $("#<%= TextBox4.ClientID %>").val($(this).attr("extravalue"));
      
              $('#myModal').dialog();
          });
      </script>
      

      以及 RowDataBound 事件将额外属性添加到行的方法背后的代码。

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          //check if the row is a datarow
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              //cast the dataitem back to a row
              DataRowView row = e.Row.DataItem as DataRowView;
      
              e.Row.Attributes.Add("extravalue", row["Column 4"].ToString());
          }
      }
      

      【讨论】:

      • 可能。我使用了 jQuery 对话框。您可能需要稍微调整一下 javascript 代码。
      • 这对我来说太棒了!使用 JQuerry
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 2012-10-28
      • 2015-07-16
      相关资源
      最近更新 更多