20070704(日常實踐收集)c#.net

自動顯示序號---分頁
前臺部分代碼:
<asp:TemplateColumn HeaderText="序號">
<HeaderStyle Width="30px"></HeaderStyle>
<ItemTemplate> <%# xuhao++%></ItemTemplate>
</asp:TemplateColumn>
後臺部分代碼:
public class viewall : System.Web.UI.Page
 {
 protected int xuhao;
 …………
 private void Page_Load(object sender, System.EventArgs e)
  {
  if(!this.IsPostBack)
   {
   xuhao=1;
   this.bind();
   }
  }
 private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
   xuhao=e.NewPageIndex*20+1;
   this.bind();
  }
 }  
*******************************
判斷日期輸入是否正確(日期正確則爲true,否則爲false):
public bool IsDate(string str)
  
  {
   try
   {
    DateTime dt=Convert.ToDateTime(str);
    return true;
   }
   catch
   {
    Response.Write("<script>alert('你輸入的日期不正確,請重新輸入');</script>");
    return false;
   }
  }
*******************************
DataGrid記錄數目和當前第幾頁的以及總頁數目顯示:
前臺部分代碼:
<asp:label id="LabPage" runat="server" Visible="False">Label</asp:label>
<asp:label id="LabRecord" runat="server" Visible="False">Label</asp:label>
<asp:label id="LabKong" runat="server" Width="414px">Label</asp:label>
後臺代碼:
private string strPage,strRecord;
private void AllBindGrid()//初始化頁面
  {
   string sqlstr="select * from View_neirong";
   SqlDataAdapter dt=new SqlDataAdapter(sqlstr,myconn);
   DataSet ds=new DataSet();
   dt.Fill(ds,"所有資料");
   this.DataGrid1.DataSource=ds.Tables["所有資料"].DefaultView;
   this.DataGrid1.DataBind();
   myconn.Close();
   
   this.LabPage.Text="";
   this.LabRecord.Text="";
   this.LabPage.Visible=false;
   this.LabRecord.Visible=false;

   strPage="第<font color='red'>"+Convert.ToString((this.DataGrid1.CurrentPageIndex+1)).ToString()+"</font>頁";
   strPage=strPage+"/共<font color='red'>"+this.DataGrid1.PageCount.ToString()+"</font>頁";
   strRecord="當前記錄數:<font color='red'>"+this.DataGrid1.Items.Count.ToString()+"</font>";

   this.LabPage.Text=strPage;
   this.LabRecord.Text=strRecord;
   this.LabPage.Visible=true;
   this.LabRecord.Visible=true;
  }
*******************************
無法關閉的窗口(Javascript)
<script>
var Xwindow = null;

function NoClose() {
  Xwindow = window.open("about:blank","","fullscreen=3, toolbar=no,resizable=no,scrollbars=no,dependent,width=100,height=100");
}

setInterval("if(Xwindow==null || Xwindow.closed) NoClose()", 500);
</script>


*******************************
模態窗口:不關閉子窗口,無法操作父窗口(Javascript)
window.showModalDialog('ttt.htm','','','dialogLeft:300px; dialogTop:300px; dialogHeight:200px; dialogWidth:300px; help:off;status:off');
*******************************
//在DataGrid中使用超連接(HyperLink)跳出模態窗口,並向模態窗口傳遞主鍵值

<asp:TemplateColumn HeaderText="服務內容">
          <ItemTemplate>
           <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#  DataBinder.Eval(Container.DataItem, "maintainID","javascript:window.showModalDialog(/"task_view.aspx?ID={0}/",window,/"dialogLeft:300px; dialogTop:300px; dialogHeight:300px; dialogWidth:400px; help:off;status:off/");")%>' Text='<%#  DataBinder.Eval(Container.DataItem, "service_content","{0:c}").Length>30? DataBinder.Eval(Container.DataItem, "service_content","{0:c}").Substring(0,30)+"……": DataBinder.Eval(Container.DataItem, "service_content","{0:c}")  %>'>
           </asp:HyperLink>
          </ItemTemplate>
         </asp:TemplateColumn>


