Obtains the Text value of the CheckBox and the checkboxtext value.

Source: Internet
Author: User

Obtains the Text value of the CheckBox and the checkboxtext value.

If you see a problem on the internet, try to get the Checkbox value. The technology is not very difficult, but you have time to do exercises.
Create a webpage:

Write the click event. When the CheckBox is selected, the Text value of the CheckBox is obtained. Otherwise, the TextBox value is cleared:



Real-time Demonstration:



What if the requirement is not to directly access the Checkbox, but to access the CheckBox in the Div label? To try:

In fact, after adding the Div tag control, you can still obtain the CheckBox value using the above method. However, we want to get it from the Div control. How can we implement it?
You can cycle all controls in the Div control. because there may be other controls in the Div control, you must determine whether the same is a Checkbox control:


In the preceding example, the Checkbox Control is only placed in a Div Control. If it is stored in a deep Control, do we need several foreach (Control ctl in div1.Controls) or is the findcontrol () method used to find the CheckBox?

You don't have to worry about this. There is a way for you to quickly find the target, reference here "not using the FindControl method to find deep nested controls" http://www.cnblogs.com/insus/archive/2012/09/27/2705388.html its upgrade article "not using the FindControl method to find deep nested controls Ver2" http://www.cnblogs.com/insus/archive/2012/10/07/2713568.html

 


In js, the value of text and combobox can be obtained using value. How can I obtain the value of checkbox?

Because after the checkbox is compiled into html, it exists in this form.
Before Compilation: <asp: CheckBox ID = "chk1" runat = "server" Text = "11"/>/* asp.net is a column */
After compilation: <input id = "chk1" type = "checkbox" name = "CheckBox1"/> <label for = "chk1"> 11 </label>
Therefore, it is obviously impossible to obtain the text value through. value. You can use the following method:
Var chk_value = document. getElementById ("chk1"). nextSibling. innerText;

How can I obtain the CheckBox VALUE IN THE aspxcs file ???

The obtained Text is ckbFieldService. Text.
Obtain whether ckbFieldService. Checked = true
Write in the cs File
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
String str = ckbFieldService. Text // obtain the check box Text "required"
String a = "";
If (ckbFieldService. Checked = true)
{
A = "1 ";
}
Else
{
A = "2 ";
} // This is whether the check box is selected, true is selected, false is not selected. If it is selected, a = 1, otherwise a = 2
}

Do you understand this?

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.