User Controls

Discussion in 'ASP .Net Building Controls' started by Mike, Feb 25, 2004.

  1. Mike

    Mike Guest

    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
     
    Mike, Feb 25, 2004
    #1
    1. Advertising

  2. 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"
    "Mike" <> wrote in message news:%23SPuaL5%...

    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
     
    Alessandro Zifiglio, Feb 26, 2004
    #2
    1. Advertising

  3. Mike

    Jay Guest

    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


    "Alessandro Zifiglio" <> wrote in message news:EMk%b.11864$...
    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"
    "Mike" <> wrote in message news:%23SPuaL5%...

    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
     
    Jay, Feb 26, 2004
    #3
  4. Mike

    Mike Guest

    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


    "Alessandro Zifiglio" <> wrote in message news:EMk%b.11864$...
    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"
    "Mike" <> wrote in message news:%23SPuaL5%...

    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
     
    Mike, Feb 26, 2004
    #4
  5. 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."
    "Mike" <> wrote in message news:O53vUPG$...
    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


    "Alessandro Zifiglio" <> wrote in message news:EMk%b.11864$...
    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"
    "Mike" <> wrote in message news:%23SPuaL5%...

    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
     
    Alessandro Zifiglio, Feb 26, 2004
    #5
  6. your welcome Mike ;)
    "Mike" <> wrote in message news:Ofm5$hH$...

    Alessandro,

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

    Thanks a lot.
    Mike



    "Alessandro Zifiglio" <> wrote in message news:ngm%b.11900$...
    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."
    "Mike" <> wrote in message news:O53vUPG$...
    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


    "Alessandro Zifiglio" <> wrote in message news:EMk%b.11864$...
    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"
    "Mike" <> wrote in message news:%23SPuaL5%...

    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
     
    Alessandro Zifiglio, Feb 26, 2004
    #6
  7. Mike

    Mike Guest

    Alessandro,

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

    Thanks a lot.
    Mike



    "Alessandro Zifiglio" <> wrote in message news:ngm%b.11900$...
    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."
    "Mike" <> wrote in message news:O53vUPG$...
    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


    "Alessandro Zifiglio" <> wrote in message news:EMk%b.11864$...
    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"
    "Mike" <> wrote in message news:%23SPuaL5%...

    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
     
    Mike, Feb 26, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. trinitypete

    HTML Controls and User controls

    trinitypete, Jun 25, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    3,466
    trinitypete
    Jun 26, 2003
  2. Axel Dahmen
    Replies:
    1
    Views:
    543
    Teemu Keiski
    Nov 12, 2003
  3. Nathan Sokalski
    Replies:
    4
    Views:
    1,171
    Muhammad Naveed Yaseen
    Sep 5, 2007
  4. Faizan Ahmed
    Replies:
    1
    Views:
    311
    John Saunders
    Jan 4, 2005
  5. Faizan Ahmed
    Replies:
    1
    Views:
    390
    John Saunders
    Jan 4, 2005
Loading...

Share This Page