Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Passing parameter values in Asp.net User Controls

Passing parameter values in Asp.net User Controls

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netwinformsjsonquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    just4ulove7
    wrote on last edited by
    #1

    Hi, I am new to UserControls in asp.net. I guess, this is a very basic thing that i am implementing, but still not able to find it. I have a user control named Timesheettabs.ascx It has public string CurrentTabName; Now I am including this usercontrol in my aspx page. Say <%@ Register TagPrefix="pm" TagName="Report" Src="~/UserControls/TimeSheetTabs.ascx" %> And this is how I am invoking it in .aspx page. Now I want to initialize the CurrentTabName variable at runtime through codebehind, and the rest of the code is in usercontrol itself. So I just wanted to know how should I pass the value of CurrentTabName through code behind? Thanks

    G 1 Reply Last reply
    0
    • J just4ulove7

      Hi, I am new to UserControls in asp.net. I guess, this is a very basic thing that i am implementing, but still not able to find it. I have a user control named Timesheettabs.ascx It has public string CurrentTabName; Now I am including this usercontrol in my aspx page. Say <%@ Register TagPrefix="pm" TagName="Report" Src="~/UserControls/TimeSheetTabs.ascx" %> And this is how I am invoking it in .aspx page. Now I want to initialize the CurrentTabName variable at runtime through codebehind, and the rest of the code is in usercontrol itself. So I just wanted to know how should I pass the value of CurrentTabName through code behind? Thanks

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Just set the property: Report1.CurrentTabName="Tasks"; --- b { font-weight: normal; }

      J 1 Reply Last reply
      0
      • G Guffa

        Just set the property: Report1.CurrentTabName="Tasks"; --- b { font-weight: normal; }

        J Offline
        J Offline
        just4ulove7
        wrote on last edited by
        #3

        I am not getting Report1 in the intellisense in code behind, neither it is accessible. So I added protected UserControl Report1; but it doesnot show "CurrentTabName" as property. How to access Report1 in code behind? Thanks

        A G 2 Replies Last reply
        0
        • J just4ulove7

          I am not getting Report1 in the intellisense in code behind, neither it is accessible. So I added protected UserControl Report1; but it doesnot show "CurrentTabName" as property. How to access Report1 in code behind? Thanks

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          Define a public property in ur UserControl as Below Partial Class QuickLinks Inherits System.Web.UI.UserControl Public currentPage As String Public dsQuickLinks As DataSet Public Property CurrentPageName() As String Get Return currentPage End Get Set(ByVal value As String) currentPage = CStr(value) End Set End Property Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load....... Then Acces this property in the Parent Page as ctlControlName.CurrentPageName = "XYZ" Regrads

          1 Reply Last reply
          0
          • J just4ulove7

            I am not getting Report1 in the intellisense in code behind, neither it is accessible. So I added protected UserControl Report1; but it doesnot show "CurrentTabName" as property. How to access Report1 in code behind? Thanks

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            just4ulove7 wrote: but it doesnot show "CurrentTabName" as property. That is correct. The UserControl class does not have any CurrentTabName property. You have to declare it instead as your class that inherited UserControl. --- b { font-weight: normal; }

            J 1 Reply Last reply
            0
            • G Guffa

              just4ulove7 wrote: but it doesnot show "CurrentTabName" as property. That is correct. The UserControl class does not have any CurrentTabName property. You have to declare it instead as your class that inherited UserControl. --- b { font-weight: normal; }

              J Offline
              J Offline
              just4ulove7
              wrote on last edited by
              #6

              Sorry, I got what you say... but still confused :(. I am pasting my piece of code.. My comments start with ??? Have put my doubts... This is the user control... public abstract class TimeSheetTabs : System.Web.UI.UserControl { protected System.Web.UI.WebControls.DataList lstTabs; ??? This is where I had placed the public variables before.... // public string CurrentTabName; // public string CallersTabName; public string _CurrentTabName=String.Empty; public string _CallersTabName=String.Empty; ???These are the changes that I made by declaring the properties.......... public string CurrentTabName { get{return _CurrentTabName;} set{_CurrentTabName=value;} } public string CallersTabName { get{return _CallersTabName;} set{_CallersTabName=value;} } public class TimeSheetTab { public string Name=String.Empty; public string Url = String.Empty; public string _CurrentTabName=String.Empty; public string _CallersTabName=String.Empty; public TimeSheetTab(string xName, string xUrl) { Name=xName; Url=xUrl; } } private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here ArrayList colTabs= new ArrayList(); if (CallersTabName=="TimeSheet") { colTabs.Add(new TimeSheetTab("My Time-Sheet","~/Projects/AddTimeSheet.aspx")); colTabs.Add(new TimeSheetTab("Reportees Time-Sheet","~/Projects/ReporteesTimeSheet.aspx")); } else if (CallersTabName=="Reports") { colTabs.Add(new TimeSheetTab("Tasks","~/Report.aspx?ReportName=Tasks")); colTabs.Add(new TimeSheetTab("Defects","~/Report.aspx?ReportName=Defects")); } lstTabs.DataSource=colTabs; lstTabs.DataBind(); } void lstTabs_ItemDataBound(Object s, DataListItemEventArgs e) { TimeSheetTab objTab= (TimeSheetTab)e.Item.DataItem; HyperLink lnkTab = (HyperLink)e.Item.FindControl("lnkTab"); lnkTab.Text=objTab.Name; lnkTab.NavigateUrl=objTab.Url; if (String.Compare(CurrentTabName,objTab.Name)==0) e.Item.CssClass="adminTabActive"; else e.Item.CssClass="adminTabInactive"; } Can u pls tell me how to access it in the Parent page at cs/vb side? Or send a sample code, that would be quite helpful.:((. I am not getting the property in the intellisense. ... My aspx side goes as ...

              G 1 Reply Last reply
              0
              • J just4ulove7

                Sorry, I got what you say... but still confused :(. I am pasting my piece of code.. My comments start with ??? Have put my doubts... This is the user control... public abstract class TimeSheetTabs : System.Web.UI.UserControl { protected System.Web.UI.WebControls.DataList lstTabs; ??? This is where I had placed the public variables before.... // public string CurrentTabName; // public string CallersTabName; public string _CurrentTabName=String.Empty; public string _CallersTabName=String.Empty; ???These are the changes that I made by declaring the properties.......... public string CurrentTabName { get{return _CurrentTabName;} set{_CurrentTabName=value;} } public string CallersTabName { get{return _CallersTabName;} set{_CallersTabName=value;} } public class TimeSheetTab { public string Name=String.Empty; public string Url = String.Empty; public string _CurrentTabName=String.Empty; public string _CallersTabName=String.Empty; public TimeSheetTab(string xName, string xUrl) { Name=xName; Url=xUrl; } } private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here ArrayList colTabs= new ArrayList(); if (CallersTabName=="TimeSheet") { colTabs.Add(new TimeSheetTab("My Time-Sheet","~/Projects/AddTimeSheet.aspx")); colTabs.Add(new TimeSheetTab("Reportees Time-Sheet","~/Projects/ReporteesTimeSheet.aspx")); } else if (CallersTabName=="Reports") { colTabs.Add(new TimeSheetTab("Tasks","~/Report.aspx?ReportName=Tasks")); colTabs.Add(new TimeSheetTab("Defects","~/Report.aspx?ReportName=Defects")); } lstTabs.DataSource=colTabs; lstTabs.DataBind(); } void lstTabs_ItemDataBound(Object s, DataListItemEventArgs e) { TimeSheetTab objTab= (TimeSheetTab)e.Item.DataItem; HyperLink lnkTab = (HyperLink)e.Item.FindControl("lnkTab"); lnkTab.Text=objTab.Name; lnkTab.NavigateUrl=objTab.Url; if (String.Compare(CurrentTabName,objTab.Name)==0) e.Item.CssClass="adminTabActive"; else e.Item.CssClass="adminTabInactive"; } Can u pls tell me how to access it in the Parent page at cs/vb side? Or send a sample code, that would be quite helpful.:((. I am not getting the property in the intellisense. ... My aspx side goes as ...

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                You have declared the properties in the TimeSheetTab class. You have to access them through a TimeSheetTab object. What do you mean by accessing it in the Parent page? Any object that you create in the code for a page only exists while the code is running. When you are running the code for a different page, you can't access the objects of another page, as they do not exist any more. --- b { font-weight: normal; }

                J 1 Reply Last reply
                0
                • G Guffa

                  You have declared the properties in the TimeSheetTab class. You have to access them through a TimeSheetTab object. What do you mean by accessing it in the Parent page? Any object that you create in the code for a page only exists while the code is running. When you are running the code for a different page, you can't access the objects of another page, as they do not exist any more. --- b { font-weight: normal; }

                  J Offline
                  J Offline
                  just4ulove7
                  wrote on last edited by
                  #8

                  yep, my mistake i pasted the changes in a different class. I modified my message, can u pls have a look at it? Thanks

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups