您的位置:首页 > 编程语言 > ASP

一个月多的ASP.NET+C#学习经验

2006-08-05 22:48 369 查看
0.一些程式短语:
string fdwmc=TextBox1.text.trim();

ArrayList value=new ArrayList();
value.Add("aaa");
value.Add("bbb");

Hashtable h=new Hashtable();
h.Add("a1","a2");
h.Add("b1","b2");

StringBuilder s3 = new StringBuilder();
s3.Append("hello");
s3.Append(" world");
s3.Append(" !!!");

00.HTML短语:
<p> 则是代表段落标注
<br>断行标注,,这个标注不需要再写一个对映的 </br>
<font>标注:本标注用来设定文字的大小、颜色、字体
<b> 粗体、<i> 斜体及 <u> 底线标注
<h1>~<h6> 标题标注
<blockquote>缩排标示
<ol> 条列标注:(显数字)
<ul>条列标注:(显。)
<div> 段落对齐标注
<!-- 这是批注, 给开发人员看的, 不会被解译 -->
<table> 标注用来表示表格的开始及结束
<tr> 则表示其中行的开始及结束
<td> 则表示一行中的字段
<img src="Train.jpg">显视图形
<a href="H10.htm>...</a><br>超级联接

1.DataGrid自定义字段.
<Column
<asp:BoundColumn DataField="khbh" HeaderText="客户编号"></asp:BoundColumn>
<asp:BoundColumn DataField="khjc" HeaderText="客户简称"><ItemStyle BackColor="#CCFFFF"></ItemStyle></asp:BoundColumn>
<asp:BoundColumn DataField="dh" HeaderText="电话"></asp:BoundColumn>
<asp:BoundColumn DataField="cjri" HeaderText="创建日期" DataFormatString="{0:yyyy-MM-dd}"></asp:BoundColumn>
<asp:BoundColumn DataField="bycgl" HeaderText="月均出柜量"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
<ItemStyle Font-Names="楷体_GB2312" Wrap="False" ForeColor="#339966"></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"><ItemStyle Font-Names="楷体_GB2312" Wrap="False" ForeColor="#339966"></ItemStyle>
</asp:ButtonColumn>
</Columns>
(DataGrid1.AutoGenerateColumns=False)

2.按一个控件后出一提示框选择是否执行.
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Button1.Attributes["onClick"]="javascript:return confirm('确定吗?');";

}

3.一般提示框.
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("<script language='javascript'>alert('hellow');</script>");
}

4.一般的DataGrid显视数据过程:
SqlConnection SqlConn;
DataSet objDataSet=new DataSet();
string Connstr="server=bserver;user=sa;database=SFA";
string SQLstr="select * from khxx";
SqlConn=new SqlConnection(Connstr);
SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
objAdapter.Fill(objDataSet,"khxx");
DataGrid1.DataSource=objDataSet.Tables["khxx"].DefaultView;
DataGrid1.DataBind();

5.在Web.config里设好连接数据库的字符串.
//web.config
</system.web>
<appSettings>
<add key="strConn" value="server=bserver;database=SFA;user=sa;"/>
</appSettings>
</configuration>
//WebForm1.aspx
using System.Configuration;
string Connstr=ConfigurationSettings.AppSettings["strConn"];

6.分页显视数据:
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}

7.统一显视数据函数:
public void BindGrid()
{
SqlConnection SqlConn;
DataSet objDataSet=new DataSet();
string Connstr=ConfigurationSettings.AppSettings["strConn"];
string SQLstr="select * from khxx";
SqlConn=new SqlConnection(Connstr);
SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
objAdapter.Fill(objDataSet,"khxx");
DataGrid1.DataSource=objDataSet.Tables["khxx"].DefaultView;
DataGrid1.DataBind();
}

回复:
8.DataGrid数据编辑处理(一)_直接编辑
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
}

private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
BindGrid();
}

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;

SqlConnection SqlConn;
DataSet objDataSet=new DataSet();
string Connstr=ConfigurationSettings.AppSettings["strConn"];
string SQLstr="select * from khxx";
SqlConn=new SqlConnection(Connstr);
SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
objAdapter.Fill(objDataSet,"khxx");

TextBox CurrentText;
CurrentText = (TextBox)e.Item.Cells[1].Controls[0];//取得文本框
objDataSet.Tables["khxx"].Rows[(int)e.Item.ItemIndex]["khjc"]=CurrentText.Text;

SqlCommandBuilder cb=new SqlCommandBuilder(objAdapter);
SqlConn.Open();
objAdapter.Update(objDataSet,"khxx");
SqlConn.Close();

DataGrid1.DataSource=objDataSet.Tables["khxx"];
DataGrid1.DataBind();

}
在PageLoad()里一定要if(Page.IsPostBack){....};
DataGrid1.DataKeyField="khbh";

8.排序:
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
SqlConnection SqlConn;
DataSet objDataSet=new DataSet();
string Connstr=ConfigurationSettings.AppSettings["strConn"];
string SQLstr="select * from khxx";
SqlConn=new SqlConnection(Connstr);
SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
objAdapter.Fill(objDataSet,"khxx");

