locked
Want to display edit link Button in front of Gridview RRS feed

  • Question

  • User-1647172364 posted

    Hlo Professionals!

    I want to display edit link button in front of gridview and also when i click on edit gridview row to be display in above textboxes.

    here is my code

    Web design

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Package.aspx.cs" Inherits="WebApplication14.Package" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="body" runat="server">
         <div style="margin-top:30px; margin-left:20px">
            
             <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
             <asp:UpdatePanel ID="UpdatePanel1" runat="server">         
           <ContentTemplate>
     
                        <table>
                   
                     <tr>
                        <td colspan="4"><h1><strong style="color:black">Package</strong></h1>    
                       <hr />
     
                        </td>
                       
                     </tr>
                
         <tr>
                        <td><span style="color:black">Package Name</span><br />
                        <asp:TextBox ID ="tb_name"  runat="server" Width="200px"   /><br />
            
          
                        </td>
     
                       <td><span style="margin-left:25px; color:black">Reward<br /></span>
                        <span style="margin-left:25px"><asp:TextBox ID="tb_reward" runat="server"  Width="200px" /></span> <br />
          
                       </td>
                         
                        <td><span style=" margin-left:25px; color:black">Remarks</><br /></span>
                        <span style="margin-left:25px"><asp:TextBox ID="tb_remarks" runat="server" Width="410px" /></span> <br />
          
                        </td>
                      
             </tr>
     
                              
         <tr>   <td>      <br />
             <asp:Button ID="BT_submit" runat="server"  Text="Submit" style="border-radius:5px" Height="35px" Width="80px"   OnClick="BT_submit_Click" BackColor="#3366CC" BorderColor="#3366CC" ForeColor="White"  />
           
            <asp:Button ID="BT_update" runat="server"   Text="Update" style="border-radius:5px" Height="35px" Width="80px"   OnClick="BT_update_Click" BackColor="#3366CC" BorderColor="#3366CC" ForeColor="White" />
          </td>
             </tr>          
              
             </table>          
               
        
     
     
           <br /><asp:GridView runat="server" AutoGenerateColumns="False"     ID="Gv8" Width="80%" GridLines="Both" 
                               
                               
                                DataKeyNames="tbl_id" BackColor="White"   > 
                                <AlternatingRowStyle HorizontalAlign="Justify" Wrap="False" />
                            <Columns>
                            
                                
                                
     
                              <asp:BoundField DataField="package_name" HeaderText="Package Name" /> 
                              <asp:BoundField DataField="reward" HeaderText="Reward" /> 
                              <asp:BoundField DataField="remarks" HeaderText="Remarks" /> 
     
                             <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="Button1"  runat="server" Text="Edit" OnClick="Button1_Click" ></asp:LinkButton>
     
                                    </ItemTemplate>
     
                                </asp:TemplateField>
     
                             </Columns>
     
               
                    
                                 </asp:GridView>
                   
                </ContentTemplate></asp:UpdatePanel>       
        </div>
     
                       
     
    </asp:Content>

    C# Code

     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
     
    namespace WebApplication14
    {
        public partial class Package : System.Web.UI.Page
        {
            SqlCommand cmd = new SqlCommand();
            SqlConnection con = new SqlConnection();
            string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString();
     
            public void EstablishConnection(string storeprocedure)
            {
                con.ConnectionString = connection;
                cmd.Connection = con;
                cmd.Connection.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = storeprocedure;
            }
     
            public void CloseConnection()
            {
                cmd.Connection.Close();
                cmd.Connection.Dispose();
                con.Close();
            }
     
            
            public void Fillgridview()
            {
                SqlDataAdapter adp = new SqlDataAdapter("select * from package", connection);
                adp.SelectCommand.CommandType = CommandType.Text;
                DataTable DT = new DataTable();
                adp.Fill(DT);
     
                Gv8.DataSource = DT;
                Gv8.DataBind();
     
                
     
            }
     
            
     
     
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    Fillgridview();
                    BT_update.Visible = false;
     
     
                }
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                LinkButton btn = (LinkButton)sender;
                GridViewRow gvr = (GridViewRow)btn.NamingContainer;
                tb_name.Text = gvr.Cells[0].Text;
                tb_reward.Text = gvr.Cells[1].Text;
                tb_remarks.Text = gvr.Cells[2].Text;
     
                BT_update.Visible = true;
                BT_submit.Visible = false;
            }
            protected void BT_submit_Click(object sender, EventArgs e)
            {
     
     
                EstablishConnection("sp_insert_package");
                cmd.Parameters.Add("@package_name", SqlDbType.VarChar, 100).Value = tb_name.Text;
                cmd.Parameters.Add("@reward", SqlDbType.VarChar, 255).Value = tb_reward.Text;
                cmd.Parameters.Add("@remarks", SqlDbType.VarChar, 100).Value = tb_remarks.Text;
                cmd.Parameters.Add("@date_time", SqlDbType.VarChar, 100).Value = System.DateTime.Now.ToString();
                cmd.Parameters.Add("@By_whom", SqlDbType.VarChar, 100).Value = Session["username"].ToString();
                cmd.Parameters.Add("@Status", SqlDbType.VarChar, 100).Value = "1";
     
     
     
                try { cmd.ExecuteNonQuery(); }
                catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }
     
                Fillgridview();
     
                CloseConnection();
     
                
     
            }
     
            
            protected void BT_update_Click(object sender, EventArgs e)
            {
                decimal Tbl_id = Convert.ToDecimal(Gv8.DataKeys[0].Value.ToString());
                
     
                EstablishConnection("sp_update_package");
                cmd.Parameters.Add("@package_name", SqlDbType.VarChar, 100).Value = tb_name.Text;
                cmd.Parameters.Add("@reward", SqlDbType.VarChar, 255).Value = tb_reward.Text;
                cmd.Parameters.Add("@remarks", SqlDbType.VarChar, 255).Value = tb_remarks.Text;
                cmd.Parameters.Add("@tbl_id", SqlDbType.VarChar, 255).Value = Tbl_id.ToString();
     
     
     
                try { cmd.ExecuteNonQuery(); }
                catch (Exception ex1) { Response.Write("<script language=javascript>alert('" + ex1.Message.ToString() + ".')</script>"); }
     
                Fillgridview();
     
                CloseConnection();
     
     
     
     
            }
     
           
     
           
        }
     
    }

    Wednesday, June 10, 2020 12:22 PM

All replies

  • Wednesday, June 10, 2020 12:31 PM
  • User-1647172364 posted

    Hlo

    I checked the link in that link also edit link button is on last column but my requirement is i need edit link button in first column.

    Thanx for your understanding.

    Thursday, June 11, 2020 5:33 AM
  • User-17257777 posted

    Hi sanam13,

    You just need to put your edit column before other column in the GridView.

    Besides, you may clean up the html codes in your post.

    Best Regards,

    Jiadong Meng

    Friday, June 12, 2020 3:24 AM