Single selection control for DataGrid under ASP.net

Source: Internet
Author: User
asp.net|datagrid| Control #region Declaration
//----------------------------------------------------------------------
//
Author: Li Miao (nick.lee)
//
Single selection control for DataGrid under ASP.net

Time: 2005-3-15

Boyorgril@msn.com
qq:16503096
Note: Please indicate the origin of the reference, thank you
//----------------------------------------------------------------------
#endregion



Front desk:

<%@ control language= "C #" autoeventwireup= "false" codebehind= "WebUserControl1.ascx.cs" inherits= "Message1". WebUserControl1 "targetschema=" http://schemas.microsoft.com/intellisense/ie5 "%>
<link href= "Xptable.css" type= "Text/css" rel= "stylesheet" >
<asp:datagrid id= "DATAGRID1" runat= "Server" borderstyle= "None" borderwidth= "1px" cellpadding= "4"
Font-size= "9pt" width= "70%" bordercolor= "#3366CC" >
<selecteditemstyle font-bold= "True" forecolor= "#CCFF99" backcolor= "#009999" ></SelectedItemStyle>
<itemstyle forecolor= "#003399" backcolor= "white" ></ItemStyle>
<footerstyle forecolor= "#003399" backcolor= "#99CCCC" ></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<input type=radio value= ' <%# databinder.eval (Container.DataItem, "Volume")%> ' name=radioname>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<pagerstyle horizontalalign= "left" forecolor= "#003399" backcolor= "#99CCCC" mode= "NumericPages" ></ Pagerstyle>
</asp:DataGrid>
<asp:button id= "Button1" runat= "Server" text= "button" ></asp:button><input id= "rd" type= "hidden" name= "RD" runat= "Server" >
<asp:label id= "Label1" runat= "Server" >Label</asp:Label>


Background:

Namespace Message1
{
Using System;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.OleDb;
Using System.ComponentModel;

Using NickLee.Web.UI;

<summary>
Summary description of the WebUserControl1.
</summary>
public class WebUserControl1:System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlInputHidden Rd;
protected System.Web.UI.WebControls.Label Label1;
public DataSet custDS = new DataSet ();
protected System.Web.UI.WebControls.Button Button1;

private String sql;
public string SQL
{
Get{return SQL;}
Set{sql=value;}
}

//------------------------------------------------------------------------------------
<summary>
Form name
</summary>
System.Web.UI.Page _page;
[BrowsableAttribute (True), CategoryAttribute ("appearance"), Bindableattribute (true)]
Public System.Web.UI.Page Page
{
Get
{
return _page;
}
Set
{
_page=value;
}
}

private void Page_Load (object sender, System.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";
_page. RegisterStartupScript ("JS", JS);
Webjsutil.executestartup (_PAGE,JS);
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
This. Datagrid1.itemcommand + = new System.Web.UI.WebControls.DataGridCommandEventHandler (this. Datagrid1_itemcommand);
This. Datagrid1.itemdatabound + = new System.Web.UI.WebControls.DataGridItemEventHandler (this. Datagrid1_itemdatabound);
This. Datagrid1.selectedindexchanged + = new System.EventHandler (this. datagrid1_selectedindexchanged);
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Datagrid1_itemdatabound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
#region get the value of the primary key and check box for the current column
if (E.item.itemtype = = ListItemType.AlternatingItem | e.item.itemtype = = listitemtype.item)
{

E.item.attributes.add ("OnMouseDown", "form1.action=", "event=" + e.item.cells[2). Text.replace ("'", "" ") +" ");
String openview= "window.open ('./webform2.aspx?event=" +e.item.cells[2). Text.tostring () + "', ' double-click view Window ', ' height=230, width=300, top=200, left=200, Toolbar=no, Menubar=no, Scrollbars=no, Resizable=no,location=no, Status=no ') ";
E.item.attributes.add ("OnDblClick", OpenView);
E.ITEM.CELLS[2]. Text = "<nobr>" + e.item.cells[2]. Text + "</nobr>";
}
#endregion
}

public void SSSs ()
{
Place user code here to initialize page
String filenamestring = this. MapPath (".");
Filenamestring + = "\\chartdata.mdb";
String myconnectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + filenamestring;
String Myselectquery=sql;

Open Data Base Connection
OleDbConnection myconnection = new OleDbConnection (myconnectionstring);
OleDbCommand mycommand = new OleDbCommand (mySelectQuery, MyConnection);
MyCommand.Connection.Open ();

Fill Data Set Object
OleDbDataAdapter CUSTDA = new OleDbDataAdapter ();
Custda.selectcommand = mycommand;
Custda.fill (custDS, "Default");
MyCommand.Connection.Close ();

datagrid1.datasource=custds.tables["Default"]. DefaultView;
Datagrid1.databind ();
}

private void Datagrid1_itemcommand (object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
session["SSS"]=e.item.cells[1]. ToString ();
}

private void Button1_Click (object sender, System.EventArgs e)
{
button1.tooltip=session["SSS"]. ToString ();
if (request.form["radioname"]!= null)
{
Rd. Value = request.form["Radioname"]. ToString ();
Label1.Text = "Your choice is: <font color=red>" + request.form["Radioname"]. ToString () + "</font>";

}
Else
{
This. label1.text= "NULL";
}
}

private void DataGrid1_SelectedIndexChanged (object sender, System.EventArgs e)
{

}
}
}





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.