Wednesday, December 14, 2011

link button in gridview in

Earlier  we learned how to export Gridview data to word document,gridview to excel,paging and sorting in Gridview.bind data to dropdownlist in gridview in i will shown how to perform the data modifications(edit,delete) using link button in grid view in we have auto generated options to do edit and delete in grid view .But here i am using link button control with custom functionality in grid view .
Grid view having The Template field(asp:template field) which is used to add the any server controls and Bound field is used to bind the particulate field (cityid,city name) from data base to grid view columns from database .The bound fields has property to add the desirable column to it i.e Data Field
<asp:panel cssclass="panel_set " id="Panel2" runat="server">

<br />
<table align="center" tablecen="" valign="center ><tbody>
<tr>  <td><br />
<asp:label id="lblcityid" runat="server" text="CityID:"></asp:label></td> <td><br />
<asp:textbox id="Txtcityid" readonly="True" runat="server"></asp:textbox></td>  </tr>
<tr> <td><br />
<asp:label id="lblcityname" runat="server" text="CityName:"></asp:label></td> <td><br />
<asp:textbox id="txtcityname" runat="server"></asp:textbox></td> </tr>
<tr> <td><br />
<asp:button id="btnsave" onclick="btnsave_Click" runat="server" text="Save">      <asp:button id="btnupdate" onclick="btnupdate_Click" runat="server" text="Update">  </asp:button></asp:button></td> </tr>
</tbody> </table>
<asp:panel cssclass="pnel_grv" id="panelgridviews" runat="server">
<asp:gridview autogeneratecolumns="False" cssclass="grid_scroll " datakeynames="cityid" emptydatatext="There is No Records To Display" id="grvcity" onrowdeleting="DeleteRecord" runat="server">

<asp:boundfield datafield="cityid" headertext="cityid">
<itemstyle horizontalalign="Center" width="20px"></itemstyle>

<asp:boundfield datafield="cityname" headertext="cityname">
<itemstyle horizontalalign="Center" width="20px"></itemstyle>

<headertemplate> </headertemplate>
<asp:linkbutton id="Linkedit" onclick="Linkedit_click" runat="server">Edit</asp:linkbutton>
<asp:templatefield headertext="Delete?">

<asp:linkbutton causesvalidation="false" commandname="Delete" id="lnBD" runat="server" text="Delete"></asp:linkbutton>

<footerstyle backcolor="#99CCCC" forecolor="#003399">
<pagerstyle backcolor="#99CCCC" forecolor="#003399" horizontalalign="Left">
<selectedrowstyle backcolor="#009999" font-bold="True" forecolor="#CCFF99">
<headerstyle backcolor="#003399" font-bold="True" forecolor="#CCCCFF">       

Code behind:
Here one pop up is coming to confirmation to delete the record when we click on the delete link button in grid more thing will have to observe here is how to delete record event is raised.The OnRowDeleting command which has given to grid view properties(can see in aspx page) is used to delete the record in grid view
protected void Linkedit_click(object sender, EventArgs e)
   LinkButton lnkButton = sender as LinkButton;
   GridViewRow row = (GridViewRow)lnkButton.NamingContainer;
   lblgdeptid.Text = row.Cells[0].Text;
   cmd = new SqlCommand("editcity", con);
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add("cityid", SqlDbType.NVarChar).Value = lblgdeptid.Text;
   SqlDataReader dr = cmd.ExecuteReader();
   if (dr.HasRows)
       while (dr.Read())
                Txtcityid.Text = dr["cityid"].ToString();
                txtcityname.Text = dr["cityname"].ToString();
//give an pop with "there is no data"


 protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
     int cityid = Int32.Parse(grvcity.DataKeys[e.RowIndex].Value.ToString());
     CityBAL3 pBAL = new CityBAL3();
     grvcity.EditIndex = -1;

No comments: