Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi, How can we access the webcontrols within a user control.

For example, I created a user control that has two text boxes in it. Now, I want to read the text entered in these text boxes from the main page. How can we do that?

 

SJ.

Posted

I never do it before, but you can try this:

 

If you usercontrol name is "myUsercontrol"

 

  Quote

Protected WithEvents myUsercontrol_textbox1 As System.Web.UI.WebControls.TextBox

[/Quote]

 

I hope it work... :)

Posted

Nope...that didn't work!!

Its giving the following error:

---------------------------------------------------------------------------

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

 

Source Error:

 

 

If ((Not NewPassRows1_txtNewFirstPass.Text Is String.Empty) And (NewPassRows1_txtNewFirstPass.Enabled = True)) Then

' Error is in the above line'

passText = NewPassRows1_txtNewFirstPass.Text

End If

--------------------------------------------------------------------------

 

NewPassRows1 is the user control and txtNewFirstPass is a textbox in the control.

 

 

SJ.

Posted

Make your textbox public (not protected) in user control:

Public WithEvents txtNewFirstPass As System.Web.UI.WebControls.TextBox

Declare your user control in main page:

Public WithEvents NewPassRows1 As WebUserControl1

 

Now you can call text property of textbox:

NewPassRows1.txtNewFirstPass.Text

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted

Hi....It did not work. It is saying that txtFirstNewPass is not a member of System.Web.UI.UserControl !!!!

 

I declared the text field txtNewFirstPass as Public in the usercontrol code-behind file and also declared the NewPassRows1 instance of NewPassRows in the main .aspx page as a UserControl.

 

 

SJ.

Posted

this is the error it is giving me when I did that:

==============================================

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

 

Source Error:

 

 

Line 110: Dim passText As String, returnValue As Integer

Line 111: passText = curUser.Get_Pass()

Line 112: If ((Not NewPassRows1_txtNewFirstPass.Text Is String.Empty) And (NewPassRows1_txtNewFirstPass.Enabled = True)) Then

Line 113: passText = NewPassRows1_txtNewFirstPass.Text

Line 114: End If

 

 

\EditProfile.aspx.vb Line: 112

==============================================

I made the textbox in the usercontrol public and also declared the user control textbox like this:

 

Protected WithEvents NewPassRows1_txtNewFirstPass As System.Web.UI.WebControls.TextBox

 

SJ

Posted

It should work like what hrabia said...

 

Or you can create a "Property" in usercontrol refer to the textbox.

 

If you still can't get, then properly you miss the basic thing.... I suggest you post or attach your source here let us identify if possible

Posted

Hi Guys,

That did work. As you said, I declared the usercontrol in the wrong way. But when I changed it to what "hrabia" suggested, it worked. Thanks a lot for responding.

 

SJ

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...