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
Solved

Finding controls inside a details view

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

790 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