HighTechTalks DotNet Forums  

FindControl in a gridview's dynamically-added template

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss FindControl in a gridview's dynamically-added template in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Craig Buchanan
 
Posts: n/a

Default FindControl in a gridview's dynamically-added template - 07-07-2008 , 03:15 PM






I dynamically add data-bound templates to a gridview in my ascx control.
while this works correctly when the gridview is databound to the
datatable, i'm having issues on postback.

i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.

i have a two templates: one that uses a label the other uses a textbox
to display data. when the InstantiateIn fires, i set the Id property
equal to 'Label_' + the name of the databound column, add the control
to the container and add the onDataBinding event handler.

what am i missing?

thanks,

Craig buchanan

Reply With Quote
  #2  
Old   
Peter Bucher [MVP]
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 06:06 PM






Hi Craig

Quote:
i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

Quote:
i have a two templates: one that uses a label the other uses a textbox
to display data. when the InstantiateIn fires, i set the Id property
equal to 'Label_' + the name of the databound column, add the control
to the container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET


Reply With Quote
  #3  
Old   
Peter Bucher [MVP]
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 06:06 PM



Hi Craig

Quote:
i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

Quote:
i have a two templates: one that uses a label the other uses a textbox
to display data. when the InstantiateIn fires, i set the Id property
equal to 'Label_' + the name of the databound column, add the control
to the container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET


Reply With Quote
  #4  
Old   
Peter Bucher [MVP]
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 06:06 PM



Hi Craig

Quote:
i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

Quote:
i have a two templates: one that uses a label the other uses a textbox
to display data. when the InstantiateIn fires, i set the Id property
equal to 'Label_' + the name of the databound column, add the control
to the container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET


Reply With Quote
  #5  
Old   
Craig Buchanan
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 06:27 PM



Peter-

Thanks for the reply.

I bind the gridview in the Page_Load when it is not a postback.

I use a button's click event as a testing harness to examing the contents of
the gridview during a postback. In this event I loop thru the gridview's
rows, and attempt to find my textbox or label using findcontrol.

i dynamically create the templates prior to binding the data and in a
separate method. in the contructor of the template's InstantiateIn method,
i create the control (label or textbox) and assign its ID property to either
Label_[dataMember] or TextBox_[dataMember]. the [dataMember] value is
passed to the template in the constructor. moreover, the [dataMember]
values is used in the OnDataBinding event handler. pseudocode :

Class TextBoxTemplate
Implements ITemplate

private _dataMember as string
Public Sub New()
End Sub
Public Sub New(dataMember as string)
_dataMember=dataMember
End Sub

Public Sub InstantiateIn(container as )
dim tb as new TextBox
tb.Id = string.concat("Textbox_" ,_dataMember)

'add event handler (can't remember coding...)

container.Controls.Add(tb)

End Sub

public sub OnDataBinding(...)
'case sender as textbox
...
'cast sender's container as gridViewRow
...
'bind data (something similar)
tb.text = DataBinder.Eval(_dataMember)

end sub

End Class

i added logic to recreate the dynamic column in the Page_Init event, which
seemed to recreate the gridview correctly. i still can't seem to find my
dynamically-created controls in the gridview, however.

thoughts?

thanks a lot.

Craig

"Peter Bucher [MVP]" <peter.bucher (AT) aspnetzone (DOT) de> wrote

Quote:
Hi Craig

i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

i have a two templates: one that uses a label the other uses a textbox to
display data. when the InstantiateIn fires, i set the Id property equal
to 'Label_' + the name of the databound column, add the control to the
container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET



Reply With Quote
  #6  
Old   
Craig Buchanan
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 06:27 PM



Peter-

Thanks for the reply.

I bind the gridview in the Page_Load when it is not a postback.

I use a button's click event as a testing harness to examing the contents of
the gridview during a postback. In this event I loop thru the gridview's
rows, and attempt to find my textbox or label using findcontrol.

i dynamically create the templates prior to binding the data and in a
separate method. in the contructor of the template's InstantiateIn method,
i create the control (label or textbox) and assign its ID property to either
Label_[dataMember] or TextBox_[dataMember]. the [dataMember] value is
passed to the template in the constructor. moreover, the [dataMember]
values is used in the OnDataBinding event handler. pseudocode :

Class TextBoxTemplate
Implements ITemplate

private _dataMember as string
Public Sub New()
End Sub
Public Sub New(dataMember as string)
_dataMember=dataMember
End Sub

Public Sub InstantiateIn(container as )
dim tb as new TextBox
tb.Id = string.concat("Textbox_" ,_dataMember)

'add event handler (can't remember coding...)

container.Controls.Add(tb)

End Sub

public sub OnDataBinding(...)
'case sender as textbox
...
'cast sender's container as gridViewRow
...
'bind data (something similar)
tb.text = DataBinder.Eval(_dataMember)

