转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
[原创]ASP.net下DataGrid的单项选择控件         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1087 更新时间:2009/4/23 10:43:13

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

// 时间:2005-3-15

// boyorgril@msn.com
// 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)
  {

  }
 }
}


[C语言系列]NET 中C#的switch语句的语法  [系统软件]托拽Explore中的文件到VB.net的窗口
[系统软件]Boost库在XP+Visual C++.net中的安装  [常用软件]新配色面板:Paint.Net3.0RC1官方下载
[常用软件]用内建的“Net Meeting”聊天  [VB.NET程序]Henry的VB.NET之旅(三)—共享成员
[VB.NET程序]Henry的VB.NET之旅(二)—构造与析构  [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体
[VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET)  [VB.NET程序]VB.NET中关于DataGrid颜色的自定义。
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台