ASP.NET user control and accessing a property from Javascript? - asp.net

This I can't work out
I have ASP.Net user control,which is basically two drop downs, that has a public property Index which is calculated from the drop downs, and works fine
I need to access the value of 'Index' from javascript, but accessing via getElementById was completely wrong, can anybody point me in a better direction
Cheers

The property you're talking about is a server side thing and has no presence in Javascript. Basically, whatever ASP.NET does on the server will generate a single HTML page containing a bunch of HTML tags. The browser is not aware of the user control being a single entity at all.
You should calculate the property on the client by looking up the ID of the drop down directly in Javascript. You can do find out the client ID of the drop down by getting its ClientID property.

Create javascript method as a string from server side and use index property to create script string.
register string on which event it is required.
you can take help from following link to register javascript:
http://msdn.microsoft.com/en-us/library/aa479011.aspx
If any server side control is used in javascript method, use ClientID property as ID of the control.
Hope, It helps

Related

get client side element ID of a Field in SilverStripe

In asp.net, server side control have a property called ClientID, which can be used to get the id that template engine generated for a server side control, is there a similar thing in SilverStripe, to get the id of the input element rather than its .field wrapper?
thanks
Have a look at the ID method of FormField
http://api.silverstripe.org/3.1/class-FormField.html
In your templates, you should be able to use
$Fields.FieldByName(FieldName).ID

How to change html view of web user control

I am creating a web user control for a simple poll. I am currently registering it on the page and then referencing it via tagprefix.
The form for the poll is in basic html (no server controls) and is in the front-end of the web control. How can I change the look of the user control depending on the settings passed into it? Is this possible without using server controls?
Update
Can I change the html layout of a user control? If so could someone post some examples. Please note I do not use asp.net form controls, so none of that please :)
You might be able to also use jQuery to replace existing css setting in your code. Create properties on for your user control, and then pass settings in the classes. Then use jQuery to replace them. This however requires jQuery to be linked to your page (or within your control) and you'd have to write the CSS classes out to the jQuery code (using server controls, but you could use the literal control so there's no excess code).
Personally I'd go with the option of using server controls instead of straight up HTML, you'd get alot more flexibility, and then passing through the settings would be pretty straightforward, put something like this in your controls backend code:
Private _TextBoxCssClass As String
Public Property TextBoxCssClass() As String
Get
Return _TextBoxCssClass
End Get
Set(ByVal value As String)
_TextBoxCssClass = value
txtBox1.CssClass = value
txtBox2.CssClass = value
End Set
End Property
You most likely want to have a property or event in the control that changes the css. It may end up best to add some server controls or javascript / jquery to make it easier.
If its only the styles you want to change, then you can expose a property to set the style attribuites of the respective control inside your User Control. If you want to control the whole HTML layout of the control then Custom Control is the viable option.

How to get child controls correct id to client side

I am working on ASP.NET and not using any ASP.NET's AJAX framework. Now I am trying to update the contents of the textboxes and dropdowns in the Grid controls cell on client side using (classic JavaScript way)AJAX. But problem I am facing is that controls (textbox, dropdown) which I would like to update when rendered on the client side get prefixed by the User Controls id and row ids.
So it becomes like UserContro_row_no_controlId. With this it is becoming difficult to keep track of controls ids on the client side to update them.
Please let me know how this can be simplified?
Can we get what exactly will be prefixed to control when it’s rendered on client side?
What is good way to get ID to client side ? I have tried using control.clientId but it’s giving me only _controlId part not the UserContro_row_no part.
Thanks all,
If you want the id of a control in the same control/page you can do that this way:
<%=txtControl.ClientID%>
If you want the id of a control inside a usercontrol you can do that like this:
<%=userControl.FindControl("txtControl").ClientID%>
Where "txtControl" is the server id of the control and "userControl" is the server id of the usercontrol.

How can I pass an ASP.NET querystring value into my silverlight 2 control?

I have an ASP.NET page. What I want to do is pass in an ID field that is in the querystring.
So if my page is
http://www.mysite.com/default.aspx?id=35
I want a silverlight control that is on this page to have access to the id field. My silverlight control is going to get data for a grid and it needs to use the id.
You can use the HTML DOM Bridge to do that:
using System.Windows.Browser;
string queryString = HtmlPage.Document.DocumentUri.Query;
While this covers the concept in startup parameters (which is possibly what you are doing) here's a walk-through about it. http://silverlight.net/learn/learnvideo.aspx?video=72312

Are hidden fields on child window inaccessible from parent window

I have asp.net form that contains fields. When I access this window, my javascript functions can access the fields via the DOM with the getElementById() method and when I postpack to the server I am receiving the updates made by the client.
However, when I launch the form as a child window using Telerik's RadWindow control, the javascript can not access the hidden fields on the child form. Instead I get null.
My questions are:
Are hidden fields on a child window
not accessible when the window is
launched from a parent asp.net form?
Has anyone attempted this with Telerik controls and run into issues?
EDIT
Craig pointed out that the id may be different. Two additional questions then:
Can you ensure that the id you assign at the server is actually used?
Is using getElementByName() a better mechanism to access DOM elements?
To get the ID of your asp.net control do something like this:
<%= theControl.ClientID %>
getElementByName is not as commonly used as getElementById. The ID attribute is supposed to be unique for each element on the page whereas the name attribute can be duplicated.
It is quite possible that the element's ID is not what you think it is. Check the rendered page and see if the ID is there. I am guessing that the page is given a different ID since it is rendered inside another control.
If that is the case, you can have the form render some script that returns the element ID by accessing the controls client ID.
David, I'm sending you this answer because I saw the same issue in my code, and the only REAL solution I found was that I had to support the "OnClick" function in two places... In my case, I was using PetersDatePackage, but it was on a Telerik RAD Strip.
In my case, the control was on a .ascx page, and the JS code was as follows:
function OnIncidentDateChange(ctrl, dtDate, bErr)
{
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
<%=LabelDayOfWeek.ClientID %>.innerText = weekday[dtDate.getDay()];
}
But, this itself was not enough. I had to add THIS code to my parent page. The page that holds the controls for the Telerik strip.
// Dummy function?
function OnIncidentDateChange()
{
}
Once I did that, it worked...
I'm not certain why, to tell you the truth, and it makes no sense to me, and may just be a issue with the PDP package...
I use getElementsByName for checkboxes within the same group.
As for the control's ID, TonyB has the right idea, but make sure you refer to the ClientID property in the PreRender event handler, because if you do it too early in the page life cycle, it will not be available yet).
Is it possible the that javascript is trying to get a reference to the hidden field before the RadWindow has loaded it? I believe I've run into this before and had to use setTimeout to get around the problem.

Resources