end sub

End Class

i added logic to recreate the dynamic column in the Page_Init event, which
seemed to recreate the gridview correctly. i still can't seem to find my
dynamically-created controls in the gridview, however.

thoughts?

thanks a lot.

Craig

"Peter Bucher [MVP]" <peter.bucher (AT) aspnetzone (DOT) de> wrote

Quote:
Hi Craig

i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

i have a two templates: one that uses a label the other uses a textbox to
display data. when the InstantiateIn fires, i set the Id property equal
to 'Label_' + the name of the databound column, add the control to the
container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET



Reply With Quote
  #7  
Old   
Craig Buchanan
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 06:27 PM



Peter-

Thanks for the reply.

I bind the gridview in the Page_Load when it is not a postback.

I use a button's click event as a testing harness to examing the contents of
the gridview during a postback. In this event I loop thru the gridview's
rows, and attempt to find my textbox or label using findcontrol.

i dynamically create the templates prior to binding the data and in a
separate method. in the contructor of the template's InstantiateIn method,
i create the control (label or textbox) and assign its ID property to either
Label_[dataMember] or TextBox_[dataMember]. the [dataMember] value is
passed to the template in the constructor. moreover, the [dataMember]
values is used in the OnDataBinding event handler. pseudocode :

Class TextBoxTemplate
Implements ITemplate

private _dataMember as string
Public Sub New()
End Sub
Public Sub New(dataMember as string)
_dataMember=dataMember
End Sub

Public Sub InstantiateIn(container as )
dim tb as new TextBox
tb.Id = string.concat("Textbox_" ,_dataMember)

'add event handler (can't remember coding...)

container.Controls.Add(tb)

End Sub

public sub OnDataBinding(...)
'case sender as textbox
...
'cast sender's container as gridViewRow
...
'bind data (something similar)
tb.text = DataBinder.Eval(_dataMember)

end sub

End Class

i added logic to recreate the dynamic column in the Page_Init event, which
seemed to recreate the gridview correctly. i still can't seem to find my
dynamically-created controls in the gridview, however.

thoughts?

thanks a lot.

Craig

"Peter Bucher [MVP]" <peter.bucher (AT) aspnetzone (DOT) de> wrote

Quote:
Hi Craig

i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

i have a two templates: one that uses a label the other uses a textbox to
display data. when the InstantiateIn fires, i set the Id property equal
to 'Label_' + the name of the databound column, add the control to the
container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET



Reply With Quote
  #8  
Old   
bruce barker
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 11:56 PM



as your databind creates the dynamic controls, you need to rebind in the
postback before the onload event. the best place to create dynaic
control is in CreateChildControls method (which was designed for this).

-- bruce (sqlwork.com)

Craig Buchanan wrote:
Quote:
Peter-

Thanks for the reply.

I bind the gridview in the Page_Load when it is not a postback.

I use a button's click event as a testing harness to examing the contents of
the gridview during a postback. In this event I loop thru the gridview's
rows, and attempt to find my textbox or label using findcontrol.

i dynamically create the templates prior to binding the data and in a
separate method. in the contructor of the template's InstantiateIn method,
i create the control (label or textbox) and assign its ID property to either
Label_[dataMember] or TextBox_[dataMember]. the [dataMember] value is
passed to the template in the constructor. moreover, the [dataMember]
values is used in the OnDataBinding event handler. pseudocode :

Class TextBoxTemplate
Implements ITemplate

private _dataMember as string
Public Sub New()
End Sub
Public Sub New(dataMember as string)
_dataMember=dataMember
End Sub

Public Sub InstantiateIn(container as )
dim tb as new TextBox
tb.Id = string.concat("Textbox_" ,_dataMember)

'add event handler (can't remember coding...)

container.Controls.Add(tb)

End Sub

public sub OnDataBinding(...)
'case sender as textbox
...
'cast sender's container as gridViewRow
...
'bind data (something similar)
tb.text = DataBinder.Eval(_dataMember)

end sub

End Class

i added logic to recreate the dynamic column in the Page_Init event, which
seemed to recreate the gridview correctly. i still can't seem to find my
dynamically-created controls in the gridview, however.

thoughts?

thanks a lot.

Craig

"Peter Bucher [MVP]" <peter.bucher (AT) aspnetzone (DOT) de> wrote in message
news:EAC8BCBB-1049-436A-A422-632D826817F3 (AT) microsoft (DOT) com...
Hi Craig

i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

i have a two templates: one that uses a label the other uses a textbox to
display data. when the InstantiateIn fires, i set the Id property equal
to 'Label_' + the name of the databound column, add the control to the
container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET



Reply With Quote
  #9  
Old   
bruce barker
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 11:56 PM



