User Controls

M

Mike

In a test project, I have created two WebForms and one WebUserControl that contains a textbox and a button. I would like to display a string in the textbox when the WebForm1 is used, but another string when WebForm2 is used. How can I do this? Do I have to create 2 different classes and then reference them in the "@ Register" directive, like this?

In WebForm1:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm1.ascx" %>

In WebForm2:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm2.ascx" %>

But what about the default class that is created when I create the WebUserControl? Is it safe to delete it? Sorry for so many question, but WebControls are a subject that I cannot really understand.

Any help is appreciated...

Thank you very much.
Mike
 
A

Alessandro Zifiglio

hi Mike,
have your UserControl expose a text property. Your going to have to use get/set accessors here. And then programmatically reference this text property on your webform that way if you were on webform1 you would go :
webusercontrol1.text = "Text specific to webform1"

and if you were on webform2 :
webusercontrol1.text = "Text specific to webform2"

In a test project, I have created two WebForms and one WebUserControl that contains a textbox and a button. I would like to display a string in the textbox when the WebForm1 is used, but another string when WebForm2 is used. How can I do this? Do I have to create 2 different classes and then reference them in the "@ Register" directive, like this?

In WebForm1:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm1.ascx" %>

In WebForm2:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm2.ascx" %>

But what about the default class that is created when I create the WebUserControl? Is it safe to delete it? Sorry for so many question, but WebControls are a subject that I cannot really understand.

Any help is appreciated...

Thank you very much.
Mike
 
J

Jay

HI Alessandro,

The problem is that I don't see the user control in my WebForm. I added the properties to the template page, in which I have my 3 controls. But then the Main page does not inherit from this template.ascx. This is the first time I am doing this, and I think something is wrong with the inheritance or register directive. But cannot find the problem.

--
Michael


hi Mike,
have your UserControl expose a text property. Your going to have to use get/set accessors here. And then programmatically reference this text property on your webform that way if you were on webform1 you would go :
webusercontrol1.text = "Text specific to webform1"

and if you were on webform2 :
webusercontrol1.text = "Text specific to webform2"

In a test project, I have created two WebForms and one WebUserControl that contains a textbox and a button. I would like to display a string in the textbox when the WebForm1 is used, but another string when WebForm2 is used. How can I do this? Do I have to create 2 different classes and then reference them in the "@ Register" directive, like this?

In WebForm1:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm1.ascx" %>

In WebForm2:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm2.ascx" %>

But what about the default class that is created when I create the WebUserControl? Is it safe to delete it? Sorry for so many question, but WebControls are a subject that I cannot really understand.

Any help is appreciated...

Thank you very much.
Mike
 
M

Mike

HI Alessandro,

The problem is that I don't see the user control in my WebForm. I added the properties to the template page, in which I have my 3 controls. But then the Main page does not inherit from this template.ascx. This is the first time I am doing this, and I think something is wrong with the inheritance or register directive. But cannot find the problem.

Mike


hi Mike,
have your UserControl expose a text property. Your going to have to use get/set accessors here. And then programmatically reference this text property on your webform that way if you were on webform1 you would go :
webusercontrol1.text = "Text specific to webform1"

and if you were on webform2 :
webusercontrol1.text = "Text specific to webform2"

In a test project, I have created two WebForms and one WebUserControl that contains a textbox and a button. I would like to display a string in the textbox when the WebForm1 is used, but another string when WebForm2 is used. How can I do this? Do I have to create 2 different classes and then reference them in the "@ Register" directive, like this?

In WebForm1:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm1.ascx" %>

In WebForm2:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm2.ascx" %>

But what about the default class that is created when I create the WebUserControl? Is it safe to delete it? Sorry for so many question, but WebControls are a subject that I cannot really understand.

Any help is appreciated...

Thank you very much.
Mike
 
A

Alessandro Zifiglio

Mike,
if you simply dragged the userControl from solution explorer in vs.net and dropped it on the page vs.net will automatically put the @ directive and reference your control on the page for you. In addition it will add the control with its corresponding tags like below :

in webform1.aspx
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>

<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>


now in your code behind class for webform1 webform1.vb or webform1.cs if you code in c#
If you wanted to then reference this control in your code behind you need to declare it like any webcontrol.

Protected WithEvents WebUserControl11 As WebUserControl1

make sure that the id you provide when declaring it in your code behind class is the same as the control ID provided in our example above it is : webUserControl11
now you can easily access any of of its properties and members same way you would with any webControl using the controlName and period like:

webusercontrol11.Visible = False

I dont know if this is what you are after, otherwise you might need to provide me some sample code and go into details as to what you want to achieve. I'm having difficulty following the part where you mentioned "I added the properties to the template page, in which I have my 3 controls. But then the Main page does not inherit from this template.ascx."
HI Alessandro,

The problem is that I don't see the user control in my WebForm. I added the properties to the template page, in which I have my 3 controls. But then the Main page does not inherit from this template.ascx. This is the first time I am doing this, and I think something is wrong with the inheritance or register directive. But cannot find the problem.

Mike


hi Mike,
have your UserControl expose a text property. Your going to have to use get/set accessors here. And then programmatically reference this text property on your webform that way if you were on webform1 you would go :
webusercontrol1.text = "Text specific to webform1"

