当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET 设计中的 N 个技巧

ASP.NET
C# Quoted-Printable编码、解码
asp.net Google的translate工具翻译 API
.NET读取所有目录下文件正则匹配文本电子邮件
asp.net 半角全角转化工具
AjaxControlToolKit 显示浏览者本地语言的方法
asp.net HTML文件上传标签
ASP.NET 绑定DataSet中的多个表
ASP.NET 重定向的几种方法小结
Asp.Net 重定向必须要知道的一些资料
ASP.NET 导出到Excel时保留换行的代码
Asp.Net Cache缓存使用代码
document.getElementsByName和document.getElementById 在IE与FF中不同实现
GridView单元格合并
asp.net 大文件上传控件
asp.net 日期函数 某月的第一天和最后一天的日期
asp.net 时间类 一周的周一和周末的日期
C# javaScript函数的相互调用
asp.net membership 密码重设
"虚拟路径"..."映射到另一个应用程序,这是不允许的!
如何传值在2个页面之间 要求不刷新父页面,并且不能用Querystring传值

ASP.NET 设计中的 N 个技巧


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 100 ::
收藏到网摘: n/a

asp.net中DataGrid双行跨列表头设计心得!
一、DataGrid的属性设置
  1. AllowPaging: true
  2. PageStyle->Position: TopAndBottom
3. 可选:PageStyle->HorizonAlign: Center(使文本居中显示)
  4. 可选:ItemStyle->HorizonAlign: Center(使文本居中显示)
二、代码部分
  1.首先,使DataGrid绑定数据库中某个表,例如:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SqlConnection myConn=new SqlConnection("server=localhost;uid=sa;pwd=sa;database=db_test");
SqlDataAdapter da=new SqlDataAdapter("Select * from 个人",myConn);
DataSet ds=new DataSet();
da.Fill(ds,"gr");
dgGeRen.DataSource=ds.Tables["gr"].DefaultView;
dgGeRen.DataBind();


}
  2.为DataGrid添加ItemCreated事件的处理函数、
  3.为了判断DataGrid中的两个(上下)Pager的位置,我们可以使用一个全局变量来判断。
定义一个全局变量 private int m_CreatePageTimes = 0;
  4.为DataGrid的ItemCreated事件的处理函数添加内容,如下:
private void dgGeRen_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
//case(ListItemType.Pager):
case ListItemType.Pager:
{
if(m_CreatePageTimes == 0)
{
DataGridItem row=(DataGridItem)e.Item;
row.Cells.Clear();

//row.BackColor=Color.Navy; //背景色
//row.ForeColor=Color.Red; //前景色

row.HorizontalAlign=HorizontalAlign.Center;//使文本居中显示

TableCell cell0=new TableCell();
cell0.RowSpan=2;
cell0.Controls.Add(new LiteralControl("姓名"));

TableCell cell1=new TableCell();
cell1.ColumnSpan=2;  //默认的ColumnSpan值为1
cell1.Text="住房地址信息";

//也可如此:cell1.Controls.Add(new LiteralControl("住房地址信息"));

//TableCell cell2=new TableCell();
//cell2.Controls.Add(new LiteralControl(""));
TableCell cell2=new TableCell();
cell2.RowSpan=2;
cell2.Text="出生日期";

row.Cells.Add(cell0);
row.Cells.Add(cell1);
row.Cells.Add(cell2);


m_CreatePageTimes++;
}
break;
}
case ListItemType.Header:
{
DataGridItem head=(DataGridItem)e.Item;
head.Cells.Clear();

//head.VerticalAlign=VerticalAlign.Middle;
//head.HorizontalAlign=HorizontalAlign.Center;

//TableCell cell00=new TableCell();
//cell00.RowSpan=2;
//cell00.Text="姓名";

TableCell cell01=new TableCell();
cell01.Text="楼号";

TableCell cell02=new TableCell();
cell02.Text="房号";

//TableCell cell03=new TableCell();
//cell03.Text="出生日期";

//head.Cells.Add(cell00);
head.Cells.Add(cell01);
head.Cells.Add(cell02);
//head.Cells.Add(cell03);
break;
}

}

}

屏蔽CTRL-V


在WinForm中的TextBox控件没有办法屏蔽CTRL-V的剪贴板粘贴动作,如果需要一个输入框,但是不希望用户粘贴剪贴板的内容,可以改用RichTextBox控件,并且在KeyDown中屏蔽掉CTRL-V键,例子:

private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.Control & e.KeyCode==Keys.V)
e.Handled = true;

--------------------------------------------------------------------------------
 
 Panel 横向滚动,纵向自动扩展
<asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>

回车转换成Tab

<script language="javascript" for="document" event="onkeydown">
 if(event.keyCode==13 & event.srcElement.type!=’button’ & event.srcElement.type!=’submit’& event.srcElement.type!=’reset’ & event.srcElement.type!=’’& event.srcElement.type!=’textarea’);
   event.keyCode=9;
</script>

onkeydown="if(event.keyCode==13) event.keyCode=9"

DataGrid行随鼠标变色

private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 if (e.Item.ItemType!=ListItemType.Header)
 {
  e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=\""+e.Item.Style["BACKGROUND-COLOR"]+"\"");
  e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=\""+ "#EFF3F7"+"\"");
 }
}

改变列的选定,实现全选或全不选。
protected void CheckAll_CheckedChanged(object sender, System.EventArgs e)
{ 
 CheckBox chkExport ;
 if( CheckAll.Checked)
 {
  foreach(DataGridItem oDataGridItem in MyDataGrid.Items)
  {
   chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
   chkExport.Checked = true;
  }
 }
 else
 {
  foreach(DataGridItem oDataGridItem in MyDataGrid.Items)
  {
   chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
   chkExport.Checked = false;
  }
 }
}
数字格式化

【<%#Container.DataItem("price")%>的结果是500.0000,怎样格式化为500.00?】

<%#Container.DataItem("price","{0:¥#,##0.00}")%>

int i=123456;
string s=i.ToString("###,###.00");

日期格式化

  【aspx页面内:<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date")%>

  显示为: 2004-8-11 19:44:28

  我只想要:2004-8-11 】
<%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%>


怎样作到HyperLinkColumn生成的连接后,点击连接,打开新窗口?

  

datagrid选定比较底下的行时,为什么总是刷新一下,然后就滚动到了最上面,刚才选定的行因屏幕的关系就看不到了。

page_load
page.smartNavigation=true


在Datagrid中修改数据,当点击编辑键时,数据出现在文本框中,怎么控制文本框的大小 ?

private void DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e)
{
 for(int i=0;i<e.Item.Cells.Count-1;i++)
  if(e.Item.ItemType==ListItemType.EditType)
  {
   e.Item.Cells[i].Attributes.Add("Width", "80px")
  }
}

asp.net的多行TextBox随内容增加自动增高而不显示滚动条!