Link to home
Start Free TrialLog in
Avatar of Andy Green
Andy GreenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Find Control problem

Hi All

I have a multipage and within that a PageView within that a Placeholder, and into this place holder I load a number of user controls.

On this user control I have a checkbox and a textbox.

I'm having trouble getting the text value on postback. From testing I dont think the user controls are being passed back to the server on postback.

On the page I have a test button:

    Protected Sub btnTest_Click(sender As Object, e As System.EventArgs) Handles btnTest.Click

        Dim TextBox As TextBox

        TextBox = CType((Page.FindControl("Name_txtDemographicElement")), TextBox)
        Response.Write(TextBox.Text)

    End Sub

Open in new window


This returns the error -  Exception Details: System.NullReferenceException: Object reference not set to an instance of an object, for the textbox line.

But if I change the FindControl argument to  txtAlias it returns the value of the text box.

The differences here are that the first one is a user control but the second (working)one is a fixed text box on the page (ie it's not a user conrtol)

The id values were taken from the page view source, so I know they exist on the page before postback.

Does anyone have any ideas?

Andy
Avatar of Pratima
Pratima
Flag of India image

you need to find first user control and in that yoe need find textbox

WebUserControl WebUserControl1 = (WebUserControl)FindControl("WebUserControl1");
TextBox TextBox1 = (TextBox)WebUserControl1.FindControl("TextBox1");


refer
http://forums.asp.net/p/1224243/2193706.aspx
You do not need to find the user control, it already exists in your page.
Let MyUserControl1 be your your user control on page, the
Protected Sub btnTest_Click(sender As Object, e As System.EventArgs) Handles btnTest.Click

        Dim TextBox As TextBox = CType((MyUserControl1.FindControl("Name_txtDemographicElement")), TextBox)
        Response.Write(TextBox.Text)
   End Sub

Open in new window

Avatar of Andy Green

ASKER

Thanks for reply, but don't fully understand.

This is my user conrtol markup:

<%@ Register src="../Controls/DemographicElement.ascx" tagname="DemographicElement" tagprefix="ucDemographicElement" %>

I then add the control to the page like this:

For Each rw As DataRow In dc_ds.Tables(0).Rows
                        Dim uc As Controls_DemographicElement = TryCast(LoadControl("~\Controls\DemographicElement.ascx"), Controls_DemographicElement)
                        With uc

                            .ID = rw("ControlName").ToString
                            .type = rw("ControlType").ToString
                            .name = rw("ControlName").ToString
                            
                        End With

Open in new window


What do I use in place of Webusercontrol & WebUserControl1. I have tried various combinations with no joy

Andy
Thanks MAS_OZ2003

Same question what do I replace the M<yUserCOntrol1 with, The names are generated when the control is created.

Andy
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia 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
In the page load I call the load sub routine this goes to  a SQl database and Iload the controls bases on what is there,

State?? this might be my problem, I'm just creating them on the page and expecting them to be there on postback. I postback only once and close the page behind me, so wasn't worried about state beyond 1 postback.

Thanks for links, checking them out now.

PS, sorry should have said dynamicalkyy loaded.

Andy
You actually have to keep state because your page will recreate the controls and your page will lose all the values when page postbacks even it is only once.
This is very confusing.

I also have in my Usr Control a Property for New_Value, can this be read serverside, I need to do some more reading I think, I cant see how the values get into the property, and if on postback the controls are recreated the control maintain state.

I also have another issue in that I cant load the controls on page  preinit, as there is some page load code that runs to detemine which user controls to laod.

Should I move all this code into the page Preinit

Andy
Please post your code to have a look.
Thanks for the offer MAS_OZ2003, I have done thsi now using 'Old School' techniques.

I can get the datavalues using thr Request Form object. Not ideal, But I can revisit this in the new year.

Your links and insight have been extremely hekpfull and I'll award you the points.

Andy
Thanks again - see closing comments in last post.

Andy