[原创]ASP.net下DataGrid的单项选择控件

类别:.NET开发 点击:0 评论:0 推荐:

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

// 时间:2005-3-15

// [email protected]
// QQ:16503096
//注意:引用请标明出处,谢谢
//----------------------------------------------------------------------
#endregion

前台:

<%@ 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>
 <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
 <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
 <Columns>
  <asp:TemplateColumn>
   <ItemTemplate>
    &nbsp;<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>

后台:

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>
 ///  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>
  /// 窗体名
  /// </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);
  }

  #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)
  {

  }
 }
}

本文地址:http://com.8s8s.com/it/it41535.htm