LinkButton Click Event Inside GridView Using SelectedIndexChanged in Asp.Net C#

LinkButton Click Event Inside GridView


LinkButton Handle ClickEvent  Inside the Gridview  Row  Details Show to Panel Using SelectedIndexChanged.


                             Download Coding

                                                Download




                                   DEMO







HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
      <table><tr><td>
          <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
               OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="40px">
              <AlternatingRowStyle BackColor="#FF9999" />
              <Columns>
                  <asp:TemplateField HeaderText="ID">
                      <ItemTemplate>
                          <br />
                          <asp:Label ID="lblID" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                          <br />
                          <br />
                          <br />
                          <asp:Panel ID="Panel1" runat="server" Visible="False">
                              City
                              <asp:Label ID="lblCity" runat="server" Text="Label" ForeColor="Red"></asp:Label>
                              <br />
                              Country
                              <asp:Label ID="lblCountry" runat="server" Text="Label" ForeColor="#FF3300">

                              </asp:Label>
                          </asp:Panel>
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Action">
                      <ItemTemplate>
                          <asp:LinkButton ID="LinkButton1" runat="server"
                              CommandName="Select">More Details</asp:LinkButton>
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>
              <HeaderStyle BackColor="#FF99FF" Height="40px" />
             <RowStyle BackColor="#FFCC99" Height="100px" Width="200px" />
          </asp:GridView>
          </td></tr></table>
    </div>
    </form>
</body>
</html>




C# CODING



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;

public partial class GridLinkButton : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;

    public void dbcon()
    {
        string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();

    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            bind1();

        }
    }
    protected void bind1()
    {
        dbcon();
        query = "select * from grid";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();

        }


        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            // GridView1.FooterRow.Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
        }

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

     Panel cccc = (GridView1.SelectedRow.FindControl("Panel1") as Panel);

        cccc.Visible = true;

   Label lblid = (GridView1.SelectedRow.FindControl("lblID") as Label);

   Label lblcity = (GridView1.SelectedRow.FindControl("lblCity") as   Label);

  Label lblcountry = (GridView1.SelectedRow.FindControl("lblCountry")   as  Label);
  

        dbcon();
        {

    query = "select city,country from grid where id='" + lblid.Text + "'";
          
  cmd = new SqlCommand(query, con);
       
     rd = cmd.ExecuteReader();
    
        if (rd.Read())
            {

                lblcity.Text = rd[0].ToString();
                lblcountry.Text = rd[1].ToString();

            }


        }
    }
}




First Add The New WebForm - Select GridView From ToolBox







Next - Create  Table - Add Required Fields 









Next - Right Click - GridView -  Select Edit Columns










Next - Add TemplateFields -  Change Header Text 










Next - Add Template Field - Change Header Text 











Next - Select - Edit Template -  Select  - ID Column  -  Add the Label Change Id  - Bind Field Eval("id")












Next -   Select  - ID Column  -  Add the Panel  - Visible = False  & Add Lables Inside Panel  Change Id
For City & Country Display











Next - City Label  - Change Id











Next - Country Label  - Change Id












Next - Add the LinkButton - Change The Name  & commandName="Select"









Next - Change The GridView  AlternateRowStyle Color









Next - Change The GridView  HeaderStyle & RowStyle Color










Html  Coding 






Next - DoubleClick - SelectedIndexChanged










Next - Add NameSpaces - DataBase Connection - Select Table Fields 










Next -  Panel & labels GridView Selected Row Find Control & Where Condition Id Get Selectd  Values To Display Panel









Next - DataBase Table  Fields Fill 









OUTPUT


Next - Run [F5] Program  - Select LinkButton Related  Id Values  Diaplay After Panel Display.












0 comments:

Post a Comment