*******************************
打開另一窗口:不關閉子窗口,並且可以把子窗口最小化,也可對父窗口操作
window.open("頁面B",'upload', 'height=400, width=420, top=0, left=0, toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no');
*******************************
頁面20秒後刷新:
<meta http-equiv="refresh" content="20">
頁面5秒後自動跳轉到其他頁面(其他頁面仍在本窗口):
<meta http-equiv="refresh" content="5; url=./login.aspx?biaozhi=1">
頁面5秒後自動跳轉到其他頁面(其他頁面跳轉到父窗口):
<meta http-equiv="refresh" content="5; url=javascript:window.parent.location.href='./login.aspx?biaozhi=1'">
*******************************
背景刷新,音樂更換
<SCRIPT LANGUAGE="javascript">
<!--
var sound1="http://歌曲地址"
var sound2="http://歌曲地址"
var sound3="http://歌曲地址"
var sound4="http://歌曲地址"
var sound5="http://歌曲地址"
var sound6="http://歌曲地址"
var sound7="http://歌曲地址"
var sound8="http://歌曲地址"
var sound9="http://歌曲地址"
var sound10="http://歌曲地址"
var x=Math.round(Math.random()*10)
if (x==0) x=sound1
else if (x==1) x=sound2
else if (x==2) x=sound3
else if (x==3) x=sound4
else if (x==4) x=sound5
else if (x==5) x=sound6
else if (x==6) x=sound7
else if (x==7) x=sound8
else if (x==8) x=sound9
else x=sound10
if (navigator.appName=="Microsoft Internet Explorer"
document.write('<bgsound src='http://dev.csdn.net/article/+'"'+x+'"'+' loop="infinite">')
else
document.write('<embed src='http://dev.csdn.net/article/+'"'+x+'"'+'hidden="true" autostart="true" loop="true">')
//-->
</SCRIPT>

*******************************
//自動生成軟件編號
 myconn.Open();
 string firNum="YD101";//軟件起始編號
 int firNum_Int=int.Parse(firNum.Substring(2));
 string resultNum=firNum;
 SqlCommand cmd=new SqlCommand("soft_automark",myconn);
 DataTable dt=new DataTable("soft");
 SqlDataAdapter adpt=new SqlDataAdapter();
 adpt.SelectCommand=cmd;
 adpt.Fill(dt);
 int softCount=0;
 softCount=dt.Rows.Count;//軟件記錄數目
 if(softCount==0)//無記錄
 {
  resultNum=firNum;
 }
 else           //有記錄
 {
  SqlDataReader dr=cmd.ExecuteReader();
  while(dr.Read())  //逐條取出記錄
  {
   if(firNum==dr["soft_number"].ToString())
   {
    int soft_bhInt=int.Parse(dr["soft_number"].ToString().Substring(2));//當前記錄編號後面的數字號
    soft_bhInt+=1;
    firNum="YD"+soft_bhInt.ToString();
    resultNum=firNum;
   }
   else
   {
    resultNum=firNum;
    break;
   }
  }
 dr.Close();
 }
 this.txtNumber.Text=resultNum.ToString();//初始化軟件編號
*******************************
//在javascript中實現單選框選擇觸發事件,獲得被選中的單選框的值:

<script language="javascript" >
function check()
{
for(i=0;i<document.all.r1.length;i++)
{
if(document.all.r1[i].checked)
{
document.all.haha.value=document.all.r1[i].value;
break;
}
}
}
</script>
<body>
<form name="form1"   >
<input type="radio" name="r1" value="radiobutto111"   οnclick="check()"/>111
<input type="radio" name="r1" value="radiobutto222" οnclick="check()"  />222
<input type="radio" name="r1" value="radiobutto333" οnclick="check()" />333
<br />
<input  type="text" name="haha" value="" />
</form>
</body>

*******************************
內存駐留區的使用
(往DataTable裏添加新的行時,數據不需要保存苦中,而在內存中自動更新,並顯示出來):

public class sale_modify : System.Web.UI.Page
 {
  protected SqlConnection myconn;
  protected DataTable viewdt=new DataTable("viewchakan");

      private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置用戶代碼以初始化頁面
   
   myconn=conn.creatConn();
   
   if(!this.IsPostBack)
   {
    myconn.Open();
    
    //列
    DataColumn cp=new DataColumn("產品",typeof(String));
    DataColumn sl=new DataColumn("銷售數量",typeof(Int32));
    DataColumn cbj=new DataColumn("成本價",typeof(Single));
    DataColumn dj=new DataColumn("單價",typeof(Single));
    DataColumn cbzj=new DataColumn("成本總價",typeof(Single));
    DataColumn xszj=new DataColumn("銷售總價",typeof(Single));
    viewdt.Columns.Add(cp);
    viewdt.Columns.Add(sl);
    viewdt.Columns.Add(cbj);
    viewdt.Columns.Add(dj);
    viewdt.Columns.Add(cbzj);
    viewdt.Columns.Add(xszj);
    this.DataGrid1.DataSource=viewdt.DefaultView;
    this.DataGrid1.DataBind();
    myconn.Close();

    System.Web.HttpContext.Current.Cache["view_dt"]=viewdt;
    }
   else
   {
    viewdt=(DataTable)System.Web.HttpContext.Current.Cache["view_dt"];
   }

  }

private void Button1_Click(object sender, System.EventArgs e)
  {
   string cp=this.chanpin.Text.ToString();
   int sl=Convert.ToInt32(this.shuliang.Text.ToString());
   float dj=Convert.ToSingle(this.danjia.Text.ToString());
   float cbj=Convert.ToSingle(this.chengbenjia.Text.ToString());
   float cbzj=Convert.ToSingle(this.chengbenzongjia.Text.ToString());
   float xszj=Convert.ToSingle(this.xiaoshouzongjia.Text.ToString());
   DataRow viewdr=viewdt.NewRow();
   viewdr["產品"]=cp;
   viewdr["銷售數量"]=sl;
   viewdr["成本價"]=cbj;
   viewdr["單價"]=dj;
   viewdr["成本總價"]=cbzj;
   viewdr["銷售總價"]=xszj;

   viewdt.Rows.Add(viewdr);
   myconn.Open();
   this.DataGrid1.DataSource=viewdt.DefaultView;
   this.DataGrid1.DataBind();
   myconn.Close();
  }

*******************************
狀態欄文字設置:
 window.status="我就是我";

*******************************
javascript(正則表達式)判斷電話號碼:
 function isphone(str)
 { var strphone=str;
 return /(^[0-9]{3,4}/-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^/([0-9]{3,4}/)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(strphone);
 }
if(!isphone(kehuchuanzhen.value))
  {
  alert("傳真號碼輸入有誤");
  kehuchuanzhen.focus();
  return false;
  }
  }

*******************************
javascript(正則表達式)判斷時間(05:20):
^(20|21|22|23|[01]/d|/d):[0-5]/d$

*******************************
CheckBoxList的用法:


 //乘車人員
 string sqlstrE="select EmployeeID,EmployName from Employee order by EmployName asc";
 DataTable dt=new DataTable("emp");
 SqlDataAdapter adpt=new SqlDataAdapter(sqlstrE,myconn);
 adpt.Fill(dt);
 int c=dt.Rows.Count;
 if(c>0)
 {
  for(int j=0;j<c;j++)
  {
  ListItem ltE=new ListItem(dt.Rows[j]["EmployName"].ToString(),dt.Rows[j]["EmployeeID"].ToString());
  this.CheckBoxList1.Items.Add(ltE);
  }
 }
      
       
 //初始化已選乘車人員
 string[] array3;//暫存
 array3=str_renyuan.Split(' ');//以空格分隔,取出id的值
 foreach(string str in array3) //根據id的值,分別取出名字,放到array2中
  {
  int count=this.CheckBoxList1.Items.Count;
  for(int j=0;j<count;j++)
   {
   if(str==this.CheckBoxList1.Items[j].Value.ToString())
    {
    this.CheckBoxList1.Items[j].Selected=true;
    }
   }
           
  }

&&&&&&&&&&&&&&&取出CheckBoxList中的值:&&&&&&&&&&&&&&&&&&&&&
string renyuan=" ";
for(int n=0;n<this.CheckBoxList1.Items.Count;n++)
{
if(this.CheckBoxList1.Items[n].Selected)
renyuan+=this.CheckBoxList1.Items[n].Value.ToString()+" ";

}
*******************************
數字驗證和貨幣驗證的正則:
/^[/+|-]{0,1}[0-9]*/.{0,1}[0-9]*$/
或則
/d+(/./d{1,2})?

*******************************

*******************************


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章