Click here to Skip to main content
15,889,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hye.


I would like to set dynamically-created button size according screen resolution. However, I never get the screen resolution before server side as I need to get the value from Javascript first and assign the value to HiddenField.

1. Since page_load executes first before javascript, how can I get screen resolution before everything is loaded into the page?
2. Or is there any way I can do to set dynamic button size from javascript?


Thank you.
Posted

 
Share this answer
 
Comments
snamyna 21-Aug-13 23:45pm    
Hye. Thanks for your reply.
I already got some idea on how to resize the controls inside my aspx page according screen resolution. But, my problem is, my controls are dynamically created. How can I access all dynamic controls inside javascipt?
Dholakiya Ankit 21-Aug-13 23:50pm    
you have id of that controls so use window.onload method in javascript then access it this will help you
snamyna 22-Aug-13 0:19am    
how to use window.onload to find dynamic controls?
I've tried my page's innerHTML and get the dynamic controls' id.
However when I use this code : var d = document.getElementById('<%= pnlCabPP1.ClientID %>');
the error occurred is
'pnlCabPP1' is not declared. It may be inaccessible due to its protection level.
Dholakiya Ankit 22-Aug-13 0:34am    
can you put some code you have posted diff q but nw you are going to diff way i need some code to help you
snamyna 22-Aug-13 1:17am    
One of the methods that I used:

in aspx page:

<script>
function getScrHeight() {

var d = document.documentElement.clientHeight;
return d;

}
</script>

in code behind:

Dim scriptKey As String = "ScreenHeight"
Dim getScrHeight As String = "<script type='text/javascript'>getScrHeight();</script>"

Dim scrHeight As String = "javascript:getScrHeight();"
ScriptManager.RegisterStartupScript(Me, Me.GetType, scriptKey, "alert(getScrHeight());",True)

How can I assign javascript value into scrHeight variable? because I succeed to get the value in alert method.
You can try setting the height[^] and width[^] of the button on your Javascript code.

As for the screen resolution, it would not make sense to base your button sizes on that one because browsers already have the functionality that allows you to zoom pages. You can check the current zoom level of your browser though in Javascript but seems that almost each browser has a their own solution. See here[^] for a more detailed explanation.
 
Share this answer
 
Comments
snamyna 21-Aug-13 23:48pm    
Yes it is possible. I just need to include some calculation to set the button size. I've already succeed resize the static controls according screen resolution, but I never found the way to find dynamic controls inside javascript.
walterhevedeich 22-Aug-13 0:19am    
I guess the problem with dynamic controls is that they tend to have a different ID when they are rendered on the browser. So the getElementById() function of Javascript may not be useful since you have to provide the exact ID of the control you want to manipulate. Unless you can confirm that the control ID is always the same when rendered on the browser, you can just use the said function in getting the control. Otherwise, I think you may want to implement Jquery on your application since it has more flexible DOM selectors.
snamyna 22-Aug-13 0:27am    
If that is the case, i've got another way to set the dynamic button size. From page_Load function, I need to get the screen width so that I can calculate the suitable button size from code behind. I've tried many methods to achieve that but never succeed.

By assigning screen width into hidden field, the first page_load would make the hidden field value as empty because javascript is not executed yet. How can I get the screen width at first page load before javascript is executed?
walterhevedeich 22-Aug-13 0:56am    
I'm not sure if that's possible, but if it is, it will not be a nice trick because basically, you will be doing a POST on the server for the screen resolution while executing the GET (Page_Load event). Why not do the button sizes on JS instead of doing it on the code behind? Are those dynamic controls buttons that will have the same size? If yes, there's a function in JS called getElementByClassName(). What you can do is create a CSS class that is exclusive only to those dynamic buttons and then calling that function will give you an array of those elements.
snamyna 22-Aug-13 1:03am    
Nice suggestion. I will give it a try. Thanks for your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900