ASP.net下DataGrid的单项选择控件

80酷酷网    80kuku.com

  asp.net|datagrid|控件#region 声明
//----------------------------------------------------------------------
//
// 作者: 李淼(Nick.Lee)
//
// ASP.net下DataGrid的单项选择控件

// 时间:2005-3-15

// boyorgrilmsn.com
// QQ:16503096
//注意:引用请标明出处,谢谢
//----------------------------------------------------------------------
#endregion



前台:

<% Control Language="c#" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.cs" Inherits="message1.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<LINK 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+="var webSiteID = 6;window.onload=ld\r\n";
// js+="</"+"script>\r\n";
// _page.RegisterStartupScript("js",js);
webJSUtil.ExecuteStartup(_page,js);
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </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 取得当前列的主键和复选框的值
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()+"', '双击查看窗口', '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()
{
// // 在此处放置用户代码以初始化页面
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 = "您所选择的是:<font color=red>" + Request.Form["RadioName"].ToString() +"</font>";

}
else
{
this.Label1.Text="NULL";
}
}

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

}
}
}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: