How to find control with JavaScript

P

Peter

I have a ASP.NET page this page has a Tab Control
Tabl Control has several tabs
Each tab has a custom UserControl
Each UserControl has several ASP.NET controls

I want to be able to disable any one of those ASP.NET controls on the client
side.
My question is: While on Tab 1 how do I find an ASP.NET control which is
located on different Tab and UsercControl using JavaScript?


Thank You


Peter
 
R

Registered User

I have a ASP.NET page this page has a Tab Control
Tabl Control has several tabs
Each tab has a custom UserControl
Each UserControl has several ASP.NET controls

I want to be able to disable any one of those ASP.NET controls on the client
side.
My question is: While on Tab 1 how do I find an ASP.NET control which is
located on different Tab and UsercControl using JavaScript?
Do some research on the control type's ClientId property. If you
wanted to put the focus on Button1using dynamically generated
javascript the server code might write
"document.getElementById('" +Button1.ClientID + "').focus();";

regards
A.G.
 
P

Peter

What about if I am not using dynamically generated JavaScript?
I might have several Button1 controls on the web page

I have a ASP.NET page this page has a Tab Control
TabControl has several tabs
Each tab has a custom UserControl
Each UserControl has it's own Button1 control.

If focus is on Button1 on MyUserControl which is on Tab 1, how do I disable
Button1 on MyUserContro2 which is on Tab 3 with JavaScript,
how do I get the ClientID of Button1 on MyUserContro2 on Tab 3 ?
 
T

Thomas Sun [MSFT]

Hi Peter,

From your description, I understand that you are using Tab Control and you
want to find every control in this Tab Container using JavaScript. If I
have misunderstood you, please feel free to let me know.

ASP.NET doesn't have built-in Tab Control. So could you please tell us
which Tab Control you are using? In this case, I assume that you are using
ASP.NET Ajax Tab Container, and the following example is basing on ASP.NET
Ajax Tab Container.

To disable control in client, we need to get object of the control and then
set its corresponding property using JavaScript. But how can we get them
from Tab Container? The answer is that we can loop all controls in Tab
control and then set property.

We can also use document.getElementById method with control's ClientID as
parameter to get this control. To do so, we can save the ClientID of the
control in Hidden Field, and then use JavaScript to retrieve ClientID from
this Hidden Field and set its property.

Note: The ClientID value is generated for a control is identical to the
UniqueID by ASP.NET and is used to programmatically access the HTML element
rendered for a control in client-side script. For more information about
ClientID, see
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

For the simple example, we place a HiddenField control in the ASP.NET page
and save control's ClientID in this HiddenField's value from CodeBehind
class, and then we access HiddenField's value in JavaScript:

==========================
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HiddenField1.Value += TextBox1.ClientID +"$";
HiddenField1.Value += TextBox2.ClientID + "$";
}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function DisableControl()
{
var obj = document.getElementById('HiddenField1');
var idArray = obj.value.split('$');
for ( var i=0; i< idArray.length; i++)
{
if(idArray != '')
{
var control = document.getElementById(idArray);
control.disabled = true;
}
}

}

function EnableControl()
{
///
///TO DO: Enable control
///

}
</script>

</head>
<body>
<form id="form1" runat="server">
<asp:scriptmanager ID="ScriptManager1" runat="server">
</asp:scriptmanager>
<AjaxToolkit:tabcontainer ID="TabContainer1" runat="server"
ActiveTabIndex="0">
<ajaxtoolkit:tabpanel ID="TabPanel1" runat="server"
HeaderText="TabPanel1">
<contenttemplate>
<input id="btnDisableAnthoerTabControl" type="button"
value="Disable another controls"onclick="DisableControl();" />
</contenttemplate>
</ajaxtoolkit:tabpanel>
<ajaxtoolkit:tabpanel ID="TabPanel2" runat="server"
HeaderText="TabPanel2">
<contenttemplate>
<asp:textbox ID="TextBox1" runat="server"></asp:textbox>
</contenttemplate>
</ajaxtoolkit:tabpanel>
<ajaxtoolkit:tabpanel ID="TabPanel3" runat="server"
HeaderText="TabPanel3">
<contenttemplate>
<asp:textbox ID="TextBox2" runat="server"></asp:textbox>
</contenttemplate>
</ajaxtoolkit:tabpanel>
</AjaxToolkit:tabcontainer>

