首页 > 资讯 > 网站设计
ASP.NET 设计中的 N 个技巧
发布时间:2008-05-29   浏览次数:1151249
ASP.NET 设计中的 N 个技巧 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")%>   显示为: 04-8-11 19:44:28   我只想要:04-8-11 】 <%# DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}")%> 怎样作到HyperLinkColumn生成的连接后,点击连接,打开新窗口?    HyperLinkColumn有个属性Target,将器值设置成"_blank"即可.(Target="_blank") 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随内容增加自动增高而不显示滚动条!
业务咨询

Copyright 2003 - 2023 huinet.cn All Rights Reserved.
慧网公司 版权所有