as your databind creates the dynamic controls, you need to rebind in the
postback before the onload event. the best place to create dynaic
control is in CreateChildControls method (which was designed for this).

-- bruce (sqlwork.com)

Craig Buchanan wrote:
Quote:
Peter-

Thanks for the reply.

I bind the gridview in the Page_Load when it is not a postback.

I use a button's click event as a testing harness to examing the contents of
the gridview during a postback. In this event I loop thru the gridview's
rows, and attempt to find my textbox or label using findcontrol.

i dynamically create the templates prior to binding the data and in a
separate method. in the contructor of the template's InstantiateIn method,
i create the control (label or textbox) and assign its ID property to either
Label_[dataMember] or TextBox_[dataMember]. the [dataMember] value is
passed to the template in the constructor. moreover, the [dataMember]
values is used in the OnDataBinding event handler. pseudocode :

Class TextBoxTemplate
Implements ITemplate

private _dataMember as string
Public Sub New()
End Sub
Public Sub New(dataMember as string)
_dataMember=dataMember
End Sub

Public Sub InstantiateIn(container as )
dim tb as new TextBox
tb.Id = string.concat("Textbox_" ,_dataMember)

'add event handler (can't remember coding...)

container.Controls.Add(tb)

End Sub

public sub OnDataBinding(...)
'case sender as textbox
...
'cast sender's container as gridViewRow
...
'bind data (something similar)
tb.text = DataBinder.Eval(_dataMember)

end sub

End Class

i added logic to recreate the dynamic column in the Page_Init event, which
seemed to recreate the gridview correctly. i still can't seem to find my
dynamically-created controls in the gridview, however.

thoughts?

thanks a lot.

Craig

"Peter Bucher [MVP]" <peter.bucher (AT) aspnetzone (DOT) de> wrote in message
news:EAC8BCBB-1049-436A-A422-632D826817F3 (AT) microsoft (DOT) com...
Hi Craig

i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

i have a two templates: one that uses a label the other uses a textbox to
display data. when the InstantiateIn fires, i set the Id property equal
to 'Label_' + the name of the databound column, add the control to the
container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET



Reply With Quote
  #10  
Old   
bruce barker
 
Posts: n/a

Default Re: FindControl in a gridview's dynamically-added template - 07-07-2008 , 11:56 PM



as your databind creates the dynamic controls, you need to rebind in the
postback before the onload event. the best place to create dynaic
control is in CreateChildControls method (which was designed for this).

-- bruce (sqlwork.com)

Craig Buchanan wrote:
Quote:
Peter-

Thanks for the reply.

I bind the gridview in the Page_Load when it is not a postback.

I use a button's click event as a testing harness to examing the contents of
the gridview during a postback. In this event I loop thru the gridview's
rows, and attempt to find my textbox or label using findcontrol.

i dynamically create the templates prior to binding the data and in a
separate method. in the contructor of the template's InstantiateIn method,
i create the control (label or textbox) and assign its ID property to either
Label_[dataMember] or TextBox_[dataMember]. the [dataMember] value is
passed to the template in the constructor. moreover, the [dataMember]
values is used in the OnDataBinding event handler. pseudocode :

Class TextBoxTemplate
Implements ITemplate

private _dataMember as string
Public Sub New()
End Sub
Public Sub New(dataMember as string)
_dataMember=dataMember
End Sub

Public Sub InstantiateIn(container as )
dim tb as new TextBox
tb.Id = string.concat("Textbox_" ,_dataMember)

'add event handler (can't remember coding...)

container.Controls.Add(tb)

End Sub

public sub OnDataBinding(...)
'case sender as textbox
...
'cast sender's container as gridViewRow
...
'bind data (something similar)
tb.text = DataBinder.Eval(_dataMember)

end sub

End Class

i added logic to recreate the dynamic column in the Page_Init event, which
seemed to recreate the gridview correctly. i still can't seem to find my
dynamically-created controls in the gridview, however.

thoughts?

thanks a lot.

Craig

"Peter Bucher [MVP]" <peter.bucher (AT) aspnetzone (DOT) de> wrote in message
news:EAC8BCBB-1049-436A-A422-632D826817F3 (AT) microsoft (DOT) com...
Hi Craig

i would like to iterate thru the gridview's rows, examine the databound
controls, then perform a database action. for some reason, i can't find
the controls.
Code

i have a two templates: one that uses a label the other uses a textbox to
display data. when the InstantiateIn fires, i set the Id property equal
to 'Label_' + the name of the databound column, add the control to the
container and add the onDataBinding event handler.
Dito, show relevant Code to this issue.

How are you binding? On every Page_Load or with the IsPostBack-Constrain?
Where are you trying to access via .FindControl and how?
How are you put your dynamically names together "Label_"....?

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.