Passing parameter values in Asp.net User Controls
-
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
-
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
-
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
-
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
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
-
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
-
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; }
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 ...
-
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 ...
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; }
-
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; }
yep, my mistake i pasted the changes in a different class. I modified my message, can u pls have a look at it? Thanks