Introduction -------------------------- The situation is this : I have a developed an user control which acts as a footer in all pages of the project.the code for the user control is given below.Place the user control inside a panel < asp:Panel id="Panel1" runat="server" style="POSITION: absolute; Left:910px; TOP:208px"> < /asp:Panel> -------------------------- Changing the HTML -------------------------- But my task is to strip off the left and top in the style attribute of the panel and set the values dynamically.
so to do that we need to write a property of the user control and apply it to all the pages which are using the user control.
First of all i need to take off the left and top values from the HTML and set position as "relative" change the panel like this : < asp:Panel id="Panel1" runat="server" style="POSITION: relative; > < /asp:Panel >
-------------------------- Code behind settings -------------------------- Now set the property in the code behind of the user control.
1.This property is used to set the TOP value of the style attribute of the panel at runtime.
public int Top { get { // assigning the inner attribute as top
string topValue = Panel1.Style["TOP"]; // if the indexof the value is greater than o then the strip off the "px" from the value
if (topValue.IndexOf("px") > 0)
topValue = topValue.Substring(0, topValue.Length - 2);
// returns the integer value for TOP.
return System.Convert.ToInt32(topValue); } set { //Setting the panels TOP value as string followed by "px"
Panel1.Style["TOP"] = value.ToString() + "px"; } }
2.This property is used to set the LEFT value of the style attribute of the panel at runtime. The description is same as the TOP and it follows the same logic.
public int Left { get { string topValue = Panel1.Style["LEFT"]; if (topValue.IndexOf("px") > 0) topValue = topValue.Substring(0, topValue.Length - 2); return System.Convert.ToInt32(topValue); } set { Panel1.Style["LEFT"] = value.ToString() + "px"; } } These changes are made and the property for the user control is set and we can see how to use this in the page which is using the user control.
1.open the webform which is using the user control.
2.Take the "id" of the user control.
3.Instantiate the id inside the code behind of the web form
for example the id of the user control is say Useroption then write in the code behind C# code: protected Useroption Useroption1;
4.so the new instance is created.Now, In the Page Load, write the below code : // If we type the Useroption1 then in the intelisense it will give the properties of the usercontrol
// set the top values to what ever u want Useroption1.Top = 300; //These 300 and 200 values are just to show the values, you can set //the values as you wish or where u want your usercontrol to be. Useroption1.Left = 200;
-------------------------- Summary -------------------------- Thats it follow the same format in all the webforms which is using the web control.set the values of the left and top as you wish. Hope this example would have given an insight of how to set the inner attributes of the HTML attribute. and to set the property settings of the user control during runtime.Hope this article will be helpful for everyone. Feel free to contact me if u have any questions
--------------------------
|
| Author: santosh narayan poojari 25 Nov 2004 | Member Level: Gold Points : 0 |
Hi, Thanks a lot for explaining Usercontrol.Good explain.Initially i faced some problem,atlast i got the thinks working. Santosh
|
| Author: Daniel 08 Mar 2005 | Member Level: Silver Points : 0 |
hi
it will be useful if u say how to retreive the left and top property of the server control that is placed in the web form.
|
| Author: Mathew J Koithara 23 May 2005 | Member Level: Bronze Points : 0 |
Sir I found the article interesting but there seems to be a mistake in the code. It is mentioned that the id of the control has to be specified while declaring the control in the codebehind.
For example if the id of the usercontrol in the page is cratio then the control has to be declared in the codebehind as
protected cratio cr
where 'cratio' is the id of the usercontrol used in the form
I spent a lot of time trying to make it work with no success.
Ultimately I realised from another code sample in another site that
we have to use the name of the usercontrols class while declaring the control in the codebehind.
For example if the name of the class of the usercontrol is 'UserControlClass' then the control has to be declared in the codebehind as
protected UserControlClass as uc
|