ASP.net下DataGrid的單項選擇控制項

來源:互聯網
上載者:User
asp.net|datagrid|控制項 #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>
 <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 Form設計器產生的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
//
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)
{

}
}
}





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。