使用RowCommand事件在GridView编辑上单击弹出引导模型

时间:2017-09-25 16:04:13

标签: javascript c# jquery asp.net gridview

我有一个Bootstrap Model弹出窗口:

<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

2 个答案:

答案 0 :(得分:4)

这可以是一个解决方案,并遵循以下步骤:

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

    <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;
    

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

enter image description here

答案 1 :(得分:3)

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

在下面的代码段中,模态绑定到GridView tr,并在单击时打开。然后脚本将循环所有单元格并将其值放在模态TextBoxes中。出于演示目的,我已经为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());
    }
}