This is a common problem I have faced during my work. Every events are properly handled you will not get the new value in the update time. This is ridiculous thing and often your head can be cracked. But the solution to this problem is quite simple. Lets look at the example that I did....
Suppose this is the code for Update:
protected void ImgGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
/*Image info to be updated*/
try {
/*Extracting different elements from Gridview*/
GridViewRow myRow = ImgGridView.Rows[e.RowIndex];
/*getting pic id*/
Label PicIDLabel = myRow.Cells[2].FindControl("PicIDLabel") as Label;
/*getting image title*/
TextBox titleTextBox = myRow.Cells[1].FindControl("ImgTitleTextBox") as TextBox;
/*updating ad image*/
AdImage adImage = new AdImage();
adImage.ConnectionString = ConfigurationManager.ConnectionStrings["BIZNESConnectionString"].ConnectionString;
if(adImage.LoadByPrimaryKey(short.Parse(PicIDLabel.Text)))
{
adImage.ImgTitle=titleTextBox.Text;
adImage.Save();
}
ImgGridView.EditIndex = -1;
showAdImageInfo();
StatusLabel.ForeColor=Color.Green;
StatusLabel.Text="Image information updated successfully!";
StatusImage.ImageUrl = "adimages/ok.gif";
}
catch (Exception exc)
{
StatusLabel.Text = exc.Message;
}
}
So found no problem in update code every time I debug the code ....I spend the whole day on it and at last I found a forum that probably I have to check for autopostback option of the page.
So my Page_Load was like this:
protected void Page_Load(object sender, EventArgs e)
{
string access = (string)Session["access"];
/*checking access*/
if (access != "yes")
Response.Redirect("SellerAdminLogin.aspx"
/*showing images*/
showAdImageInfo();
}
For that killing problem to resolve we have to check the postback option. Lets look at the editing function:
protected void ImgGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
/*Image editing code*/
ImgGridView.EditIndex = e.NewEditIndex;
showAdImageInfo();
}
If autopost-back is not checked every time gridview will loaded with values from databse so when update command will be clicked then data will be loaded from database to the textbox and new value in the textbox will be lost. So we need to check the auto-postback option:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsAutoPostBack)
{
string access = (string)Session["access"];
/*checking access*/
if (access != "yes")
Response.Redirect("SellerAdminLogin.aspx"
/*showing images*/
showAdImageInfo();
}
}
Thus update will easily work and we will be able to update the gridview data
thanks.
Masud
No comments:
Post a Comment