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.Sort;

DataGrid1.DataSource=objDataView;
   DataGrid1.DataBind();
}
DataGrid1.AllowSorting=True;
<asp:BoundColumn DataField="khbh" ReadOnly="True" HeaderText="客户编号" Sort="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.Cells[0].Controls[1];
       if(h.Checked == true)
       { //取得已选取的主键
           SIDString +=" or (khbh ='" + DataGrid1.Items.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["Oper_state"].ToString()=="1")
  ds.Tables[0].Rows["Oper_state"]="有效";
  else if(ds.Tables[0].Rows["Oper_state"].ToString()=="0")
  ds.Tables[0].Rows["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();
35 一些程式短语:
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(" !!!");

36.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>超级联接

L来至 http://www.360doc.com/userhome/15774578 收藏

ASP.NET c#学习经验的更多相关文章

  1. ASP.NET从零开始学习EF的增删改查

           ASP.NET从零开始学习EF的增删改查           最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...

  2. ASP.NET MVC学习系列(二)-WebAPI请求

    继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...

  3. ASP.NET MVC学习系列(二)-WebAPI请求(转)

    转自:http://www.cnblogs.com/babycool/p/3922738.html 继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的g ...

  4. [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参

    [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参 本文转自:http://www.cnblogs.com/babycool/p/3922738.html ASP.NET MVC学习系 ...

  5. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

  6. 路由其实也可以很简单-------Asp.net WebAPI学习笔记(一) ASP.NET WebApi技术从入门到实战演练 C#面向服务WebService从入门到精通 DataTable与List<T>相互转换

    路由其实也可以很简单-------Asp.net WebAPI学习笔记(一)   MVC也好,WebAPI也好,据我所知,有部分人是因为复杂的路由,而不想去学的.曾经见过一位程序猿,在他MVC程序中, ...

  7. ASP.NET MVC学习系列(二)-WebAPI请求 转载https://www.cnblogs.com/babycool/p/3922738.html

    继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...

  8. [转]C语言指针学习经验总结浅谈

    指针是C语言的难点和重点,但指针也是C语言的灵魂 . 这篇C语言指针学习经验总结主要是我入职以来学习C指针过程中的点滴记录.文档里面就不重复书上说得很清楚的概念性东西,只把一些说得不清楚或理解起来比较 ...

  9. ASP.NET MVC学习之Ajax(完结)

    一.前言 通过上面的一番学习,大家一定收获不少.但是总归会有一个结束的时候,但是这个结束也意味着新的开始. 如果你是从事ASP.NET开发,并且也使用了第三方控件,那么一定会觉得ASP.NET开发aj ...

随机推荐

  1. 3527: [Zjoi2014]力

    题目大意:给出n个数qi,定义 Fj为        令 Ei=Fi/qi,求Ei. 设A[i]=q[i],B[i]=1/(i^2). 设C[i]=sigma(A[j]*B[i-j]),D[i]=si ...

  2. MySQL笔记--查询语句实践

    有一个用户表,属性为 id,age,buytime 创建表以及插入数据的语句 CREATE TABLE USER( id INT, age INT, buytime INT ) ,,); ,,); , ...

  3. 触摸事件 - UIControlEvents

    首先,UIControlEvents有这个几种: UIControlEventTouchDown           = 1 <<  0,      // on all touch dow ...

  4. 信号量 sem_t 进程同步

    sem_t分为有名和无名.有名的sem_t通过sem_open来创建, 而无名的sem_t通过sem_init的初始化. 用有名的sem_t来进程间同步是件很容易的事情,百度上一搜很多想相关的例子. ...

  5. BAT带队烧钱圈地华为们猛追云计算

    在和一位创业者交流时,他说现在创业者想从市场脱颖而出太难了,且不论创业本身的不易,更多时候是想做的事情都被BAT广撒网覆盖了. 现实也正是如此,包括影业.在线音乐.车联网等领域,BAT都已涉足.如今, ...

  6. Ubuntu 14.04 升级后 VPN 无法连接的问题

    如果不知道怎么配置 VPN Server(IPSEC + L2TP),可以看这篇文章(英文). 问题表现: 将 Ubuntu 12.04 通过自动更新 —— do-release-upgrade —— ...

  7. app开发历程————服务器端生成JSON格式数据,采用Unicode编码,隐藏中文

    今天,问以前的同事,他们写接口按什么编码,怎么看到有\u的一些看不懂的内容,一问,原来是信息隐藏,防止信息泄漏. 然后在网上查了Java如何把中文转换成unicode编码,转自:http://blog ...

  8. 2013=12=2 bitree-----补充

  9. 使用GDB生成coredump文件【转载】

    本文转载自: http://blog.csdn.net/sky_qing/article/details/8548989 如果在测试过程中遇到某个进程的CPU利用率过高或者卡死而需要去调试该进程时,可 ...

  10. java GBK字符转换成为UTF-8编码字符

    import java.util.HashMap; import java.util.Map; /** * 创建日期: 2014-04-18 10:36:25 * 作者: 黄飞 * mail:huan ...