Solved

Finding controls inside a details view

Posted on 2008-10-24
5
357 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now