Add the radio (single choice button) column to the DataGrid

Source: Internet
Author: User

<P> // datagridradio. aspx <br> </P>
<% @ Page Language = "C #" codebehind = "datagridradio. aspx. cs" autoeventwireup = "false" inherits = "sample. datagridradio" %>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = generator>
<Meta content = C # name = code_language>
<Meta content = JavaScript name = vs_defaultclientscript>
<Meta content = http://schemas.microsoft.com/intellisense/ie5 name = vs_targetschema>
<Form ID = form1 method = post runat = "server">
<Input id = RD type = hidden name = RD runat = "server"> <asp: dataGrid id = itemsgrid runat = "server" onpageindexchanged = "grid_change" autogeneratecolumns = "false" allowpaging = "true" cellpadding = "3" borderwidth = "1" bordercolor = "black">
<Headerstyle backcolor = "#00 aaaa">
</Headerstyle>
<Pagerstyle mode = "numericpages"> </pagerstyle>
<Columns> <asp: templatecolumn> <itemtemplate> <input type = radio value = '<% # databinder. eval (container. dataitem, "integervalue") %> 'name = radioname> </itemtemplate> </ASP: templatecolumn> <asp: boundcolumn datafield = "integervalue" headertext = "Number Column"> </ASP: boundcolumn> <asp: boundcolumn datafield = "stringvalue" headertext = "string column"> </ASP: boundcolumn> <asp: boundcolumn datafield = "currencyvalue" headertext = "currency column" dataformatstring = "{0: c} "> <itemstyle horizontalalign =" right "> </itemstyle> </ASP: boundcolumn> </columns> </ASP: DataGrid> <br> <asp: button id = BTN onclick = btnclick runat = "server" text = "see what you choose"> </ASP: button> <asp: label id = label1 runat = "server" text = ""> </ASP: Label>
</Form>

// Datagridradio. aspx. CS

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace sample
{
/// <Summary>
/// Summary of the datagridradio.
/// </Summary>
Public class datagridradio: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. DataGrid itemsgrid;
Protected system. Web. UI. webcontrols. Button BTN;
Protected system. Web. UI. webcontrols. Label label1;
Protected system. Web. UI. htmlcontrols. htmlinputhidden RD;

Icollection createdatasource ()
{
Datatable dt = new datatable ();
Datarow Dr;
DT. Columns. Add (New datacolumn ("integervalue", typeof (int32 )));
DT. Columns. Add (New datacolumn ("stringvalue", typeof (string )));
DT. Columns. Add (New datacolumn ("currencyvalue", typeof (double )));
For (INT I = 0; I <101; I ++)
{
Dr = DT. newrow ();
Dr [0] = I;
Dr [1] = "item" + I. tostring ();
Dr [2] = 1.23 * (I + 1 );
DT. Rows. Add (DR );
}
Dataview DV = new dataview (DT );
Return DV;
}

Private void page_load (Object sender, system. eventargs E)
{
// Place the user here Code To initialize the page
String JS = "";
JS + = "<SCRIPT> \ r \ n ";
JS + = "function LD () {\ r \ n ";
JS + = "for (I = 0; I <document. getelementsbyname ('radioname'). length; I ++) \ r \ n ";
JS + = "If (document. getelementsbyname ('radioname') [I]. value = ";
JS + = "document. getelementbyid ('" + RD. clientid + "'). Value )";
JS + = "document. getelementsbyname ('radioname') [I]. Checked = true \ r \ n ";
JS + = "} \ r \ n"; JS + = "window. onload = LD \ r \ n ";
JS + = "</" + "script> \ r \ n ";
This. registerstartupscript ("JS", JS );
If (! Ispostback)
{
Itemsgrid. datasource = createdatasource ();
Itemsgrid. databind ();
}

}

Public void grid_change (Object sender, datagridpagechangedeventargs E)
{
Itemsgrid. currentpageindex = E. newpageindex;
Itemsgrid. datasource = createdatasource ();
Itemsgrid. databind ();
}

Public void btnclick (Object sender, eventargs E)
{
If (request. Form ["radioname"]! = NULL)
{
Rd. value = request. Form ["radioname"]. tostring ();
Label1.text = "your choice is: <font color = Red>" + request. Form ["radioname"]. tostring () + "</font> ";
}
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion
}
}

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.