ASP.NET简单整理收藏(1)

来源:互联网 发布:翼支付mac 赚客吧 编辑:程序博客网 时间:2024/04/27 18:13

//连接数据库:
<appSettings>
<add key="sqllink" value="server=yfq-001;uid=ADM;pwd=sdadm;database=DBMT50"></add>
</appSettings>
//登录
string connstr=System.Configuration.ConfigurationSettings.AppSettings["sqllink"];
SqlConnection conn=new SqlConnection(connstr);
conn.Open();
string sql="select count(*) as icount from sysoper where logname='"+this.usrname.Text+"'";
SqlCommand comm=new SqlCommand(sql,conn);
SqlDataReader dr1=comm.ExecuteReader();
dr1.Read();
string count=dr1["icount"].ToString();
dr1.Close();
if(count!="0")
{
 sql="select * from sys where logname='"+this.usrname.Text+"'";
 SqlCommand  comm1=new SqlCommand(sql,conn);
 SqlDataReader dr=comm1.ExecuteReader();
 dr.Read();
 string drpwd=dr["logpass"].ToString();
 dr.Close();
 if(drpwd==this.usrpwd.Text)
 {
  this.Session["logid"]=this.usrname.Text.Trim();
  if(dr["PLCName"].ToString()=="")
  {
   this.Session["level"]="1";
  }
  if(dr["PLCName"].ToString()!="")
  {
   this.Session["level"]="2";
   this.Session["plcname"]=dr["PLCName"].ToString();
  } 
  this.Response.Redirect("main.aspx");
 }
 else
 {
  this.Response.Write("<script>alert('登录密码错误!')</script>");
 }
}
if(count=="0")
{
 this.Response.Write("<script>alert('没有这个用户!')</script>");
}
//框架的使用
<HTML>
<HEAD>
<TITLE>xxxxx</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<frameset rows="60,*" cols="*" border="0" frameSpacing="0" frameBorder="0">
 <frame name="topFrame" src="top.aspx" scrolling="no" noresize>
 <frameset style="CURSOR: w-resize" border="0" frameSpacing="12" frameBorder="yes" cols="143,74%">
  <frame name="leftFrame" borderColor="#99ccff" src="left.aspx">
  <frame name="mainFrame" src="bottom.htm">
 </frameset>
</frameset>
</HTML>
//treeview的使用
<iewc:TreeNode NavigateUrl="aa/bb.aspx" Text="设置" Target="mainFrame"></iewc:TreeNode>
//DataGrid的应用
//动态添加列
BoundColumn column1=new BoundColumn();
column1.HeaderText="用户";       
column1.DataField="UsrName";   
column1.HeaderStyle.Wrap=false;      
column1.ItemStyle.Wrap=false;   
column1.ItemStyle.HorizontalAlign=HorizontalAlign.Center;
column1.HeaderStyle.HorizontalAlign=HorizontalAlign.Center;
this.DataGrid1.Columns.Add(column1);

HyperLinkColumn linkcolumn=new HyperLinkColumn();
linkcolumn.HeaderText="帐号";
linkcolumn.DataNavigateUrlField="actid";
linkcolumn.DataTextField="actid";
//linkcolumn.DataNavigateUrlFormatString="bak.aspx?actid={0}"; 
   

linkcolumn.DataNavigateUrlFormatString="javascript:varwin=window.open('bak.aspx?id={0}');window.close()";
linkcolumn.HeaderStyle.Wrap=false;
linkcolumn.ItemStyle.Wrap=false;
linkcolumn.ItemStyle.HorizontalAlign=HorizontalAlign.Center;
linkcolumn.HeaderStyle.HorizontalAlign=HorizontalAlign.Center;
this.DataGrid1.Columns.Add(linkcolumn); 

HyperLinkColumn linkcolumn1=new HyperLinkColumn();
linkcolumn1.DataNavigateUrlField="actid";
linkcolumn1.HeaderText="修改";
linkcolumn1.Text="修改";
linkcolumn1.DataNavigateUrlFormatString="edit_usr.aspx?actid={0}";
linkcolumn1.HeaderStyle.Wrap=false;
linkcolumn1.ItemStyle.Wrap=false;
linkcolumn1.Visible=true;
linkcolumn1.HeaderStyle.HorizontalAlign=HorizontalAlign.Center;
linkcolumn1.ItemStyle.HorizontalAlign=HorizontalAlign.Center;
this.DataGrid1.Columns.Add(linkcolumn1);

HyperLinkColumn linkcolumn2=new HyperLinkColumn();
linkcolumn2.DataNavigateUrlField="actid";
linkcolumn2.HeaderText="删除";
linkcolumn2.Text="删除";
linkcolumn2.DataNavigateUrlFormatString="del.aspx?actid={0}";
linkcolumn2.HeaderStyle.Wrap=false;
linkcolumn2.ItemStyle.Wrap=false;
linkcolumn2.Visible=true;
linkcolumn2.HeaderStyle.HorizontalAlign=HorizontalAlign.Center;
linkcolumn2.ItemStyle.HorizontalAlign=HorizontalAlign.Center;
this.DataGrid1.Columns.Add(linkcolumn2);

