Add a radio (radio button) column to the DataGrid

Source: Internet
Author: User
Tags tostring
datagrid| button <%@ Page language= "C #" autoeventwireup= "true" EnableViewState = "true"%>
<%@ Import namespace= "System.Data"%>
<script runat= "Server" >
DataTable Cart;
DataView Cartview;
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 < 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;
}

void Page_Load (Object sender, EventArgs e)
{
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 ();
}
}
void Grid_change (Object sender, DataGridPageChangedEventArgs e)
{
Itemsgrid.currentpageindex = E.newpageindex;
Itemsgrid.datasource = CreateDataSource ();
Itemsgrid.databind ();
}
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>";
}
}

</script>
<body>
<form runat= "Server" id=mm>
<input type= "hidden" id=rd runat=server/>
<asp:datagrid id= "Itemsgrid" runat= "Server"
Bordercolor= "Black" borderwidth= "1"
cellpadding= "3" allowpaging= "true"
Autogeneratecolumns= "false"
onpageindexchanged= "Grid_change" >
<pagerstyle mode= "NumericPages" ></PagerStyle>

<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<input type=radio name= "radioname" Value= ' <%# DataBinder.Eval (Container.DataItem, "IntegerValue")%> '/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:boundcolumn headertext= "number column" datafield= "IntegerValue"/>
<asp:boundcolumn headertext= "string column" datafield= "StringValue"/>
<asp:boundcolumn headertext= "Currency column" datafield= "Currencyvalue" dataformatstring= "{0:c}" >
<itemstyle horizontalalign= "right" ></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
<br>
<asp:button id= "Btn" text= "you choose Onclick=" Btnclick "runat=" "Server"/>
<asp:label id= "Label1" text= "" runat= "Server"/>
</form>
</body>




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.