473,722 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11008
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=dat aMember
End Sub

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

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

container.Contr ols.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**********@a spnetzone.dewro te in message
news:EA******** *************** ***********@mic rosoft.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 CreateChildCont rols 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=dat aMember
End Sub

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

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

container.Contr ols.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**********@a spnetzone.dewro te in message
news:EA******** *************** ***********@mic rosoft.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 CreateChildCont rols 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=dat aMember
End Sub

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

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

container.Contr ols.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**********@a spnetzone.dewro te in message
news:EA******* *************** ************@mi crosoft.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
2223
by: Phil | last post by:
asp.net 2.0 i have this scenario: <loginview> <loggedintemplate> <formview_1> <gridview> <formview_2>
3
19340
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 2005 (ASP 2.0) and VB .NET. I have a page with a datalist, a gridview and then other data controls, nested in that order. This all works great, and produces a nice looking page, but I cannot for the life of me figure out how to reference
0
2380
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 that I use to dynamically populate a gridview - I have to do this in order to specify which control type to put in one of the columns. All this works fine. I've run Trace and can clearly see that the control.id I assigned (lblQuestionID) is there...
4
15362
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 the dates the employees are working. I then want to push a button that adds a new column to the gridview, a new Employees drop down list appears, and again the user selects the
6
1709
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 subroutines, without having to redefine them every function/subroutine. I ran into a problem when trying to define a GridView in that FormView and I was hoping somebody can help me on this, since I'm going a tad
11
1892
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. Thank you. string option = ((TextBox)dgDropDownMenus.Items.FindControl("txtName")).Text; -----------------------DataGrid------------------------------------------- <asp:datagrid id="dgMenus" style="Z-INDEX: 101; LEFT: 0px; POSITION:
5
1814
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 MyWebApp.Folder.Folder.MyUsercontrol myUC = (MyWebApp.Folder.Folder.MyUsercontrol) LoadControl("~/Folder/Folder/MyUsercontrol.ascx");
1
7970
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) { ((TextBox)GriwViewRates.FooterRow.FindControl("TextBoxRateInsert")).Text = "60"; } The successfully loads the Footer / TextBox on page load but fails to load the Footer / TextBox when the GridView is Paged. The line still executes
1
1558
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 a button, but I stripped off the name of that button and would like to FindControl a textbox that I know is on the same line of that grid as the button, so it's prefix will be the same. Do I have to go FindControl the repeater, and then the...
1
2016
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
8740
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9158
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8059
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3208
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2606
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2148
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.