Solved

Finding controls inside a details view

Posted on 2008-10-24
5
363 Views
Last Modified: 2013-11-26
I am building a website using ASP.NET 2.0 and Visual Studio 2005.

I have a page (called default.aspx) which has a SqlDatasouce in it, a FormView, and a DetailsView inside of the FormView.

Inside the detailsview, there are two columns (both are templateitems). One contains a webusercontrol (called "mywebusercontrol") and one is a textbox (called "textbox1")

The webusercontrol needs to know about the textbox, because it uses the value of the textbox to do other processing. So I made a property in the webusercontrol called "TargetTextBox", of type "Textbox". Now I need to initialize that  property when the page loads.

But in the Page_Load even for my default.aspx page, I can't see any of these controls I created. The intellisense doesn't show the WebUserControl or the Textbox. The only thing I can see is the FormView, but I can't see anything inside of it.

How do I initialize this TargetTextBox property of the WebUserControl1 object to be equal to a reference to the TextBox1 object?
0
Comment
Question by:Frosty555
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 22797033
U'll have to use the findcontrol method

WebUserControl ctrl = (WebUserControl)dtlView1.FindControl(WebUserControl1);

TextBox txt1 = (TextBox)dtlView1.FindControl(TextBox1);

ctrl.TargetTextBox = txt1.ID;
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22798215
addition to informaniac:

that's right you have to use FindControl, but you have to do it within the event ItemDataBound of this FormView

private void ..ItemDataBound(object sender,
        System.Web.UI.WebControls.FormViewItemEventArgs e)
{
//inteligense willhelp you with this method, I dont have my code right now, you might wanna check the syntax...
    if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {

     TextBox txt1  = (TextBox)e.Item.FindControl("TextBox1");
    ctrl.TargetTextBox = txt1.ID;
...
Hope this help

JINN

0
 
LVL 31

Author Comment

by:Frosty555
ID: 22801490
jinn_hnnl:
      The details view and formview do not have an "ItemDataBound" event.

informaniac:
      The variables get set to "Nothing" when I call the FindControl method, no matter what I do.

I have tried the suggested code in the Page_Load event, Page_PreRender event and FormView1_DataBound event, to no avail.

// note: the webusercontrol is called Field_TaxDropdown
// The textbox is called txtExpense
// The property in the webusercontrol I'm trying to set is called "SourceDataTextBox"
 
        Dim ctrl As Field_TaxDropdown = CType(FormView1.FindControl("Field_TaxDropdownGst"), Field_TaxDropdown)
        Dim txt1 As TextBox = CType(FormView1.FindControl("txtExpense"), TextBox)
        ctrl.SourceDataTextBox = txt1

Open in new window

0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22871032
I am back to office now ^^

SOrry I was confusing myself with the ListVIew and didn't realize that you use FormView, have you fixed your problem, or do you need further assistance, I think the suggesion of informaniac is right.

Make sure you called it after the FormView has been bounded.  try ItemCreated event now?

JINN
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 0 total points
ID: 22928402
No, I never ended up figuring it out. I've just wrapped the entire set of controls into yet another webusercontrol. From in there I can get at any of the controls I need directly.
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question