and if you were on webform2 :
webusercontrol1.text = "Text specific to webform2"

In a test project, I have created two WebForms and one WebUserControl that contains a textbox and a button. I would like to display a string in the textbox when the WebForm1 is used, but another string when WebForm2 is used. How can I do this? Do I have to create 2 different classes and then reference them in the "@ Register" directive, like this?

In WebForm1:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm1.ascx" %>

In WebForm2:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm2.ascx" %>

But what about the default class that is created when I create the WebUserControl? Is it safe to delete it? Sorry for so many question, but WebControls are a subject that I cannot really understand.

Any help is appreciated...

Thank you very much.
Mike
 
A

Alessandro Zifiglio

your welcome Mike ;)

Alessandro,

Removing all register directives I had and then dragging and dropping the control over the WebForm fixed the problem.

Thanks a lot.
Mike



Mike,
if you simply dragged the userControl from solution explorer in vs.net and dropped it on the page vs.net will automatically put the @ directive and reference your control on the page for you. In addition it will add the control with its corresponding tags like below :

in webform1.aspx
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>

<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>


now in your code behind class for webform1 webform1.vb or webform1.cs if you code in c#
If you wanted to then reference this control in your code behind you need to declare it like any webcontrol.

Protected WithEvents WebUserControl11 As WebUserControl1

make sure that the id you provide when declaring it in your code behind class is the same as the control ID provided in our example above it is : webUserControl11
now you can easily access any of of its properties and members same way you would with any webControl using the controlName and period like:

webusercontrol11.Visible = False

I dont know if this is what you are after, otherwise you might need to provide me some sample code and go into details as to what you want to achieve. I'm having difficulty following the part where you mentioned "I added the properties to the template page, in which I have my 3 controls. But then the Main page does not inherit from this template.ascx."
HI Alessandro,

The problem is that I don't see the user control in my WebForm. I added the properties to the template page, in which I have my 3 controls. But then the Main page does not inherit from this template.ascx. This is the first time I am doing this, and I think something is wrong with the inheritance or register directive. But cannot find the problem.

Mike


hi Mike,
have your UserControl expose a text property. Your going to have to use get/set accessors here. And then programmatically reference this text property on your webform that way if you were on webform1 you would go :
webusercontrol1.text = "Text specific to webform1"

and if you were on webform2 :
webusercontrol1.text = "Text specific to webform2"

In a test project, I have created two WebForms and one WebUserControl that contains a textbox and a button. I would like to display a string in the textbox when the WebForm1 is used, but another string when WebForm2 is used. How can I do this? Do I have to create 2 different classes and then reference them in the "@ Register" directive, like this?

In WebForm1:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm1.ascx" %>

In WebForm2:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm2.ascx" %>

But what about the default class that is created when I create the WebUserControl? Is it safe to delete it? Sorry for so many question, but WebControls are a subject that I cannot really understand.

Any help is appreciated...

Thank you very much.
Mike
 
M

Mike

Alessandro,

Removing all register directives I had and then dragging and dropping the control over the WebForm fixed the problem.

Thanks a lot.
Mike



Mike,
if you simply dragged the userControl from solution explorer in vs.net and dropped it on the page vs.net will automatically put the @ directive and reference your control on the page for you. In addition it will add the control with its corresponding tags like below :

in webform1.aspx
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>

<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>


now in your code behind class for webform1 webform1.vb or webform1.cs if you code in c#
If you wanted to then reference this control in your code behind you need to declare it like any webcontrol.

Protected WithEvents WebUserControl11 As WebUserControl1

make sure that the id you provide when declaring it in your code behind class is the same as the control ID provided in our example above it is : webUserControl11
now you can easily access any of of its properties and members same way you would with any webControl using the controlName and period like:

webusercontrol11.Visible = False

I dont know if this is what you are after, otherwise you might need to provide me some sample code and go into details as to what you want to achieve. I'm having difficulty following the part where you mentioned "I added the properties to the template page, in which I have my 3 controls. But then the Main page does not inherit from this template.ascx."
HI Alessandro,

The problem is that I don't see the user control in my WebForm. I added the properties to the template page, in which I have my 3 controls. But then the Main page does not inherit from this template.ascx. This is the first time I am doing this, and I think something is wrong with the inheritance or register directive. But cannot find the problem.

Mike


hi Mike,
have your UserControl expose a text property. Your going to have to use get/set accessors here. And then programmatically reference this text property on your webform that way if you were on webform1 you would go :
webusercontrol1.text = "Text specific to webform1"

and if you were on webform2 :
webusercontrol1.text = "Text specific to webform2"

In a test project, I have created two WebForms and one WebUserControl that contains a textbox and a button. I would like to display a string in the textbox when the WebForm1 is used, but another string when WebForm2 is used. How can I do this? Do I have to create 2 different classes and then reference them in the "@ Register" directive, like this?

In WebForm1:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm1.ascx" %>

In WebForm2:
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebCtrlForForm2.ascx" %>

But what about the default class that is created when I create the WebUserControl? Is it safe to delete it? Sorry for so many question, but WebControls are a subject that I cannot really understand.

Any help is appreciated...

Thank you very much.
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top