DataTable objDataTable=objDataSet.Tables["khxx"];
DataView objDataView=new DataView(objDataTable);
objDataView.Sort=e.SortExpression;

DataGrid1.DataSource=objDataView;
DataGrid1.DataBind();
}
DataGrid1.AllowSorting=True;
<asp:BoundColumn DataField="khbh" ReadOnly="True" HeaderText="客户编号" SortExpression="khbh">
<ItemStyle Wrap="False"></ItemStyle></asp:BoundColumn>

9.删除一条记录:
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection SqlConn;
DataSet objDataSet=new DataSet();
string Connstr=ConfigurationSettings.AppSettings["strConn"];
SqlConn=new SqlConnection(Connstr);

string bh=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string SQLstr="delete from khxx where khbh='"+bh+"'";
//int ProductID =(int)MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
//string SQLStatement="Delete Products WHERE ProductID="+ProductID;(当字段为整数时)

SqlCommand myCommand = new SqlCommand(SQLstr,SqlConn);

myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();

BindGrid();
}

10.删除时提示是否删除:
<HEAD>
.....
<script language="javascript">
function delete_confirm(e) {
if (event.srcElement.outerText=="删除")
event.returnValue=confirm("确认删除否?");
}
document.onclick=delete_confirm;
</script>
</HEAD>

11.在DataGrid里加复选框.
<Columns>
<asp:TemplateColumn HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox runat="server" ID="del" />
</ItemTemplate>
</asp:TemplateColumn>
......
</Columns>

12.选中删除多条记录:
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection SqlConn;
string Connstr=ConfigurationSettings.AppSettings["strConn"];
SqlConn=new SqlConnection(Connstr);

string DelString,SIDString="";
for(int i=0;i<DataGrid1.Items.Count;i++)
{
CheckBox h;
h=(CheckBox)DataGrid1.Items[i].Cells[0].Controls[1];
if(h.Checked == true)
{ //取得已选取的主键
SIDString +=" or (khbh ='" + DataGrid1.Items[i].Cells[1].Text + "')";
}
}
if(SIDString.Length>0)
{
SIDString=SIDString.Remove(0,4);
DelString = "Delete from khxx where " + SIDString;
SqlCommand DelRec = new SqlCommand(DelString,SqlConn);
DelRec.Connection.Open();
DelRec.ExecuteNonQuery();
DelRec.Connection.Close();
BindGrid();
}
}

13.不同WEB页传递参数(前一个向后一个传):
(WebForm1.aspx)
private void Button2_Click(object sender, System.EventArgs e)
{
Response.Redirect("WebForm2.aspx@ID="+TextBox1.Text+"&NAME="+TextBox2.Text);
}
(WebForm2.aspx)
private void Page_Load(object sender, System.EventArgs e)
{
string s;
s=Request.QueryString["ID"];
Label1.Text=s;
s=Request.QueryString["NAME"];
Label2.Text=s;
}

回复:
14.增加一条记录(一):
SqlConnection SqlConn;
DataSet objDataSet=new DataSet();
string Connstr=ConfigurationSettings.AppSettings["strConn"];
string SQLstr="select * from khxx";
SqlConn=new SqlConnection(Connstr);
SqlDataAdapter objAdapter=new SqlDataAdapter(SQLstr,SqlConn);
objAdapter.Fill(objDataSet,"khxx");

DataRow row=objDataSet.Tables[0].NewRow();
row["khbh"]=TextBox3.Text;
row["khjc"]=TextBox4.Text;
row["dh"]=TextBox5.Text;
row["cjri"]=TextBox6.Text;
row["bycgl"]=TextBox7.Text;

objDataSet.Tables[0].Rows.Add(row);
SqlCommandBuilder cb=new SqlCommandBuilder(objAdapter);
objAdapter.Update(objDataSet,objDataSet.Tables[0].ToString());
BindGrid();
15.代码色之用:
LinkButton1.ForeColor=ColorTranslator.FromHtml("#FF8000");

16.关于加载执行:
protected void Page_Load(Object sender, EventArgs e)
{
// 网页每次加载时,执行的一些操作
if (!IsPostBack)
{
// 网页第一次加载时执行的操作
}
else
{
// 回送时执行的操作
}

// 网页每次加载时执行的操作
}

17.打开网页时全屏显视:
(另加default.aspx,加入:)
<script language="javascript">
window.open('loadfile.aspx.html','_blank','menubar=no,location=no,toolbar=no,
scrollbars=no,status=no,top=0,left=0,width=' + screen.width + ',height=' + screen.height);
window.opener = null;
window.close();
</script>

18.打开新的浏览器窗口:
Response.Write("<SCRIPT language='javascript'> window.open('YOURURL')</SCRIPT>");

19.网页启动的时候弹出一个小窗体:
在Page_Lod事件中加入:
Response.Write("<script>window.open('newyear.htm','_blank','toolbar=no,location=no,directories=no,status=no,
menubar=no,scrollbars=no,revisable=no,left=100,top=0,width=600,height=50')</" + "script>");
弹出文件名:newyear.htm

20.Session的用法:
//传递参数
Session["markid"] = webform1.Text1.Value;
Application["markid"] = webform1.Text1.Value;
WebForm2.Text1.Value = Session["markid"].ToString();
WebForm2.Text1.Value = Application["markid"].ToString();

//清除
Session.Remove("markid"); (session可以定义在webform1的任何地方。)

21.html控件:
button光标移上移开的不同效果:
<INPUT id="Button3" style="Z-INDEX: 111; LEFT: 248px; POSITION: absolute;
TOP: 456px" onmouseover="this.style.color='red';"
onmouseout="this.style.color='black';" type="button"
value="text" name="Button3" runat="server">

22.cookie的用法:
HttpCookie cookie = new HttpCookie("aspcn");
cookie.Values.Add("webmaster","飞刀");
cookie.Values.Add("writer","beige");
cookie.Values.Add("LinkColor","blue");
Response.AppendCookie(cookie);
取出信息也一样简单
HttpCookie cookie = Request.Cookies["aspcn"];
value1 = cookies.Values["webmaster"];
value2 = cookies.Values["writer"];

23.ASP.NET中文显示之种几种解决方法:
(1)configuration>
<globalization
requestencoding="utf-8"
responseencoding="utf-8"
/>
</configuration>  或"gb2312" 或"big5"
(2)添加<%@ CODEPAGE = "936" %>到每一页的开头;

24.在线用户统计:
private void Page_Load(object sender, System.EventArgs e)
{
Visitors.Text = Application["user_sessions"].ToString();
}
global.asax文件:
protected void Application_Start(Object sender, EventArgs e)
{
Application["user_sessions"] = 0;
}
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application["user_sessions"] = (int)Application["user_sessions"] + 1;
Application.Unlock();
}
protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
Application["user_sessions"] = (int)Application["user_sessions"] - 1;
Application.Unlock();
}

25.DataSet的一种遍历修改方法:
SqlDataAdapter myCmd=new SqlDataAdapter(strSql,myConn);
DataSet ds=new DataSet();
myCmd.Fill(ds,"操作员");
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
if(ds.Tables[0].Rows[i]["Oper_state"].ToString()=="1")
ds.Tables[0].Rows[i]["Oper_state"]="有效";
else if(ds.Tables[0].Rows[i]["Oper_state"].ToString()=="0")
ds.Tables[0].Rows[i]["Oper_state"]="冻结";
}

26.自动刷新:
<head>
<!--每10秒自动刷新-->
<meta http-equiv="refresh" content="10">
</head>

27.ViewState的读取:
// 保存在 ViewState 中
ViewState["SortOrder"] = "DESC";
// 从 ViewState 中读取
string sortOrder = (string)ViewState["SortOrder"];

28.用Session传递DataSet:
DataTable Dt=new DataTable();
Dt=yourDataSet.Tables[yourtable].DefaultView;
DataGrid1.DataSource=Dt;//yourDataSet是一个DataSet对象
DataGrid1.DataBind();//假如你的“当前页”的DataGrid是这样邦定的
Session["MyTable"]=Dt;
另一页(打印页):
DataTable Dt2=new DataTable();
Dt2=(DataTable)Session["MyTable"];
DataGrid2.DataSource=Dt2;
DataGrid2.DataBind();

29.超链接传递中文参数的问题:"list.aspx?name=" + Server.UrlEncode("中国");

30.Calendar的日期取出:
private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
TextBox1.Text=Calendar1.SelectedDate.ToShortDateString();
}
private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
......
}

31.用DataTable在内存建表和加入数据方式一:
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("编号",typeof(Int32)));
dt.Columns.Add(new DataColumn("客户",typeof(string)));
DataRow dr;
dr=dt.NewRow();
dr[0]=9;
dr[1]="custom";
dt.Rows.Add(dr);
DataGrid1.DataSource=new DataView(dt);

32.Hashtable表的用法:
Hashtable h = new Hashtable();
h.Add ("键 1", "值 1");
h.Add ("键 2", "值 2");
h.Add ("键 3", "值 3");
MyDataList.DataSource = h;

33.通常的DataList模板应用:
<ItemTemplate>
编号:<%# DataBinder.Eval(Container.DataItem,"ID","{0:N2}") %><br>
项:<%# DataBinder.Eval(Container.DataItem,"STRING") %><br>
日期:<%# DataBinder.Eval(Container.DataItem,"DATETIME","{0:d}") %><br>
是否:
<asp:CheckBox ID="checkbox1" Checked='<%# DataBinder.Eval(Container.DataItem,"BOOL") %>' Runat=server /><br>
</ItemTemplate>

34.使用 SqlDataReader的一般方法:
SqlConnection myConnection = new SqlConnection(".......");
SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
MyDataGrid.DataSource = dr;
MyDataGrid.DataBind();
myConnection.Close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