<asp:hiddenfield ID="HiddenField1" runat="server" />
</form>
</body>
</html>
=================================


Note: If there is UserControl, we can also refer to this HiddenField and
then add control's id to it.
For example, in UserControl's CodeBehind class:
//HiddenField1 is in the parent page
HiddenField hf = (HiddenField)Page.FindControl("HiddenField1");
hf.Value += TextBox1.ClientID + "$";

I look forward to receiving your test results.

Best Regards,
Thomas Sun

Microsoft Online Partner Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter

Thank You very much!

This is what I needed!!!


Thomas Sun said:
Hi Peter,

From your description, I understand that you are using Tab Control and you
want to find every control in this Tab Container using JavaScript. If I
have misunderstood you, please feel free to let me know.

ASP.NET doesn't have built-in Tab Control. So could you please tell us
which Tab Control you are using? In this case, I assume that you are using
ASP.NET Ajax Tab Container, and the following example is basing on ASP.NET
Ajax Tab Container.

To disable control in client, we need to get object of the control and
then
set its corresponding property using JavaScript. But how can we get them
from Tab Container? The answer is that we can loop all controls in Tab
control and then set property.

We can also use document.getElementById method with control's ClientID as
parameter to get this control. To do so, we can save the ClientID of the
control in Hidden Field, and then use JavaScript to retrieve ClientID from
this Hidden Field and set its property.

Note: The ClientID value is generated for a control is identical to the
UniqueID by ASP.NET and is used to programmatically access the HTML
element
rendered for a control in client-side script. For more information about
ClientID, see
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

For the simple example, we place a HiddenField control in the ASP.NET page
and save control's ClientID in this HiddenField's value from CodeBehind
class, and then we access HiddenField's value in JavaScript:

==========================
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HiddenField1.Value += TextBox1.ClientID +"$";
HiddenField1.Value += TextBox2.ClientID + "$";
}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function DisableControl()
{
var obj = document.getElementById('HiddenField1');
var idArray = obj.value.split('$');
for ( var i=0; i< idArray.length; i++)
{
if(idArray != '')
{
var control = document.getElementById(idArray);
control.disabled = true;
}
}

}

function EnableControl()
{
///
///TO DO: Enable control
///

}
</script>

</head>
<body>
<form id="form1" runat="server">
<asp:scriptmanager ID="ScriptManager1" runat="server">
</asp:scriptmanager>
<AjaxToolkit:tabcontainer ID="TabContainer1" runat="server"
ActiveTabIndex="0">
<ajaxtoolkit:tabpanel ID="TabPanel1" runat="server"
HeaderText="TabPanel1">
<contenttemplate>
<input id="btnDisableAnthoerTabControl" type="button"
value="Disable another controls"onclick="DisableControl();" />
</contenttemplate>
</ajaxtoolkit:tabpanel>
<ajaxtoolkit:tabpanel ID="TabPanel2" runat="server"
HeaderText="TabPanel2">
<contenttemplate>
<asp:textbox ID="TextBox1"
runat="server"></asp:textbox>
</contenttemplate>
</ajaxtoolkit:tabpanel>
<ajaxtoolkit:tabpanel ID="TabPanel3" runat="server"
HeaderText="TabPanel3">
<contenttemplate>
<asp:textbox ID="TextBox2"
runat="server"></asp:textbox>
</contenttemplate>
</ajaxtoolkit:tabpanel>
</AjaxToolkit:tabcontainer>

<asp:hiddenfield ID="HiddenField1" runat="server" />
</form>
</body>
</html>
=================================


Note: If there is UserControl, we can also refer to this HiddenField and
then add control's id to it.
For example, in UserControl's CodeBehind class:
//HiddenField1 is in the parent page
HiddenField hf = (HiddenField)Page.FindControl("HiddenField1");
hf.Value += TextBox1.ClientID + "$";

I look forward to receiving your test results.

Best Regards,
Thomas Sun

Microsoft Online Partner Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
T

Thomas Sun [MSFT]

Hi Peter,

Thanks for your response and I am glad that my reply can help you.

If you have any farther question, please feel free to post it here.


Best Regards,
Thomas Sun

Microsoft Online Partner Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Thomas Sun [MSFT]

Hi Mark,

Thanks for your farther explanation of ClientID value.

In the case, I just want to let Peter know that this value is the unique to
every control and the value is often used to programmatically access the
HTML element in client script.



Best Regards,
Thomas Sun

Microsoft Online Partner Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top