Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 27th, 2009, 06:17 AM
Authorized User
 
Join Date: Jun 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default FindControl method get nothing in Gridview with Master Page

Hi,everybody,
I just encountered a hard problem.First,I create a master page,and add some controls in the master page.Then I created another aspx page using the master page created before.
So,I add a gridview in this aspx page in the ContentPlaceHolder,like before,I did some basic actions,add,update,select etc.The problem is when I edit a row in the gridview,then update it,the cell's content became noting in it.
I try to set the breakpoint in the GridView1_RowUpdating event,and the I followed the sentence,but I got nothing in the cell,code following:
Code:
 using (NorthwindEntities ctx = new NorthwindEntities())
        {
            var categoryid = (int)this.GridView1.DataKeys[e.RowIndex].Value;
            var category = ctx.Categories.First(p => p.CategoryID == categoryid);
            DropDownList ddlUpdateCategory = this.GridView1.Rows[e.RowIndex].Cells[2].FindControl("ddlUpdateCategory") as DropDownList;
            if (ddlUpdateCategory.SelectedItem.Text.Trim() != category.CategoryName)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "alert('Already Exists!');", true);
                return;
            }
            category.CategoryName = ddlUpdateCategory.SelectedItem.Text.Trim();
            category.Description = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("txtDes")).Text.Trim();
            ctx.SaveChanges();
        }
        GridView1.EditIndex = -1;
        BindGridView();
What an odd thing! You konw, when I did the same thing in another newly-created aspx page without master page, it worked fine.

I just wonder if it had something with the master page.If I use the master page, the findcontrol() method just conuln't find the textbox control in the EditItemTemplate.

Who can help me? The problem has been bothering me for a couple of hours.
Thanks in advance!
  #2 (permalink)  
Old August 27th, 2009, 09:17 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Code:
category.Description = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("txtDes")).Text.Trim();
It looks to me, with the code above, you are looking for the TexBox in the row, which won't work. You first need to locate the appropriate cell in the row, then look for the TextBox in the cell.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #3 (permalink)  
Old August 27th, 2009, 08:31 PM
Authorized User
 
Join Date: Jun 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I indeed find the right row and right cell
Code:
TextBox txtDescription = this.GridView1.Rows[e.RowIndex].Cells[3].FindControl("txtDes") as TextBox;
it works fine in the aspx without using masterpage,but it didn't work in the aspx page with masterpage.
  #4 (permalink)  
Old August 27th, 2009, 09:08 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by Michael YoeH View Post
I indeed find the right row and right cell
Code:
TextBox txtDescription = this.GridView1.Rows[e.RowIndex].Cells[3].FindControl("txtDes") as TextBox;
it works fine in the aspx without using masterpage,but it didn't work in the aspx page with masterpage.
This is NOT the code you pasted in your original post. Go back and check.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #5 (permalink)  
Old August 28th, 2009, 03:42 AM
Authorized User
 
Join Date: Jun 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I know,but both the two segment code didn't work well.
Thanks for your reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
My GridView does not display in the content area of my master page? dotnetDeveloper ASP.NET 3.5 Basics 1 March 14th, 2009 02:42 PM
Master Page Method: Code-Behind for Content Page kwilliams ASP.NET 2.0 Professional 3 June 2nd, 2008 12:57 PM
VB: GridView & DetailsView in Master-Child Rich57 ASP.NET 2.0 Basics 1 May 29th, 2008 07:14 AM
Master/Detail Data Presentation in GridView AbrarNazeer ASP.NET 2.0 Professional 0 June 22nd, 2006 12:59 AM
FindControl Method problem jbenson001 ASP.NET 1.x and 2.0 Application Design 3 March 8th, 2004 10:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.