Link to home
Start Free TrialLog in
Avatar of Frosty555
Frosty555Flag for Canada

asked on

Finding controls inside a details view

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?
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

U'll have to use the findcontrol method

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

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

ctrl.TargetTextBox = txt1.ID;
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

Avatar of Frosty555

ASKER

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

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
ASKER CERTIFIED SOLUTION
Avatar of Frosty555
Frosty555
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial