Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one gridview in which i bind data through sql command.
aspx page:
<asp:GridView ID="gv_ads" runat="server" >  
</asp:GridView>

aspx.cs:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
protected void BindGridview()
    {
        var o = Session["advertise"];
        if (o != null)
        {
            ses = o.ToString();
        }

        SqlCommand cmd = new SqlCommand("sps_myads", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@email", ses);
        try
        {
            con.Open();
            gv_ads.EmptyDataText = "No Records Found";
            gv_ads.DataSource = cmd.ExecuteReader();
            gv_ads.DataBind();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

stored procedure:
SQL
ALTER PROCEDURE [dbo].[sps_myads] 
	@email nvarchar(100)
AS
BEGIN
	select CONVERT(VARCHAR(10),registertime ,104) as Register_On, r.ad_id, c.category_name, sc.subcategory_name, title from dbo.tbl_adregister r INNER JOIN tbl_category c ON r.category = c.category_id INNER JOIN tbl_subcategory sc on r.subcategory = sc.subcategory_id
	where useremail=@email
END


It showing me result according to my query, my problem is i need two buttons for every row at right side of my result: one is View and other is Delete.
View button: when i click on view button it will redirect to other page with "ad_id" as parameter so that i can display complete details of one particular clicked row "ad_id".
Delete button: when i click on delete button of particular row it should ask a confirmation dialog to delete and after click ok it will delete particular row data from gridview.

Please help programmers.
Posted

 
Share this answer
 
Try this:
For View button:
ASP.NET
<asp:templatefield headertext="No Of Seats" xmlns:asp="#unknown">
<itemtemplate>
<asp:linkbutton id="LinkButton1" runat="server"
 postbackurl="<%#Eval("id","Yourpage.aspx?id={0}") %>" forecolor="Red"> 
                                       Go to.</asp:linkbutton> >
</itemtemplate>
</asp:templatefield>


For Delete Button:
Use RowCommand Event

ASP.NET
<asp:templatefield headertext="Delete" xmlns:asp="#unknown">
            <itemtemplate>
            <asp:imagebutton id="ImageButton3" runat="server">
             ImageUrl ="../image/delete.png" CommandName ="Delete" OnClientClick="return confirm("Are you sure you want to delete this entry?");"  />
            
            </asp:imagebutton></itemtemplate>
            </asp:templatefield>


In .cs page:
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  {
      try
      {

          if (e.CommandName == "Delete")
          {
              ImageButton img = (ImageButton)e.CommandSource as ImageButton;
              GridViewRow row = img.NamingContainer as GridViewRow;

              Label lbid = (Label)row.FindControl("label1");//idlabel
              bid = Convert.ToInt32(lbid.Text);

            // pass the bid to delete

          }



      }
      catch (Exception ex)
      {
          Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Cannot be Deleted because it is used in other Pages!!!');", true);
      }

  }


you can achieve view process in rowcommand also
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900