Code Analysis-The DataGrid enables auto-increment columns, single-choice, and multiple-choice

Source: Internet
Author: User
The last time we added, deleted, modified, paginated, and dynamically modified the content for this DataGrid, we will analyze how to add auto-increment columns, single-choice, and multiple-choice features for it today.
First, let's take a look at the code that needs to be added based on the previous one:
(1) implement auto-increment columns
You only need to add a template column in the foreground and bind ItemIndex + 1.
<Asp: TemplateColumn HeaderText = "select">
<ItemTemplate>
<% # Container. ItemIndex + 1%> </ItemTemplate>
</Asp: TemplateColumn>
(2) single answer
Based on the above, add a radio for this column
<Asp: TemplateColumn HeaderText = "select">
<ItemTemplate>
<% # Container. ItemIndex + 1%>
<Input type = radio name = "rad" value = '<% # Container. ItemIndex + 1%>'>
</ItemTemplate>
</Asp: TemplateColumn>
The radio value is the auto-increment 1-PageSize.
Create a button (named "single choice") and add the following code to it:
This. Label1.Text = "selected row number:" + Request. Form ["rad"]. ToString ();
Although the selected row number is displayed in this simple step, after you press the button, the selection of radio cannot be retained because this radio is not runat = server, we need to place a hidden domain (such as runat = server) for this page to save the selected radio when clicking the button, add <input type = "hidden" id = "rd" runat = "server" name = "rd"> at the front end (note: Do not add this to the DataGrid ), then add rd. value = Request. form ["rad"]. toString (); (if vs.net does not declare this HtmlInputHidden, add protected System. web. UI. htmlControls. htmlInputHidden rd ;). The location has not been completed yet. Although the selected row number has been saved in HtmlInputHidden, you also need to set this radio to the selected status when loading the page, and change Page_Load ()
Private void Page_Load (object sender, System. EventArgs e)
{
String js = "";
Js + = "<script> ";
Js + = "function ld (){";
Js ++ = "for (I = 0; I <document. getElementsByName ('Rad '). length; I ++ )";
Js + = "if (document. getElementsByName ('Rad ') [I]. value = ";

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.