R ) Detailview
* Detailview *
- Detailview is reprensting all data in Detail Format.
- Detailview Bind Process same as Gridview.
* Gridview Simple Bind Data Video *
* Click Grid Button and show data in Detailview and repeater*
* DeatilsView FindControl And Update *
Source Code :-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="S_DetailViewUpdate.aspx.cs" Inherits="ZZrough" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" OnItemCommand="rep_show" Height="273px" Width="474px">
<Fields>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("id") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("city") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit1_click"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</form>
</body>
</html>
Page 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.SqlClient;
using System.Data;
using System.Configuration;
public partial class ZZrough : System.Web.UI.Page
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
show();
}
}
protected void show()
{
string path = ConfigurationManager.AppSettings["db1"];
cn = new SqlConnection(path);
cn.Open();
string k = "select * from emp";
cm = new SqlCommand(k, cn);
dr = cm.ExecuteReader();
DetailsView1.DataSource = dr;
DetailsView1.DataBind();
dr.Close(); ;
}
protected void rep_show(object sender, DetailsViewCommandEventArgs e)
{
string path = ConfigurationManager.AppSettings["db1"];
cn = new SqlConnection(path);
cn.Open();
if (e.CommandName == "Edit1_click") // source button commandname and e is object ( e.CommandName == "show1" )
{
TextBox txtid = (TextBox)((DetailsView)sender).FindControl("TextBox1");
TextBox txtname = (TextBox)((DetailsView)sender).FindControl("TextBox2");
TextBox txtcity = (TextBox)((DetailsView)sender).FindControl("TextBox3");
string k2 = "update emp set name=@name1 where id=@id1";
cm = new SqlCommand(k2, cn);
cm.Parameters.AddWithValue("name1", txtname.Text);
cm.Parameters.AddWithValue("id1", txtid.Text);
cm.ExecuteNonQuery();
Response.Write("record updated succesfully");
}
else
{
Response.Write("record not updated ");
}
}
}
Comments
Post a Comment