天天看點

ASP.NET程式中常用代碼彙總(三)

21.如何設定全局變量

ASP.NET程式中常用代碼彙總(三)

Global.asax中

ASP.NET程式中常用代碼彙總(三)

  Application_Start()事件中

ASP.NET程式中常用代碼彙總(三)

  添加Application[屬性名] = xxx;

ASP.NET程式中常用代碼彙總(三)

  就是你的全局變量

ASP.NET程式中常用代碼彙總(三)

22.怎樣作到HyperLinkColumn生成的連接配接後,點選連接配接,打開新視窗?

ASP.NET程式中常用代碼彙總(三)

HyperLinkColumn有個屬性Target,将器值設定成"_blank"即可.(Target="_blank")

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  【ASPNETMENU】點選菜單項彈出新視窗

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  在你的menuData.xml檔案的菜單項中加入URLTarget="_blank",如:

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

<?xml version="1.0" encoding="GB2312"?>

ASP.NET程式中常用代碼彙總(三)

<MenuData ImagesBaseURL="images/"> 

ASP.NET程式中常用代碼彙總(三)

<MenuGroup>

ASP.NET程式中常用代碼彙總(三)

<MenuItem Label="内參資訊" URL="Infomation.aspx" >

ASP.NET程式中常用代碼彙總(三)

<MenuGroup ID="BBC">

ASP.NET程式中常用代碼彙總(三)

<MenuItem Label="公告資訊" URL="Infomation.aspx" URLTarget="_blank" LeftIcon="file.gif"/>

ASP.NET程式中常用代碼彙總(三)

<MenuItem Label="編制資訊簡報" URL="NewInfo.aspx" LeftIcon="file.gif" />

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  最好将你的aspnetmenu更新到1.2版

23.讀取DataGrid控件TextBox值

ASP.NET程式中常用代碼彙總(三)

foreach(DataGrid dgi in yourDataGrid.Items)

ASP.NET程式中常用代碼彙總(三)

{

ASP.NET程式中常用代碼彙總(三)

 TextBox tb = (TextBox)dgi.FindControl("yourTextBoxId");

ASP.NET程式中常用代碼彙總(三)

 tb.Text

ASP.NET程式中常用代碼彙總(三)

.

ASP.NET程式中常用代碼彙總(三)

}

24.在DataGrid中有3個模闆列包含Textbox分别為 DG_ShuLiang (數量) DG_DanJian(單價) DG_JinE(金額)分别在5.6.7列,要求在錄入數量及單價的時候自動算出金額即:數量*單價=金額還要求錄入時限制為 數值型.我如何用用戶端腳本實作這個功能?

ASP.NET程式中常用代碼彙總(三)

<asp:TemplateColumn HeaderText="數量"> 

ASP.NET程式中常用代碼彙總(三)

<ItemTemplate>

ASP.NET程式中常用代碼彙總(三)

<asp:TextBox id="ShuLiang" runat=’server’ Text=’<%# DataBinder.Eval(Container.DataItem,"DG_ShuLiang")%>’ 

ASP.NET程式中常用代碼彙總(三)

onkeyup="javascript:DoCal()"

ASP.NET程式中常用代碼彙總(三)

/>

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

<asp:RegularExpressionValidator id="revS" runat="server" ControlToValidate="ShuLiang" ErrorMessage="must be integer" ValidationExpression="^\d+$" />

ASP.NET程式中常用代碼彙總(三)

</ItemTemplate>

ASP.NET程式中常用代碼彙總(三)

</asp:TemplateColumn>

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

<asp:TemplateColumn HeaderText="單價"> 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

<asp:TextBox id="DanJian" runat=’server’ Text=’<%# DataBinder.Eval(Container.DataItem,"DG_DanJian")%>’ 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

<asp:RegularExpressionValidator id="revS2" runat="server" ControlToValidate="DanJian" ErrorMessage="must be numeric" ValidationExpression="^\d+(\.\d*)?$" />

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

<asp:TemplateColumn HeaderText="金額"> 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

<asp:TextBox id="JinE" runat=’server’ Text=’<%# DataBinder.Eval(Container.DataItem,"DG_JinE")%>’ />

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

