两个DropDownList在Repeater、DataList、DataGrid内的连动和冒泡事件(BubbleEvent)
问题: http://community.csdn.net/Expert/topic/4670/4670056.xml?temp=3.944033E-02
刚看第一眼觉得在第一个DDL的SelectedIndexChanged事件内绑定第二个DDL就ok了,页面上写或者ItemDataBound内写事件挂接代码都可以。打开VS写测试却发现有点障碍哦,
在SelectedIndexChanged内要知道去绑定哪个行哪个列的DDL要费些周折。
只有从DDL一级一级的向上找Parent最后找到DataGridItem,再利用其ItemIndex定位到行然后FindControl到要绑定的DDL,最终可以实现,好像比较累哦。
随又想到DataGrid的ItemCommand事件,但发现WebControl内只有Button ImageButton LinkButton有CommandName属性和 CommandArgument属性,并可以将事件上浮(冒泡).DropDownList先天不足!真是郁闷~~~~~ 能不能自己改造呢?动手试试就知道了。
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="gb2312" lang="gb2312">
<head>
<title> ItemDataBoundGetColumnName </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="title" content="" />
<meta name="author" content="活靶子,Huobazi,www.AspxBoy.com" />
<meta name="subject" content="" />
<meta name="language" content="gb2312" />
<meta name="keywords" content="" />
<meta name="Copyright" content="www.AspxBoy.com" />
<meta name="robots" content="all" />
<script language="c#" runat="server">
void BindGrid()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 0; i < 36; i++) {
dr = dt.NewRow();
dr[0] = i;
dr[1] = "项 " + i.ToString();
dr[2] = DateTime.Now;
dr[3] = (i % 2 != 0) ? true : false;
dr[4] = 1.23 * (i+1);
dt.Rows.Add(dr);
}
dg.DataSource= dt;
dg.DataBind();
}
void Page_Load(object o, EventArgs e)
{
dg.PagerStyle.Mode = PagerMode.NumericPages;
if(!IsPostBack)
{
BindGrid();
}
}
void btnClick(object o , EventArgs e)
{
Response.Write("页面回发,但是不执行ItemDataBound");
}
void PageChange(object o , DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
void ItemDataBound(object o , DataGridItemEventArgs e)
{
if(e.Item.ItemIndex > -1 )//必须
{
DataRowView drv = (DataRowView)e.Item.DataItem;
for (int i=0; i<drv.Row.Table.Columns.Count ; i++)
{
Response.Write(drv.Row.Table.Columns[i].ColumnName + " ");
}
}
}
</script>
</head>
<body>
<form runat="server">
<ASP:DataGrid id="dg" runat="server"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="true"
AllowPaging="true"
PageSize="6"
OnItemDataBound="ItemDataBound"
OnPageIndexChanged="PageChange"
>
</asp:DataGrid>
</br>
<asp:button id="btn" runat="server" onclick="btnClick" Text="我是按钮,按我一下"></asp:button>
在AutoGenerateColumns="false"使用绑定列或者模板列的时候只需要使用
<font color="blut">YourDataGrid.Columns[编号].HeaderText</font> 去获取
</form>
</body>
</html>
http://www.aspxboy.com/Files/71/66/284.Aspx
posted on
November 3, 2004
如果 AutoGenerateColumns="false" 使用绑定列,模板列
则比较好办,直接使用 YourDataGrid.Columns[编号].HeaderText 获得
可是,如果 AutoGenerateColumns="true" ,就比较麻烦了,因为这时YourDataGrid.Columns.Count是0
但是可以变通做到。
示例:
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="gb2312" lang="gb2312">
<head>
<title> ItemDataBoundGetColumnName </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="title" content="" />
<meta name="author" content="活靶子,Huobazi,www.AspxBoy.com" />
<meta name="subject" content="" />
<meta name="language" content="gb2312" />
<meta name="keywords" content="" />
<meta name="Copyright" content="www.AspxBoy.com" />
<meta name="robots" content="all" />
<script language="c#" runat="server">
void BindGrid()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 0; i < 36; i++) {
dr = dt.NewRow();
dr[0] = i;
dr[1] =& amp;nbsp;"项 " + i.ToString();
dr[2] = DateTime.Now;
dr[3] = (i % 2 != 0) ? true : false;
dr[4] = 1.23 * (i+1);
dt.Rows.Add(dr);
}
dg.DataSource= dt;
dg.DataBind();
}
void Page_Load(object o, EventArgs e)
{
dg.PagerStyle.Mode = PagerMode.NumericPages;
if(!IsPostBack)
{
BindGrid();
}
}
void btnClick(object o , EventArgs e)
{
Response.Write("页面回发,但是不执行ItemDataBound");
}
void PageChange(object o , DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
void ItemDataBound(object o , DataGridItemEventArgs e)
{
if(e.Item.ItemIndex > -1 )//必须
{
DataRowView drv = (DataRowView)e.Item.DataItem;
for (int i=0; i<drv.Row.Table.Columns.Count ; i++)
{
Response.Write(drv.Row.Table.Columns[i].ColumnName + " ");
}
}
}
</script>
</head>
<body>
<form runat="server">
<ASP:DataGrid id="dg" runat="server"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="true"
AllowPaging="true"
PageSize="6"
OnItemDataBound="ItemDataBound"
OnPageIndexChanged="PageChange"
>
</asp:DataGrid>
</br>
<asp:button id="btn"& amp;nbsp;runat="server" onclick="btnClick" Text="我是按钮, 按我一下"></asp:button>
在AutoGenerateColumns="false"使用绑定列或者模板列的时候只需要使用
<font color="blut"& amp;gt;YourDataGrid.Columns[编号].HeaderText</font> 去获取
</form>
</body>
</html>
http://www.aspxboy.com/Files/71/66/284.Aspx
手中的活计需要打印,就到处找找。
从google中扑获,挺不错的,大家共享~
how can i print the data in DataGrid?
http://forums.aspfree.com/archive/t-17107
http://forums.aspfree.com/t17107/s.html
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
namespace Hooooo.Print
{
public class DataGridPrinter
{
private DataGrid dataGrid;
private PrintDocument printDocument;
private PageSetupDialog pageSetupDialog;
private PrintPreviewDialog printPreviewDialog;
public DataGridPrinter(DataGrid dataGrid)
{
this.dataGrid = dataGrid;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int rowCount = 0;
int colCount = 0;
int x = 0;
int y = 0;
int rowGap = 20;
int colGap = 5;
int leftMargin = 50;
Font font = new Font("Arial", 10);
Font headingFont = new Font("Arial", 11, FontStyle.Underline);
Font captionFont = new Font("Arial", 10, FontStyle.Bold);
Brush brush = new SolidBrush(Color.Black);
string cellValue = "";
if(dataGrid.DataSource.GetType().ToString() == "System.Data.DataTable")
{
rowCount = ((DataTable)dataGrid.DataSource).Rows.Count;
}
else if(dataGrid.DataSource.GetType().ToString() == "System.Collections.ArrayList")
{
rowCount = ((ArrayList)dataGrid.DataSource).Count;
}
colCount = dataGrid.TableStyles[0].GridColumnStyles.Count;
//print caption
if(dataGrid.CaptionVisible)
{
y += rowGap;
x = leftMargin;
e.Graphics.DrawString(dataGrid.CaptionText, captionFont, brush, x, y);
}
//print headings
y += rowGap;
x = leftMargin;
for(int j = 0; j < colCount; j++)
{
if(dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)
{
cellValue = dataGrid.TableStyles[0].GridColumnStyles[j].HeaderText;
e.Graphics.DrawString(cellValue, headingFont, brush, x, y);
x += dataGrid.TableStyles[0].GridColumnStyles[j].Width + colGap;
}
}
//print all rows
for(int i = 0; i < rowCount; i++)
{
y += rowGap;
x = leftMargin;
for(int j = 0; j < colCount; j++)
{
if(dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)
{
cellValue = dataGrid[i,j].ToString();
e.Graphics.DrawString(cellValue, font, brush, x, y);
x += dataGrid.TableStyles[0].GridColumnStyles[j].Width + colGap;
y = y + rowGap * (cellValue.Split(new char[] {'\r', '\n'}).Length - 1);
}
}
}
string s = cellValue;
string f3 = cellValue;
}
public PrintDocument GetPrintDocument()
{
return printDocument;
}
public void Print()
{
try
{
pageSetupDialog = new PageSetupDialog();
pageSetupDialog.Document = printDocument;
pageSetupDialog.ShowDialog();
printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.Height = 600;
printPreviewDialog.Width = 800;
printPreviewDialog.ShowDialog();
}
catch(Exception e)
{
throw new Exception("Printer error." + e.Message);
}
}
}
}
需要打印表格线条及分页的朋友,可以看一下下面这篇文章: