Textbox disappers due to post back and showed Error:Object reference not set to an in

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 15
Reputation: pen2satya is an unknown quantity at this point 
Solved Threads: 0
pen2satya pen2satya is offline Offline
Newbie Poster

Textbox disappers due to post back and showed Error:Object reference not set to an in

 
0
  #1
Jun 9th, 2009
I have a Gridview having 3 columns in which i took checkbox,label and label as item templates for columns select,code,name respectively.
ecode column is not editable.Button edit and update are present outside of gridview.

i can edit multiple rows like this...

when i select checkbox in 1st row of gridview then in ename column of that row i added textbox by this code

  1. protected void btnEdit_Click(object sender, EventArgs e)
  2. {
  3.  
  4. for (int i = 0; i < GridView1.Rows.Count; i++)
  5. {
  6. txteditEname = new TextBox();
  7. CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
  8. Label lblEname = (Label)GridView1.Rows[i].Cells[0].FindControl("lblEname");
  9.  
  10. if (chkDelete.Checked)
  11. {
  12. GridView1.Rows[i].Cells[2].Controls.Add(txteditEname);
  13. txteditEname.Text = lblEname.Text;
  14. lblEname.Visible = false;
  15. }
  16.  
  17. }
  18. }
  19.  
  20. protected void btnUpdate_Click(object sender, EventArgs e)
  21. {
  22. ArrayList al = new ArrayList();
  23.  
  24. for (int i = 0; i < GridView1.Rows.Count; i++)
  25. {
  26.  
  27. CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
  28. txteditEname = (TextBox)GridView1.Rows[i].Cells[2].FindControl("txteditEname");
  29. string str=txteditEname.Text;//error

//here i set breakpoint and wanted to retrieve txteditEname but error comes...
then thinking that there is no such textbox when i press Update button as autopost back then
i saved the txteditEname 's value in Viewstate but when i retrieved viewstate it showed me the textbox's value before page is posted back.
But the current txteditEname's value is different which i want to update.Please tell me how to get curret txteditEname's value.
But before that i wonder if this textbox is not available then i'll surely get null value.
Last edited by peter_budo; Jun 9th, 2009 at 10:10 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,600
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 460
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Textbox disappers due to post back and showed Error:Object reference not set to an in

 
0
  #2
Jun 9th, 2009
Please refer the ASP.NET Page life cycle topic at MSDN. ASP.NET page and control objects are created on each post back.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 164
Reputation: dnanetwork has a little shameless behaviour in the past 
Solved Threads: 24
dnanetwork's Avatar
dnanetwork dnanetwork is offline Offline
Junior Poster

Re: Textbox disappers due to post back and showed Error:Object reference not set to a

 
0
  #3
Jun 27th, 2009
your code will never work..

you need to iterate through Gridview rows..

Checking should be done on RowDataBound Event..

  1. foreach(GridviewRows Gvr in Gridview1.rows)
  2. {
  3. Checkbox chk = (CheckBox)e.row.FindControl("Name Of Your Checkbox");
  4.  
  5. if(chk.checked == true)
  6. {
  7. // carry out rest of the operation...
  8. }
  9. }
Last edited by peter_budo; Jun 27th, 2009 at 11:37 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC