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

news/2024/7/5 19:34:05
function StorePage() { d=document; t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():''); void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }

#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.οnlοad=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://www.niftyadmin.cn/n/605997.html

相关文章

mysql主从复制故障处理_MySQL主从复制及遇到问题的解决

超简单的MySQL主从复制&#xff0c;怎么安装MySQL数据库&#xff0c;这里不说了&#xff0c;只说它的主从复制&#xff0c;步骤如下&#xff1a;1、主从服务器分别作以下操作&#xff1a;1.1、版本一致1.2、初始化表&#xff0c;并在后台启动mysql1.3、修改root的密码2、修改主…

maven私服nexus上传第三方jar包以及下载

私服是一个特殊的远程仓库&#xff0c;它是架设在局域网内的仓库服务。私服代理广域网上的远程仓库&#xff0c;供局域网内的Maven用户使用。当Maven需要下载构建的使用&#xff0c;它先从私服请求&#xff0c;如果私服上没有的话&#xff0c;则从外部的远程仓库下载&#xff0…

DataGrid模板列应用——在DataGrid中用CheckBox控制TextBox的Enabled属性

将模板列的CheckBox的AutoPostBack属性设置为true&#xff0c;因为在模板列中的控件没有事件&#xff0c;故在aspx页面写事件程序&#xff0c;同时将CheckBox的OnCheckedChanged事件设置为所需要的事件程序。示例如下&#xff1a; void Check_Change(Object sender, EventArgs …

单幅图像处理的基类

2019独角兽企业重金招聘Python工程师标准>>> <!-- lang: c# --> /// <summary> /// 计算核 /// </summary> abstract class Calc {/// <summary>/// 获取一个实例/// </summary>/// <returns></returns>public static Ca…

Python初识

Python初识 Python的注释方法简介 Python的注释方法有三种&#xff1a; 单行注释使用 #&#xff1b;多行注释使用 “”""""或者’’’’’’; 数据类型 目前了解了四种Python的数据类型&#xff1a; 整数类型&#xff08;int&#xff09; &#xff0…

python 基类是什么_确定Python类是抽象基类还是Con

我的Python应用程序包含许多抽象类和实现。例如&#xff1a;import abcimport datetimeclass MessageDisplay(object):__metaclass__ abc.ABCMetaabc.abstractpropertydef display(self, message):passclass FriendlyMessageDisplay(MessageDisplay):def greet(self):hour da…

ssh通过pem文件登陆服务器

一些为了安全操作&#xff0c;推荐使用私钥进行登录服务器&#xff0c;拿jenkins来说&#xff0c;默认的验证方式就是私钥实现方式 先在本机通过ssh-keygen直接生成公私钥如下在当前文件夹下生成my.pem(私钥)和my.pem.pub(公钥)ssh-keygen -t rsa -f my.pem -C "youremail…

.net笔试题

姓名&#xff1a; 日期&#xff1a; 1. 填空: (1)面向对象的语言具有________性、_________性、________性。 (2)能用foreach遍历访问的对象需要实现 ________________接口或声明________________方法的类型。 (3)列举ADO.net中的五个主要对象_______________、_____________、…