Passing Value to a Web User Control
-
Is there a way to pass a string value to a Web User Control used by a Class to load the Header to WebForm1.aspx Thanks in advance.
-
Is there a way to pass a string value to a Web User Control used by a Class to load the Header to WebForm1.aspx Thanks in advance.
Sure. You could define a string property in your user control to accept the value, then in the Page_Load of your WebForm1.aspx you can locate the user control and set it's string property accordingly.
-
Sure. You could define a string property in your user control to accept the value, then in the Page_Load of your WebForm1.aspx you can locate the user control and set it's string property accordingly.
Thank you for your reply. I am assuming this is how i need to define a string property:
private string \_DepartmentName; public string DepartmentName { get{return \_DepartmentName;} set{\_DepartmentName = value;} }
But I am not sure how can I locate the control from Webform1.aspx since I am loading control in the BasePage class (WebForm1.aspx is inheriting from BasePage.cs). This is how I am loading the control in my BasePage.cs:
protected void LoadHeader() { //Control Control; Control Header = LoadControl("~/GlobalControls/Header.ascx"); Controls.Add(Header); }
Depending on if user has requested HEADER or not I then call this LoadHeader() to load my control when I am writing other stuff
this.Controls.AddAt( 0, new LiteralControl( @"" + Environment.NewLine + "" + Environment.NewLine + " " + Title + @"" + Environment.NewLine + "" + Environment.NewLine + "" )); // Check to see if Header was requested. if(AddHeader) { LoadHeader(); }
So I guess my question is, how can I call Header.ascx in my WebForm1.aspx and set its property? Once again thanks a lot for helping me with this.
-
Thank you for your reply. I am assuming this is how i need to define a string property:
private string \_DepartmentName; public string DepartmentName { get{return \_DepartmentName;} set{\_DepartmentName = value;} }
But I am not sure how can I locate the control from Webform1.aspx since I am loading control in the BasePage class (WebForm1.aspx is inheriting from BasePage.cs). This is how I am loading the control in my BasePage.cs:
protected void LoadHeader() { //Control Control; Control Header = LoadControl("~/GlobalControls/Header.ascx"); Controls.Add(Header); }
Depending on if user has requested HEADER or not I then call this LoadHeader() to load my control when I am writing other stuff
this.Controls.AddAt( 0, new LiteralControl( @"" + Environment.NewLine + "" + Environment.NewLine + " " + Title + @"" + Environment.NewLine + "" + Environment.NewLine + "" )); // Check to see if Header was requested. if(AddHeader) { LoadHeader(); }
So I guess my question is, how can I call Header.ascx in my WebForm1.aspx and set its property? Once again thanks a lot for helping me with this.
How about allowing the Header to be returned through a property of the BasePage? e.g. in your BasePage class, have a private variable called
_header
that is set through the LoadHeader method. Then have a readonly public propertyBasePage.Header
that would return it. -
How about allowing the Header to be returned through a property of the BasePage? e.g. in your BasePage class, have a private variable called
_header
that is set through the LoadHeader method. Then have a readonly public propertyBasePage.Header
that would return it.Thanks again. As you can tell I dont have a lot of experience in .net:sigh:. From my WebForm1.aspx I can call pass the value of DepartmentName to my BasePage class using the way you suggested. The thing that is still not clear to me is how I would pass this value to my header.ascx control? Like when I load it I use following code:
protected void LoadHeader()
{
Control MyHeader = LoadControl("~/GlobalControls/Header.ascx");
Controls.Add(lssHeader);
/* Here how can I pass my BasePage.Header variable's value to my lssHeader
Something like MyHeader.DepartNameProperty or maybe MyHeader.Add.PropertyName or something.... I just can't seem to figure it out:((.
}Thanks a lot for your help though, I really really appreciate it. Best Regards, Taurian110 -- modified at 23:11 Friday 6th January, 2006
-
Thanks again. As you can tell I dont have a lot of experience in .net:sigh:. From my WebForm1.aspx I can call pass the value of DepartmentName to my BasePage class using the way you suggested. The thing that is still not clear to me is how I would pass this value to my header.ascx control? Like when I load it I use following code:
protected void LoadHeader()
{
Control MyHeader = LoadControl("~/GlobalControls/Header.ascx");
Controls.Add(lssHeader);
/* Here how can I pass my BasePage.Header variable's value to my lssHeader
Something like MyHeader.DepartNameProperty or maybe MyHeader.Add.PropertyName or something.... I just can't seem to figure it out:((.
}Thanks a lot for your help though, I really really appreciate it. Best Regards, Taurian110 -- modified at 23:11 Friday 6th January, 2006
Yes ... I have done the same thing.... but instead of text i was passing string from the Base container or parent page. Since I am VB fan i use to write my code in vb.net just to give you idead. Check the following code :
'Page load of You Base Page Private Sub Page_Load(byval sender as blabla , byval e as blabla) 'Loading Control in a variable called Con Dim Con as Control = LoadControl("~/Controls/MyGrid.ascx") 'I am just converting the type of Control. Coz notice the above line the control which we are loading is actually of "Control" type we are converting this to MyGrid which contain the property Ctype(Con,MyGrid).query = "Select * from tbl_Emp" Page.Control.add(Con) End Sub
I wish the above code will help you out ... if you do the same thing in any other way do let me know .... :) Best Regards, -
Yes ... I have done the same thing.... but instead of text i was passing string from the Base container or parent page. Since I am VB fan i use to write my code in vb.net just to give you idead. Check the following code :
'Page load of You Base Page Private Sub Page_Load(byval sender as blabla , byval e as blabla) 'Loading Control in a variable called Con Dim Con as Control = LoadControl("~/Controls/MyGrid.ascx") 'I am just converting the type of Control. Coz notice the above line the control which we are loading is actually of "Control" type we are converting this to MyGrid which contain the property Ctype(Con,MyGrid).query = "Select * from tbl_Emp" Page.Control.add(Con) End Sub
I wish the above code will help you out ... if you do the same thing in any other way do let me know .... :) Best Regards,I tried to convert the VB.NET code to C# from a utility and I got this:
private void Page_Load(blabla sender, blabla e) { Control Con = LoadControl("~/Controls/MyGrid.ascx"); ((MyGrid)(Con)).query = "Select * from tbl_Emp"; Page.Control.add(Con); }
but this did not do the trick. :(( Thanks for your help though. -
I tried to convert the VB.NET code to C# from a utility and I got this:
private void Page_Load(blabla sender, blabla e) { Control Con = LoadControl("~/Controls/MyGrid.ascx"); ((MyGrid)(Con)).query = "Select * from tbl_Emp"; Page.Control.add(Con); }
but this did not do the trick. :(( Thanks for your help though.Well i guess you have simple copy paste the above line dear private void Page_Load(blabla sender, blabla e) this line is paassing FAKE parameters that is why the trick is not working :)
Private Sub BindData(ByVal Customer_ID As Long) Try Dim dt As DataTable = ObjProject.GetProjectByCustomerID(Customer_ID) Dim con As Control = LoadControl("/Controls/ProjectViewCon.ascx") CType(con, Controls.ProjectViewCon).DataTable = dt PlaceHolder1.Controls.Add(con) Catch ex As CustomExceptions.Exception_NoRecordFound Lbl_Error.Text = "You have not posted any project as yet [click here](/dynamic/PostProject.aspx/) to post project." Catch ex As Exception Throw ex End Try End Sub
This is a function call this function in Your Load method. it will work and please do some necesaary alteration. -
Well i guess you have simple copy paste the above line dear private void Page_Load(blabla sender, blabla e) this line is paassing FAKE parameters that is why the trick is not working :)
Private Sub BindData(ByVal Customer_ID As Long) Try Dim dt As DataTable = ObjProject.GetProjectByCustomerID(Customer_ID) Dim con As Control = LoadControl("/Controls/ProjectViewCon.ascx") CType(con, Controls.ProjectViewCon).DataTable = dt PlaceHolder1.Controls.Add(con) Catch ex As CustomExceptions.Exception_NoRecordFound Lbl_Error.Text = "You have not posted any project as yet [click here](/dynamic/PostProject.aspx/) to post project." Catch ex As Exception Throw ex End Try End Sub
This is a function call this function in Your Load method. it will work and please do some necesaary alteration.Sorry for the misconception. I did make necessary changes... I was just posting that converted text like i got from that website to show what I had in C#. Anyway I will try it again in the morning and let you know. THanks for your help though:).