</asp:TemplateColumn><script language="javascript">

ASP.NET程式中常用代碼彙總(三)

function DoCal()

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 var e = event.srcElement;

ASP.NET程式中常用代碼彙總(三)

 var row = e.parentNode.parentNode;

ASP.NET程式中常用代碼彙總(三)

 var txts = row.all.tags("INPUT");

ASP.NET程式中常用代碼彙總(三)

 if (!txts.length || txts.length < 3)

ASP.NET程式中常用代碼彙總(三)

  return;

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 var q = txts[txts.length-3].value;

ASP.NET程式中常用代碼彙總(三)

 var p = txts[txts.length-2].value;

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 if (isNaN(q) || isNaN(p))

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 q = parseInt(q);

ASP.NET程式中常用代碼彙總(三)

 p = parseFloat(p);

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 txts[txts.length-1].value = (q * p).toFixed(2);

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

</script>

25.datagrid標明比較底下的行時,為什麼總是重新整理一下,然後就滾動到了最上面,剛才標明的行因螢幕的關系就看不到了。

       page_load 

   page.smartNavigation=true

26.在Datagrid中修改資料,當點選編輯鍵時,資料出現在文本框中,怎麼控制文本框的大小 ? 

ASP.NET程式中常用代碼彙總(三)

private void DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e)

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 for(int i=0;i<e.Item.Cells.Count-1;i++)

ASP.NET程式中常用代碼彙總(三)

  if(e.Item.ItemType==ListItemType.EditType)

ASP.NET程式中常用代碼彙總(三)

  {

ASP.NET程式中常用代碼彙總(三)

   e.Item.Cells[i].Attributes.Add("Width", "80px")

ASP.NET程式中常用代碼彙總(三)

  } 

ASP.NET程式中常用代碼彙總(三)

27.對話框

ASP.NET程式中常用代碼彙總(三)

private static string ScriptBegin = "<script language=\"JavaScript\">";

ASP.NET程式中常用代碼彙總(三)

private static string ScriptEnd = "</script>";

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

public static void ConfirmMessageBox(string PageTarget,string Content)

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 string ConfirmContent="var retValue=window.confirm(’"+Content+"’);"+"if(retValue){window.location=’"+PageTarget+"’;}";

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 ConfirmContent=ScriptBegin + ConfirmContent + ScriptEnd;

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 Page ParameterPage = (Page)System.Web.HttpContext.Current.Handler;

ASP.NET程式中常用代碼彙總(三)

 ParameterPage.RegisterStartupScript("confirm",ConfirmContent);

ASP.NET程式中常用代碼彙總(三)

 //Response.Write(strScript);

ASP.NET程式中常用代碼彙總(三)

28.将時間格式化:string aa=DateTime.Now.ToString("yyyy年MM月dd日"); 

ASP.NET程式中常用代碼彙總(三)

   1.1 取目前年月日時分秒 

ASP.NET程式中常用代碼彙總(三)

currentTime=System.DateTime.Now;

ASP.NET程式中常用代碼彙總(三)

  1.2 取目前年 

ASP.NET程式中常用代碼彙總(三)

int 年= DateTime.Now.Year;

ASP.NET程式中常用代碼彙總(三)

  1.3 取目前月 

ASP.NET程式中常用代碼彙總(三)

int 月= DateTime.Now.Month; 

ASP.NET程式中常用代碼彙總(三)

  1.4 取目前日 

ASP.NET程式中常用代碼彙總(三)

int 日= DateTime.Now.Day; 

ASP.NET程式中常用代碼彙總(三)

  1.5 取目前時 

ASP.NET程式中常用代碼彙總(三)

int 時= DateTime.Now.Hour; 

ASP.NET程式中常用代碼彙總(三)

  1.6 取目前分 

ASP.NET程式中常用代碼彙總(三)

int 分= DateTime.Now.Minute; 

ASP.NET程式中常用代碼彙總(三)

  1.7 取目前秒 

ASP.NET程式中常用代碼彙總(三)

int 秒= DateTime.Now.Second; 

