Akash Patel

Akash Patel

  • NA
  • 117
  • 38.6k

Display modal popup on click of linkbutton in gridview.

Oct 26 2018 9:45 AM
I want to display modal popup on click of link button in grid view. below is the code but not displaying modal but the page refreshes.
------------------------------------------------------------
HTML
------------------------------------------------------------
<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/Admin_Master.Master" AutoEventWireup="true" CodeBehind="Search_Product.aspx.cs" Inherits="WhizCraft.Admin.Seearch_Product" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
.icon-resize
{
font-size:20px;
color:#d95459;
}
</style>
<script type="text/javascript">
function openModal() {
$('[id*=myModal').modal('show');
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="grid-form1">
<form class="form-inline">
<div class="form-horizontal">
<%--<label for="exampleInputName2" style="margin-right: 10px">
Enter Product Name</label>
<asp:TextBox ID="txtProductSearch" runat="server" CssClass="form-control" Width="70%"></asp:TextBox>--%>
<asp:GridView ID="ProductGridView" runat="server" PageSize="5" AllowPaging="True" AllowSorting="True"
CssClass="table table-bordered" AutoGenerateColumns="False"
PagerSettings-Mode="Numeric" onrowdeleting="ProductGridView_RowDeleting" >
<Columns>
<asp:BoundField HeaderText="Product ID" DataField="PID" />
<asp:BoundField HeaderText="Product Name" DataField="Product_Name" />
<%--<asp:ImageField HeaderText="Image" DataImageUrlField="Product_Image" ControlStyle-Width="50px" ControlStyle-Height="50px" ></asp:ImageField>--%>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Eval("PID")%>' CssClass="fa fa-trash icon-resize" ToolTip="Delete" aria-hidden="true" CommandName="Delete" ></asp:LinkButton>
<span><asp:LinkButton ID="lnkImageView" runat="server" OnClick="ViewImage" CommandArgument='<%#Eval("Product_Image") %>' CssClass="fa fa-picture-o icon-resize" aria-hidden="true" ToolTip="View Image" CommandName="View"></asp:LinkButton></span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
<div class="modal fade" runat="server" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h2 class="modal-title">Image</h2>
</div>
<div class="modal-body">
<asp:Image ID="ProductImg" runat="server" Width="300px" Height="300px" ImageAlign="Middle" />
</div>
<div class="modal-footer">
<%-- <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>--%>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
</div>
</asp:Content>
----------------------------------------------------------
C#
-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace WhizCraft.Admin
{
public partial class Seearch_Product : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WhizcraftDBConnect"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadProducts();
}
}
private void LoadProducts()
{
try
{
DataTable dt;
using (SqlCommand cmd = new SqlCommand("GetProducts", con))
{
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
dt = new DataTable();
ad.Fill(dt);
}
ProductGridView.DataSource = dt;
ProductGridView.DataBind();
}
}
catch (Exception ex)
{
throw;
}
}
protected void ProductGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
LinkButton lnkDelete = (LinkButton)ProductGridView.Rows[e.RowIndex].FindControl("lnkDelete");
using (SqlCommand cmd = new SqlCommand("DELETE FROM tblProduct WHERE PID=@PID", con))
{
cmd.Parameters.AddWithValue("@PID", Convert.ToInt32(lnkDelete.CommandArgument));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
LoadProducts();
}
protected void ViewImage(object sender, EventArgs e)
{
LinkButton lnkView = sender as LinkButton;
ProductImg.ImageUrl = lnkView.CommandArgument.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal();", true);
}
}
}
------------------------------------------------------------------------------------------------------- 
 
 
 

Answers (9)