string connstr=System.Configuration.ConfigurationSettings.AppSettings["sqllink"];
SqlConnection conn=new SqlConnection(connstr);
conn.Open();
string sql="select * from usract";
SqlDataAdapter dr=new SqlDataAdapter(sql,conn);
DataSet ds=new DataSet();
dr.Fill(ds,"usract");
this.DataGrid1.DataSource=ds.Tables["usr"].DefaultView;
this.DataGrid1.DataBind();
this.record.Text="共" + ds.Tables["usr"].Rows.Count.ToString() + "条";
this.pagenow.Text="第" +(this.DataGrid1.CurrentPageIndex + 1).ToString() +"/";
this.pagetotal.Text=this.DataGrid1.PageCount.ToString ()+"页";
this.DropDownList1.Items.Clear();
for(int i=1;i<=this.DataGrid1.PageCount;i++)

 this.DropDownList1.Items.Add(i.ToString());
}
this.DropDownList1.Items[(this.DataGrid1.CurrentPageIndex)].Selected=true;
this.DropDownList1.SelectedItem.Text =(this.DataGrid1 .CurrentPageIndex+1).ToString();
//默认分页
public void NavigateToPage(object sender, System.EventArgs e)
{
 string PageInfo  = ((LinkButton)sender).CommandName;
 switch (PageInfo)
 {
  case "第一页":
  DataGrid1.CurrentPageIndex = 0;
  break;
  case "上一页":
  if (DataGrid1.CurrentPageIndex > 0)
  DataGrid1.CurrentPageIndex -= 1;
  break;
  case "下一页":
  if (DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1))
  DataGrid1.CurrentPageIndex += 1;
  break;
  case "最后一页":
  DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1);
  break;
 }
 this.linkdb();

//跳页
this.DataGrid1.CurrentPageIndex=Convert.ToInt32(this.DropDownList1.SelectedItem.Text)-1;
this.linkdb();
//分页条
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="512" align="left" border="0" runat="server" style="WIDTH: 512px;

HEIGHT: 22px">
<TR>
<TD noWrap>
<p align="center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:label id="Label1" runat="server" ForeColor="White">每页显示20条记录</asp:label></p>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:label id="record" runat="server" ForeColor="White">Label</asp:label>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:label id="pagenow" runat="server" ForeColor="White">Label</asp:label>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:label id="pagetotal" runat="server" ForeColor="White">Label</asp:label>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
asp:linkbutton id="btnFirstPage" onclick="NavigateToPage" runat="server" ForeColor="White" CommandName="第一页">首页

</asp:linkbutton>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:linkbutton id="btnPreviousPage" onclick="NavigateToPage" runat="server" ForeColor="White" CommandName="上一页">上一页

</asp:linkbutton>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:linkbutton id="btnNextPage" onclick="NavigateToPage" runat="server" ForeColor="White" CommandName="下一页">下一页

</asp:linkbutton>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:linkbutton id="btnLastPage" onclick="NavigateToPage" runat="server" ForeColor="White" CommandName="最后一页">末页

</asp:linkbutton>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:label id="Label2" runat="server" ForeColor="White" Width="54px">跳转到</asp:label>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:dropdownlist id="DropDownList1" runat="server" Width="48px" AutoPostBack="True"></asp:dropdownlist>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
<asp:label id="Label3" runat="server" ForeColor="White" Width="15px">页</asp:label>
</TD>
<TD noWrap width="5"></TD>
<TD noWrap>
</TD>
</TR>
</TABLE>
//传参
private void DataGrid1_ItemDataBound_4(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 //特效
 if(e.Item.ItemIndex!=-1)
 {
  int orderid=e.Item.ItemIndex+1;
  e.Item.Cells[0].Text=orderid.ToString();    

e.Item.Attributes.Add("onmouseover","this.setAttribute('BKC',this.style.backgroundColor);this.style.backgroundColor='#3a6ea5'

");
  e.Item.Attributes .Add("onmouseout","this.style.backgroundColor=this.getAttribute('BKC');");
  if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item)
  {
  //弹出一个完整的IE窗口   

e.Item.Attributes.Add("onclick","window.open('message.aspx?actid="+e.Item.Cells[1].Text+"');");    

//e.Item.Attributes.Add("onclick","window.open('detail.aspx?ID="+e.Item.Cells[1].Text+"','newwin','width=750,height=600,scrol

lbars=yes,top=50,left=50');");
  }
 }
}
Response.Write("<script>window.open('excel.aspx','_blank');</" + "script>");
this.linkdb();
//
string sql="select * from VIEW_USRACT WHERE ACTID='"+Session["strmta"].ToString()+"'";
SqlCommand cmd=new SqlCommand(sql,conn);
try
{
 SqlDataReader dr=cmd.ExecuteReader();
 if(dr.Read())
 {  
  this.ACTIDtext.Text=dr[0].ToString();
  this.USRNAMEtext.Text=dr[1].ToString();
 }
}
//添加
sql="insert into ContList()