ASP.NET程式中常用代碼彙總(三)

  1.8 取目前毫秒 

 int 毫秒= DateTime.Now.Millisecond;

29.自定義分頁代碼

ASP.NET程式中常用代碼彙總(三)

//先定義變量 :

ASP.NET程式中常用代碼彙總(三)

public static int pageCount; //總頁面數 

ASP.NET程式中常用代碼彙總(三)

public static int curPageIndex=1; //目前頁面 

ASP.NET程式中常用代碼彙總(三)

  //下一頁: 

ASP.NET程式中常用代碼彙總(三)

if(DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1)) 

ASP.NET程式中常用代碼彙總(三)

ASP.NET程式中常用代碼彙總(三)

 DataGrid1.CurrentPageIndex += 1; 

ASP.NET程式中常用代碼彙總(三)

 curPageIndex+=1; 

ASP.NET程式中常用代碼彙總(三)

ASP.NET程式中常用代碼彙總(三)

bind(); // DataGrid1資料綁定函數 

ASP.NET程式中常用代碼彙總(三)

  //上一頁: 

ASP.NET程式中常用代碼彙總(三)

if(DataGrid1.CurrentPageIndex >0) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 curPageIndex-=1; 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  //直接頁面跳轉: 

ASP.NET程式中常用代碼彙總(三)

int a=int.Parse(JumpPage.Value.Trim());//JumpPage.Value.Trim()為跳轉值 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

if(a<DataGrid1.PageCount) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 this.DataGrid1.CurrentPageIndex=a; 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

bind(); 

ASP.NET程式中常用代碼彙總(三)

30.DataGrid使用

ASP.NET程式中常用代碼彙總(三)

//添加删除确認: 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 foreach(DataGridItem di in this.DataGrid1.Items) 

ASP.NET程式中常用代碼彙總(三)

 { 

ASP.NET程式中常用代碼彙總(三)

  if(di.ItemType==ListItemType.Item||di.ItemType==ListItemType.AlternatingItem) 

ASP.NET程式中常用代碼彙總(三)

  { 

ASP.NET程式中常用代碼彙總(三)

   ((LinkButton)di.Cells[8].Controls[0]).Attributes.Add("onclick","return confirm(’确認删除此項嗎?’);"); 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 } 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  //樣式交替: 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

ListItemType itemType = e.Item.ItemType; 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

if (itemType == ListItemType.Item ) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#FFFFFF’;"; 

ASP.NET程式中常用代碼彙總(三)

 e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor=’#d9ece1’;cursor=’hand’;" ; 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

else if( itemType == ListItemType.AlternatingItem) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=’#a0d7c4’;"; 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  //添加一個編号列: 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

DataTable dt= c.ExecuteRtnTableForAccess(sqltxt); //執行sql傳回的DataTable 

ASP.NET程式中常用代碼彙總(三)

DataColumn dc=dt.Columns.Add("number",System.Type.GetType("System.String")); 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

for(int i=0;i<dt.Rows.Count;i++) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 dt.Rows[i]["number"]=(i+1).ToString(); 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

DataGrid1.DataSource=dt; 

ASP.NET程式中常用代碼彙總(三)

DataGrid1.DataBind(); 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  //DataGrid1中添加一個CheckBox,頁面中添加一個全選框 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

private void CheckBox2_CheckedChanged(object sender, System.EventArgs e) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 foreach(DataGridItem thisitem in DataGrid1.Items) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  ((CheckBox)thisitem.Cells[0].Controls[1]).Checked=CheckBox2.Checked; 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 // 将目前頁面中DataGrid1顯示的資料全部删除 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

foreach(DataGridItem thisitem in DataGrid1.Items) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

 if(((CheckBox)thisitem.Cells[0].Controls[1]).Checked) 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

  string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString(); 

ASP.NET程式中常用代碼彙總(三)

  Del (strloginid); //删除函數 

ASP.NET程式中常用代碼彙總(三)
ASP.NET程式中常用代碼彙總(三)

本文轉自高海東部落格園部落格,原文連結:http://www.cnblogs.com/ghd258/archive/2005/11/03/268365.html,如需轉載請自行聯系原作者

繼續閱讀