Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding controls inside a details view

Posted on 2008-10-24
5
Medium Priority
?
366 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

705 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