首页 > 编程 > .NET > 正文

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

2024-07-10 13:03:04
字体:
来源:转载
供稿:网友
#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)
{

}
}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表