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)
|
<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>
|
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=SureshDasari;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();
}
|
<a href ='<%#"UpdateGridviewvalues.aspx?UserId="+DataBinder.Eval(Container.DataItem,"UserId") %>'> <%#Eval("UserName") %> </a>
|
<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>
|
SqlConnection con = new SqlConnection("Data Source=SureshDasari;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");
}
|
|
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|


Subscribe by RSS
Subscribe by Email
17 comments :
While running sample program nothing is displayed.
I tried to debug in the default page all lines executed but dataset is having null value .SO NOTHING SHOWN IN RUN TIME
Please help fast .I am new in Asp.
I have made proper changes in con statement of sql.
@Amit Karmase......
If your getting empty values means your table doesn not contain any data. Please check it once.......
Thank u ...
i want to try this all this code in ms -ACESS.pl suggest me what changes to be made.
sir i want same like this but using checkbox inside the gridview. how to do?
Please teach me sir...
Thank you..
thanks sir...it is working like anything...thank you very much...
Thank you sir, ..............thank you very much..
i am new in asp.net.....i used it.
I have one requirement like this
I have a table CUSTOMER in that CUSTID,NAME,URL columns is thier
CUSTID NAME URL
1 asp www.asp.net
2 weblog www.weblogs.asp.net
3 google www.google.com
My question is I opening perticular url based on CUSTID
I am Passing parameter Like www.Example.com?CUSTID=1 That
corressponding URL data(Ex:www.asp.net) It will open directly in browser
using asp.net........
plz help me ...........
Thank you,
anil
Hi Suresh,
I need one help from you.I'm fresher to asp.net.
I want to hyperlink only 3 rows out of 10 rows in gridview.and that hyperlink should connect to another page .please help me .
Hai Suresh, my nickname is colol, what about image, for example either the image path or image bytes? So that user can still upload the same images if no change done to the fileupload textbox.
I use above code.It displays first row in the table. If i click second row also. Can you help me sir... But it updates the data in db.
hi ur article is too helpful for me. but i have a question. i have dropdownlist and radio button on edit page.how to fetch the value of dropdownlist and radiobutton from database.
i have problem in databound field,my data is not binding in grid view except tat anchor link? help me
hi,
how to highlight the updated record when i set allowpaging=true in gridview in this example.
Hi Suresh,
Ca n u give example of same gridview column hyperlink if autogenerated column = true.
i want to do this as a object oriented (oop)way. like i write to write database connection code to like GatewayDB file. how