Tuesday, September 11, 2007

Finding/Manipulating a child control of a FormView in C#

It's distressing that it took me over a week to get this working.

The problem: In a C# web application, I want to set the visible property of a FormView's edit button based on value of a bound data field (actually I used eval for the label I would use to hold the value).

Here's the short answer: Finding ASP.NET Child Controls....the Simple Way
The key for me was not only in the post itself, but also in an answer to a comment that suggested using the Trace functionality to discern the control tree. Once I found the control I did my evaluation and manipulation in the Page_Load.

  protected void Page_Load(object sender, EventArgs e) {
    Label EA = this.FindControl("ctl00$ContentPlaceHolder$FormView$EntryAge") as Label;
    LinkButton EB = this.FindControl("ctl00$ContentPlaceHolder$FormView$EditButton") as LinkButton;
    if (EA != null && EB != null) {
      if (Convert.ToInt32(EA.Text) < visible =" true; } else { EB.Visible = false; }
    }
  }



I use Firefox and keep a tab trail so that my searches are cumulative and I can maybe shave some time off a similar issue later. This round of R&D started with this search
2.0 DataBinding "ASP NET " -"visual basic".
(It is a stubborn, personal resentment towards the reimagining of the Visual Basic language that has driven my focus on C#.)

Working with Data in ASP.NET 2.0

Accessing and Updating Data in ASP.NET 2.0: Accessing Database Data

Accessing and Updating Data in ASP.NET 2.0: Programmatically Accessing Data using the Data Source Controls

Programmatically access data source control C#

DevASP.Net Data Source Controls in ASP.NET 2.0 Article, Samples ...

Customizing DataBinded Output in Templates

Custom html Databind formview eval C# if +"eval("

Adding and Responding to Buttons to a GridView

C# formview control manipulation +advanced

formview "(eval(" visible lang:c# (Google Code Search)
Also did a Krugle code search, but the url doesn't change :/

http://forums.asp.net/p/985573/1269322.aspx (Google Code Search Result)
c# toggle form view item template visible (Microsoft Live Search - A clear sign of desperation...)

Finding controls inside ItemTemplates inside a FormView inside a LoginView
Getting Closer...

Google Code Search Refresher
I REALLY need to brush up on my regex

ASP.NET ItemTemplates, EVAL() and embedding dynamic values into controls

Finding ASP.NET Child Controls....the Simple Way GOLD!!!

This entire process strikes me as unnecessarily convoluted. It doesn't get much more basic than the evaluation and manipulation of an object in your application, but this exercise was anything but. By this point in previous Microsoft IDE's I'd given up on they're design time controls and done everything by hand. What's different now? I have less time and higher expectations. Sucks, but it's the reality. However, my goal in this post is not to bitch about over-architected and still lacking platforms, but to hopefully save someone the time I spent hunting down what should have been a simple solution.

Labels: , ,

0 Comments:

Post a Comment

<< Home