values('"+this.txtactid.Text.Trim()+"','"+count.ToString()+"','"+this.txtCName.Text.Trim()+"','"+this.txtCRank.Text.Trim()+"'

,'"+this.txtCphone1.Text.Trim()+"','"+this.txtCphone2.Text.Trim()+"','"+this.txtHdPhone.Text.Trim()+"','"+this.txtHmPhone.Tex

t.Trim()+"','"+this.txtContBP.Text.Trim()+"','"+this.txtCmemo.Text.Trim()+"','"+sqlstr+"')";
SqlCommand  myCommand=new SqlCommand(sql,conn);
myCommand.ExecuteNonQuery();
//修改
string sql1="";
sql1="update ContList set RouteID='"+sqlstr+"' where ActID='"+this.txtactid.Text.Trim()+"'";
SqlCommand  cmd=new SqlCommand(sql1,conn);
cmd.ExecuteNonQuery();
//调用存储过程
SqlCommand cmd1=new SqlCommand("PRO_PLANALME",conn);
cmd1.CommandType = CommandType.StoredProcedure; 
cmd1.Parameters.Add("@aa",this.txtactid.Text.Trim());
cmd1.ExecuteNonQuery();
//
if(this.Page.IsValid)
{
 try
 {
  this.Response.Write("<script>alert('添加成功');</script>");
 }
 catch(Exception E)
 {
  this.Response.Write(E.ToString());
  this.Response.Write("<script>alert('添加不成功');</script>");
 }
 finally
 {
  conn.Close();
 }
}
else
{
 this.Response.Write("<script>alert('修改不成功');</script>");
}
 
private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{//姓名(txtCName)
 try
 {
  int byteCount=System.Text.Encoding.Default.GetByteCount(this.txtCName.Text.Trim());
  if(byteCount>10)
  {
   args.IsValid=false;
   return;
  }
 }
 catch(Exception)
 {
 }
 args.IsValid=true;
}
//修改               

UPDATE ContList set CName=@CName WHERE (ContID=@ContID AND ActID=@ActID)"; 
SqlCommand cmd=new SqlCommand(sql,conn);
cmd.Parameters.Add(new SqlParameter("@ActID",SqlDbType.VarChar));
cmd.Parameters["@ActID"].Value="000004";
//序号
cmd.Parameters.Add(new SqlParameter("@ContID",SqlDbType.VarChar));
cmd.Parameters["@ContID"].Value=this.Request.Params["ContID"].ToString();
cmd.ExecuteNonQuery();

//删除
this.Response.Write("<script language='javascript'>");
this.Response.Write("aa=window.confirm('您确定要删除联系人');");
this.Response.Write("if(aa==false){");
this.Response.Write("window.location.href='contlist.aspx'");
this.Response.Write("}");
this.Response.Write("if(aa==true)");
this.Response.Write("{");
this.Response.Write("window.location.href='del_cont.aspx'");
this.Response.Write(" } "); 
this.Response.Write("</script>");

string sql="";
sql="delete from ContList where ContID='"+this.Session["ContID"].ToString()+"' AND ActID='000004'";
SqlCommand  cmd=new SqlCommand(sql,conn);
SqlCommand cmd1=new SqlCommand("PRO_PLANALMEVT_INTOPLANALMEVTGET",conn);
cmd1.CommandType = CommandType.StoredProcedure; 
cmd1.Parameters.Add("@ACTID","000004"); 
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
this.Response.Write("<script>alert('删除成功')</script>");       

this.Response.Write("<script>window.location.href='contlist.aspx'</script>");

//打印

<style>
 BODY { SCROLLBAR-BASE-COLOR: #99ccff }
</style>
<style media="print">
.Noprint { DISPLAY: none }
UNKNOWN { PAGE-BREAK-AFTER: always }
</style>
<style>
.style_td { BORDER-RIGHT: #ffffff 0px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM:

#000000 1px solid }
.style_tab { BORDER-RIGHT: #000000 2px solid; BORDER-TOP: #000000 2px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM:

#000000 1px solid }
.NOPRINT { FONT-SIZE: 9pt; FONT-FAMILY: "宋体" }
</style>

<TD align="center"><!--#include file="print.html" --><FONT face="宋体"></FONT></TD>

<center class="Noprint">
<p>
<OBJECT id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0" VIEWASTEXT>
</OBJECT>
<input type="button" value="打印" onclick="document.all.WebBrowser.ExecWB(6,1)"> <input type="button" value="直接打印"

onclick="document.all.WebBrowser.ExecWB(6,6)">
<input type="button" value="页面设置" onclick="document.all.WebBrowser.ExecWB(8,1)">
<input type="button" value="打印预览" onclick="document.all.WebBrowser.ExecWB(7,1)">
</p>
</center>
//取得IP地址
string hostIP=System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].ToString();


原创粉丝点击