how to access server control from javascript
-
hi, I have a custom server control (.ascx file) that contains hidden textbox in a table. After that, in another page I use this control and i want to generate javascript to access this hidden textbox (my javascript code to access it: documet.framse[1].frmName.txtbox.value, but it works incorrectly) if Anyone know it, Pls help me. Thanks a lot :zzz:
-
hi, I have a custom server control (.ascx file) that contains hidden textbox in a table. After that, in another page I use this control and i want to generate javascript to access this hidden textbox (my javascript code to access it: documet.framse[1].frmName.txtbox.value, but it works incorrectly) if Anyone know it, Pls help me. Thanks a lot :zzz:
Hi there, If you have a hidden textbox in a user control (say with an id of "Hidden1") and the id of the WebUserControl, when you insert it into the aspx page, is for example "WebUserControl11", e.g.
<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>
Then when the page is finally rendered, that hidden textbox will have an id of "WebUserControl11_Hidden1". So if in the aspx page you have some javascript, then something like the following will access it...
function getHiddenTextValue()
{
strValue = "";
var elHidden = document.getElementById("WebUserControl11_Hidden1");
if (elHidden)
{
strValue = elHidden.value;
}
return strValue;
}Hope this helps, Andy
-
hi, I have a custom server control (.ascx file) that contains hidden textbox in a table. After that, in another page I use this control and i want to generate javascript to access this hidden textbox (my javascript code to access it: documet.framse[1].frmName.txtbox.value, but it works incorrectly) if Anyone know it, Pls help me. Thanks a lot :zzz:
A Web Control acts as a naming container for its sub-controls. What this means is that your textbox will be renamed on the client side in order to ensure that it is unique. This is the case for both your custom control and the framework supplied ASP.NET controls. Assume for a moment that you dropped two of your custom controls on a page. If the hidden textbox name was not changed, you would have two controls with the id 'hidden1' on the page. What I would recommend is that you make your hidden input a server control (use runat="server"), and then add a property to retrieve the
ClientID
property. You could then use this property to dynamically retrieve the id of your hidden field. Code Behind:namespace MyWebControl
{
public class MyControl : System.Web.UI.UserControl
{
protected System.Web.UI.HtmlControls.HtmlInputHidden hidden1;public string HiddenClientName { get {return hidden1.ClientID;} }
}
}Client Script:
function getHiddenTextValue()
{
strValue = ""; -
Hi there, If you have a hidden textbox in a user control (say with an id of "Hidden1") and the id of the WebUserControl, when you insert it into the aspx page, is for example "WebUserControl11", e.g.
<uc1:WebUserControl1 id="WebUserControl11" runat="server"></uc1:WebUserControl1>
Then when the page is finally rendered, that hidden textbox will have an id of "WebUserControl11_Hidden1". So if in the aspx page you have some javascript, then something like the following will access it...
function getHiddenTextValue()
{
strValue = "";
var elHidden = document.getElementById("WebUserControl11_Hidden1");
if (elHidden)
{
strValue = elHidden.value;
}
return strValue;
}Hope this helps, Andy
thanks a lot:eek: