473,320 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

FindControl in a gridview's dynamically-added template

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
Jul 7 '08 #1
4 10969
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
Jul 7 '08 #2
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]" <pe**********@aspnetzone.dewrote in message
news:EA**********************************@microsof t.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

Jul 7 '08 #3
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:
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]" <pe**********@aspnetzone.dewrote in message
news:EA**********************************@microsof t.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

Jul 8 '08 #4
bruce-

i'm trying to pivot data in code, while two columns are available at
design time, the other column (based on a date column) are dynamically
generated.

my approach has been such:

when Not Ispostback

1). pivot the detailed datatable into a dynamically-created pivoted
datatable. for example, i am given:
ET FS DT $
-- -- -- -
A A 2008 50
A B 2008 200
A B 2009 100

this is pivoted to:

ET FS 2008 2009
-- -- ---- ----
A A 50
A B 200 100

2). add dynamically-created template columns (4 in this example) to the
datagrid based on the columns in the pivoted datatable. these template
columns are stored in the viewstate.

3). bind gridview to pivoted datatable

during a postback

1). retrieve the template columns from the viewstate
2). add them to the gridview
3). allow gridview to restore data

from what i've read, i need to add the template columns to the gridview
in the Page_Init event, but the template columns, contained in the
viewstate, aren't available until the Page_Load event.

is there another way to handle this?

thanks.

bruce barker wrote:
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:
>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]" <pe**********@aspnetzone.dewrote in message
news:EA**********************************@microso ft.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

Jul 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil | last post by:
asp.net 2.0 i have this scenario: <loginview> <loggedintemplate> <formview_1> <gridview> <formview_2>
3
by: Martin | last post by:
Hi, I have a very frustrating problem that I have researched for countless hours to no avail. There are many posts asking very similar things, however none usefull in my situation. I am using VS...
0
by: K B | last post by:
PLEASE be patient and read this. I believe I've actually reached the end of the Web trying to find the answer! I have an aspx using a MasterPage with a ContentPlaceholder. I have a datatable...
4
by: bryan | last post by:
Hi all, I am working on a .net 2.0 web app. On page load I display a gridview with a drop down list (Employees), when the user selects from the drop down list, the grid view is filled with...
6
by: MRW | last post by:
Hello! I have a problem, I can't seem to solve. I'm making a class in my page that will hold the members of a FormView, so I can access them freely throughout the several functions and...
11
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I am trying to get the text of an item in a GridView, but am doing something wrong. Can someone help me with the correct C# statement I need? Below is my GridView and my attempt to get the control....
5
by: daniel.hedz | last post by:
I am generating a usercontrol dynamically successfully, but when I try to find that usercontrol I get a type mismatch. This is what I am doing: //Loading my usercontrol...
1
by: Andrew Robinson | last post by:
I am using the following code to preload a few controls in the FooterRow within a GridView control: protected void Page_Load(object sender, EventArgs e) {...
1
by: HockeyFan | last post by:
I have the following string from one of the controls, inside gridview, inside a control, inside a repeater item. rptBorrower_ctl00_ctlAuthorizedSigners_gvAuthorizedSigners_ctl04_ The control was...
1
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
How can I use Findcontrol inside a master gridview's RowDatabound event to search for another gridview (detail) control that is inside an ASP Panel, in a template column of the master gridview?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.