Disable and enable the client and server of Asp.net related controls

Source: Internet
Author: User

This example includes examples of the following controls:
Button, imagebutton, Textbox,
Dropdownlist, checkbox, radiobutton
Disable and enable the client and server
And
Linkbutton, Which is disabled and enabled by the hyperlink server.
And
Implemented by using parent controls such as TD
Linkbutton, Which is disabled and enabled by the hyperlink client.

----------------------------------------

Button, imagebutton, Textbox,
Dropdownlist, checkbox, radiobutton
Disable and enable the client and server
------------------------------------
Server-relatedCodeAs follows:
// Enable on the server
Protected void button2_click (Object sender, eventargs E)
{
Button1.attributes. Remove ("disabled ");
Imagebutton1.attributes. Remove ("disabled ");
Textbox1.attributes. Remove ("disabled ");
Dropdownlist1.attributes. Remove ("disabled ");

Checkbox1.attributes. Remove ("disabled ");
Radiobutton1.attributes. Remove ("disabled ");
}

// Disable the server
Protected void button4_click (Object sender, eventargs E)
{
Button1.attributes ["disabled"] = "true ";
Imagebutton1.attributes ["disabled"] = "true ";
Textbox1.attributes ["disabled"] = "true ";
Dropdownlist1.attributes ["disabled"] = "true ";

Checkbox1.attributes ["disabled"] = "true ";
Radiobutton1.attributes ["disabled"] = "true ";
}

The client-side code is as follows:

<Script language = "JavaScript" type = "text/JavaScript">
// Disable the client
Function controldisabled ()
{
Document. All. button1.disabled = true;
Document. All. imagebutton1.disabled = true;

Document. All. textbox1.disabled = true;
Document. All. dropdownlist1.disabled = true;

Document. All. checkbox1.disabled = true;
Document. All. radiobutton1.disabled = true;
}

// Enable the client
Function controlenabled ()
{
Document. All. button1.disabled = false;
Document. All. imagebutton1.disabled = false;

Document. All. textbox1.disabled = false;
Document. All. dropdownlist1.disabled = false;

Document. All. checkbox1.disabled = false;
Document. All. radiobutton1.disabled = false;
}
</SCRIPT>
-----------------------------
Linkbutton, Which is disabled and enabled by the hyperlink server.
(Disabling the onclientclick event of linkbutton is not supported)
-------------------------------------------------
<Asp: linkbutton id = "linkbutton1" runat = "server"
Onclick = "linkbutton#click">
Linkbutton </ASP: linkbutton>
<Br/>
<Asp: hyperlink id = "hyperlink1" runat = "server" navigateurl = "http://www.freeliver.cn" target = "_ blank">
Hyperlink </ASP: hyperlink>
<Br/>

Server code:
// Linkbutton, enabled on the hyperlink Server
Protected void button2_click (Object sender, eventargs E)
{
Linkbutton1.enabled = true;
Hyperlink1.enabled = true;
}

// Linkbutton, Which is disabled on the hyperlink Server
Protected void button4_click (Object sender, eventargs E)
{
Linkbutton1.enabled = false;
Hyperlink1.enabled = false;
}
-----------------------
With the help of parent controls such as TD
To disable and enable the linkbutton and hyperlink client.
--------------------------
<Table>
<Tr>
<TD id = "tdlinkbutton" onclick = "Return tdlinkbuttononclick ();">
<Asp: linkbutton id = "linkbutton1" runat = "server"
Onclick = "linkbutton#click">
Linkbutton </ASP: linkbutton>
</TD>
<TD id = "tdhyperlink" onclick = "Return tdhyperlinkonclick ();">
<Asp: hyperlink id = "hyperlink1" runat = "server"
Navigateurl = "http://www.freeliver.cn" target = "_ blank">
Hyperlink </ASP: hyperlink>
</TD>
</Tr>
</Table>

<Script language = "JavaScript" type = "text/JavaScript">
// Linkbutton, Which is disabled by the hyperlink Client
Function controldisabled ()
{
Document. All. linkbutton1.disabled = true;
Document. All. hyperlink1.disabled = true;
}
// Linkbutton, Which is enabled on the hyperlink Client
Function controlenabled ()
{
Document. All. linkbutton1.disabled = false;
Document. All. hyperlink1.disabled = false;
}

// Onclick event of tdlinkbutton
// If the disabled of linkbutton1 is = true
// Does not respond to related events
Function tdlinkbuttononclick ()
{
If (document. All. linkbutton1.disabled)
{
Return false;
}
Else
{
Return true;
}
}

// Tdhyperlink onclick event
// If the disabled of hyperlink1 is = true
// Does not respond to related events
Function tdhyperlinkonclick ()
{
If (document. All. hyperlink1.disabled)
{
Return false;
}
Else
{
Return true;
}
}
</SCRIPT>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.