Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup bootstrap model on GridView edit click using RowCommand event

I have a Bootstrap Model popup:

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

And GridView is:

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

Here is RowCommand event:

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

My problem is when I click on Edit button in GridView, the Bootstrap Model not popup with id and name from GridView row.

like image 921
youpilat13 Avatar asked Sep 25 '17 16:09

youpilat13


3 Answers

I would try to save a PostBack by getting the values from the GridView itself. This only works if all or almost all values that are needed in the Modal are present in the GridView. If not you would indeed use a PostBack.

In the below snippet the modal is bound to the GridView tr and will open when clicked. Then the script will loop all cells and put their value in the modal TextBoxes. For demo purposes i've added an Attribute to the GridView row so you can send values that you do not want to show in the GridView but are required in the modal.

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

And the code behind method for the RowDataBound event to add the extra attribute to the row.

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());
    }
}
like image 123
VDWWD Avatar answered Nov 13 '22 20:11

VDWWD


Here can be a solution and follow bellow steps:

  • Add GridView also in another UpdatePanel.
  • Trigger GridView RowCommand event in first UpdatePanel upModel as like:

    <ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="gvBanquet"  EventName="RowCommand" />  
    </Triggers>
    
  • And finally run below StringBuilder code in RowCommand event:

    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;
    

It will display popup on edit click as:

enter image description here

like image 41
stefan Avatar answered Sep 17 '22 08:09

stefan


Here can be a solution and follow bellow steps:

  • Add GridView also in another UpdatePanel.
  • Trigger GridView RowCommand event in first UpdatePanel upModel as like:

    <ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="gvBanquet"  EventName="RowCommand" />  
    </Triggers>
    
  • And finally run below StringBuilder code in RowCommand event:

    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;
    

It will display popup on edit click as:

enter image description here

like image 4
youpilat13 Avatar answered Nov 13 '22 18:11

youpilat13