Category: Asp.Net

What is viewstate in ASP.NET?

January 16, 2010 | Filed Under Asp.Net | Leave a Comment

Viewstate object is used to persist data of variables across postbacks. It even existed in classic ASP. In ASP.NET, a variable’s value is assigned to a viewstate object and then this is passed as a hidden variable and then may be retrieved by a page after a postback. See the example below…
//Save the value in ViewState object before the PostBack
ViewState(”SomeVar”) = txtFirstName.text;

//Retrieve the value from ViewState object after the PostBack
String strFirstName = ViewState(”SomeVar”).ToString();
Note that Viewstate object’s value is accessible only at page level. This means that if a viewstate is created at page1.aspx, then it may be used only within page1.aspx after the postback, and cannot be used by any other page
Article written by urooj

How to Update,Edit and delete a row in GridView?

January 15, 2010 | Filed Under Asp.Net | 2 Comments

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
filldata();
}
}
public void filldata()
{
string str = “select * from login”;
da = new SqlDataAdapter(str,con);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string uname=GridView1.Rows[0].Cells[2].Text;
string qyry = “delete from login where uname=’”+uname+”‘”;
da = new SqlDataAdapter(qyry,con);
ds = new DataSet();
da.Fill(ds);
filldata();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
filldata();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtunam = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
TextBox txtpassword = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
TextBox txtconfirmpwd = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
TextBox txtemail = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];
//string qry = “update login set password=’”+txtpassword.Text+”‘,confirm_password=’”+txtconfirmpwd.Text+”‘,email=’”+txtemail.Text+”‘where uname=’”+txtunam.Text+”‘”;
string qryy = “update login set password=’”+txtpassword.Text+”‘,confirm_password=’”+txtconfirmpwd.Text+”‘,email=’”+txtemail.Text+”‘where uname=’”+txtunam.Text+”‘”;
da = new SqlDataAdapter(qryy, con);
ds = new DataSet();
da.Fill(ds);
GridView1.EditIndex = -1;
filldata();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
filldata();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
filldata();
}
}

———————————————————————————————————-

In .aspx side you need to add a GridView data control.
for update/delete you have to define the datakey in gridview at aspx side:

———————————————————————————————————–

<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”GridView1″ runat=”server” AllowPaging=”True” OnPageIndexChanging=”GridView1_PageIndexChanging”
OnRowCancelingEdit=”GridView1_RowCancelingEdit” OnRowDeleting=”GridView1_RowDeleting”
OnRowEditing=”GridView1_RowEditing” OnRowUpdating=”GridView1_RowUpdating” OnSelectedIndexChanged=”GridView1_SelectedIndexChanged”
PageSize=”3″ Style=”z-index: 100; left: 0px; position: absolute; top: 0px”>
<Columns>
<asp:CommandField ShowEditButton=”True” />
<asp:CommandField ShowDeleteButton=”True” />
</Columns>
</asp:GridView>

</div>
</form>

Article written by urooj

© PHPInterviewQuestion.com 2009 - 2010

eXTReMe Tracker