Home Web Front-end JS Tutorial How to use jQuery to achieve the folding and stretching effect of ASP.NET GridView_jquery

How to use jQuery to achieve the folding and stretching effect of ASP.NET GridView_jquery

May 16, 2016 pm 03:38 PM
asp.net jquery

There is a requirement when making a static page today, that is, there is a set of radio buttons with two options and a list of 6 rows on the page (the list is implemented with the Table tag, not DIV). When the option of the radio button is selected For a moment, the first three pieces of information in the list are displayed and the last three pieces of information are hidden. When option 2 of the radio button is selected, the first three pieces of information in the list are hidden and the last three pieces of information are displayed. So that brings us to today’s topic, how to achieve it? After implementation, what other scenarios can this implementation be applied to?

1. First response solution

After encountering this requirement, my first reaction was that it was very simple. Use two DIVs to wrap the TR tags in the first three Tables and the last three TR tags, and then control the display of the DIVs through JS.

Step one: Use DIV wrapping to hide the displayed TR. The code is as follows:

<table> 
<div id="divName"> 
<tr> 
<td>姓名:</td> 
<td><input id="txtname" type="text" /></td> 
</tr> 
</div> 
<div id="divSex"> 
<tr> 
<td>年龄:</td> 
<td><input id="txtsex" type="text" /></td> 
</tr> 
</div> 
</table> 
Copy after login

Step 2: Use JS to control the display of DIV to achieve the effect of hiding or showing rows:

$("#divName").style.display = "none";
$("#divSex").style.display = "block";
Step 3: Run the program, you will find that it doesn’t work at all, haha, it feels a bit like being fooled~! Because the TR tag can only be used with the TABLE tag! Ok, although the above code doesn’t work! But it still plays a guiding role. Failure is a success!

2. Recommended panel solution

This is after I described that DIV and TR cannot be used together, and I was laughed at by my colleagues. Hey, it seems that I need to learn more about HTML in the future. After laughing at me, my colleague Dong Ning told me to wrap TR with PANEL control. , use the Visible attribute to control the output of TR at the server level.

Step one: Use the PANEL control to wrap the TR tag used to show or hide. The code is as follows:

<table> 
<asp:Panel ID="plName" runat="server"> 
<tr> 
<td>姓名:</td> 
<td><input id="txtname" type="text" /></td> 
</tr> 
</asp:Panel> 
<asp:Panel ID="plSex" runat="server" > 
<tr> 
<td>年龄:</td> 
<td><input id="txtsex" type="text" /></asp:Panel></td> 
</tr> 
</asp:Panel> 
</table> 
Copy after login

Step 2: Use the Visible property of the Panel control on the server side to control the output of the rows. The code is as follows:

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
 string val = RadioButtonList1.SelectedValue; 
 switch (val) 
 { 
  case "Name": 
   plName.Visible = true; 
   plSex.Visible = false; 
   break; 
  case "Sex": 
   plName.Visible = false; 
   plSex.Visible = true; 
   break; 
  default: 
   plName.Visible = true; 
   plSex.Visible = true; 
   break; 
 } 
} 
Copy after login

Although there is nothing wrong with this method, it still feels too nonsensical, right? Should the code that controls page display also be done on the server side? What a waste of performance! Moreover, putting the page control code and the logical interaction code together is simply confusing. When this method was rejected, our hero Comrade Wai Wai appeared on the stage and said that I have to admire Comrade Wai Wai. As a project manager, Wai Wai Weird, my coding skills are even better than those of a programmer. No code prompts are needed at all. Just manual keyboard typing and clear thinking solve the problem perfectly!

3. Nonsensical solution

So, let’s look at this idea. First, assign a class style to each TR tag. However, this style is not implemented. It only obtains the identifier of the TR.

Step one: Add an unimplemented class style to the TR tag. The code is as follows:

<table id="MyList"> 
<tr class="NameCSS"> 
<td>姓名:</td> 
<td><input id="txtname" type="text" /></td> 
</tr> 
<tr class="SexCss"> 
<td>年龄:</td> 
<td><input id="txtsex" type="text" /></td> 
</tr> 
</table> 
Copy after login

Step 2: Use Jquery to get the TR element based on class and control its hiding or display. The code is as follows:

var $rowsName = $("#MyList").find(".NameCSS"); 
var $rowsSex = $("#MyList").find(".SexCss"); 
switch (selectedValue) 
{ 
 case "Name": 
 $rowsSex.hide(); 
 $rowsName.show(); 
 break; 
 case "Sex": 
 $rowsSex.show(); 
 $rowsName.hide(); 
 break; 
} 
Copy after login

Step 3: Run, there is no problem at all, this problem is solved!

4. Implement Lenovo’s application scenarios according to the third solution

Now that we can control the display and hiding of TR in TABLE, we can think of the data part of the ASP.NET GridView control that is output to the browser after binding data, and is also displayed in the form of TR, so we can Can you control the display and hiding of GridView content? Of course no problem.

Step one: How to add class attributes to GridView data rows? We can use the GridView's row style () to set it. The code is as follows:

<asp:GridView ID="GridView1" runat="server"> 
<RowStyle CssClass="test" /> 
</asp:GridView> 
Copy after login

At this point, when we run the page and view the source code output by the page, we will see that all TRs in the GridView data part have been given a class="test" attribute!

Step 2: Bind data, the code is as follows:

if (!IsPostBack) 
  { 
   List<Student> sList = new List<Student>() 
   { 
    new Student(){ SID = "s001", SName="张三", SSex="男"}, 
    new Student(){ SID = "s002", SName="李四", SSex="女"}, 
    new Student(){ SID = "s003", SName="王五", SSex="男"} 
   }; 
 
   GridView1.DataSource = sList; 
   GridView1.DataBind(); 
  } 
 } 
Copy after login

Step 3: Add a button to control the display or hiding of GridView data. The code is as follows:

<input id="btn" type="button" value="隐藏" onclick="ShowDate()" /> 
Copy after login

Step 4: Implement the JS method to control display and hiding. The code is as follows:

function ShowDate() { 
   var val = $("#btn").val(); 
   var $rows = $("#GridView1").find(".test"); 
   switch (val) { 
    case "隐藏": 
     $rows.hide(); 
     $("#btn").val("显示"); 
     break; 
    case "显示": 
     $rows.show(); 
     $("#btn").val("隐藏"); 
     break; 
   } 
  } 
Copy after login

Haha, that’s it for introducing the reasons, characters, inspirations, and the entire cause and effect of realizing this function. Programming is not only about realizing functions, but also integrating into life.

The above four methods are closely connected and related to each other. I hope everyone can taste them carefully, ponder them carefully, truly make them your own, and apply them to your studies.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

In-depth analysis: jQuery's advantages and disadvantages In-depth analysis: jQuery's advantages and disadvantages Feb 27, 2024 pm 05:18 PM

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

See all articles