Introduction:
In this article I will explain how to send gridview row values to other page and after update that record values return back to the gridview page using asp.net.
Description:
I have a gridview with hyperlink fields here my requirement is if I click on hyperlink of particular row I need to display that particular row values into other page in that page user will edit record values after that if he clicks on update button I need to update that record values and get back to previous home page i.e., gridview page. To achieve this first design the table in database like this
In this article I will explain how to send gridview row values to other page and after update that record values return back to the gridview page using asp.net.
Description:
I have a gridview with hyperlink fields here my requirement is if I click on hyperlink of particular row I need to display that particular row values into other page in that page user will edit record values after that if he clicks on update button I need to update that record values and get back to previous home page i.e., gridview page. To achieve this first design the table in database like this
ColumnName
|
DataType
|
UserId
|
Int(set
identity property=true)
|
UserName
|
varchar(50)
|
FirstName
|
varchar(50)
|
LastName
|
varchar(50)
|
Email
|
Varchar(50)
|
After
completion table creation add some dummy data after that creat new website
design the Default.aspx page like this
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
runat="server">
<title>PassGridviewRow
values </title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<asp:GridView
runat="server"
ID="gvrecords"
AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White"
DataKeyNames="UserId">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a
href ='<%#"UpdateGridviewvalues.aspx?UserId="+DataBinder.Eval(Container.DataItem,"UserId")
%>'>
<%#Eval("UserName") %> </a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
After
that add these namcespace using System.Data and
using System.Data.SqlClient in your codebehind
and write the following code
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridview();
}
}
protected
void BindGridview()
{
SqlConnection con = new SqlConnection("Data
Source=GohilRajni;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Close();
DataSet
ds = new DataSet();
da.Fill(ds);
gvrecords.DataSource
= ds;
gvrecords.DataBind();
}
|
Our
gridview page is ready if you run this page you will find gridview with
hyperlink fields if we click on hyperlink field we need to show that particular
record values in another page for that add new page to your website and give
name it as UpdateGridviewvalues.aspx because I gave this in hyperlink
filed that’s why I am saying that give the same name if you give different name
for new form change the form name in hyperlink field also here is the link.
<a
href ='<%#"UpdateGridviewvalues.aspx?UserId="+DataBinder.Eval(Container.DataItem,"UserId")
%>'>
<%#Eval("UserName") %> </a>
|
Now
design UpdateGridviewvalues.aspx like this
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
runat="server">
<title>Update
Gridview Row Values</title>
<script
type="text/javascript">
function
Showalert(username) {
alert(username
+ ' details updated successfully.');
if
(alert) {
window.location
= 'Default.aspx';
}
}
</script>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<table>
<tr>
<td
colspan="2"
align="center">
<b>
Edit User Details</b>
</td>
</tr>
<tr>
<td>
User
Name:
</td>
<td>
<asp:Label
ID="lblUsername"
runat="server"/>
</td>
</tr>
<tr>
<td>
First
Name:
</td>
<td>
<asp:TextBox
ID="txtfname"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last
Name:
</td>
<td>
<asp:TextBox
ID="txtlname"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox
ID="txtemail"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button
ID="btnUpdate"
runat="server"
Text="Update"
onclick="btnUpdate_Click"
/>
<asp:Button
ID="btnCancel"
runat="server"
Text="Cancel"
onclick="btnCancel_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
After
that add these namcespace using System.Data and
using System.Data.SqlClient in your codebehind
and write the following code
SqlConnection con = new SqlConnection("Data
Source=GohilRajni;Integrated Security=true;Initial Catalog=MySampleDB");
private
int userid=0;
protected
void Page_Load(object
sender, EventArgs e)
{
userid
= Convert.ToInt32(Request.QueryString["UserId"].ToString());
if(!IsPostBack)
{
BindControlvalues();
}
}
private
void BindControlvalues()
{
con.Open();
SqlCommand cmd = new SqlCommand("select
* from UserDetails where UserId=" + userid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Close();
DataSet
ds = new DataSet();
da.Fill(ds);
lblUsername.Text
= ds.Tables[0].Rows[0][1].ToString();
txtfname.Text
= ds.Tables[0].Rows[0][2].ToString();
txtlname.Text
= ds.Tables[0].Rows[0][3].ToString();
txtemail.Text
= ds.Tables[0].Rows[0][4].ToString();
}
protected
void btnUpdate_Click(object
sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("update
UserDetails set FirstName='" + txtfname.Text + "',LastName='" + txtlname.Text + "',Email='" + txtemail.Text + "' where UserId=" + userid, con);
int
result= cmd.ExecuteNonQuery();
con.Close();
if(result==1)
{
ScriptManager.RegisterStartupScript(this,
this.GetType(), "ShowSuccess",
"javascript:Showalert('"+lblUsername.Text+"')", true);
}
}
protected
void btnCancel_Click(object
sender, EventArgs e)
{
Response.Redirect("~/Default.aspx");
}
|
0 comments :
Post a Comment