个人的web开发心得(七)----------非常适合入门新手,都是常识
61.
ref 和 out的区别和用法(作用相同,只是out不用初始化 ref要先初始化)
ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。例如:
复制代码
class RefExample
{
static void Method(ref int i)
{
i = 44;
}
static void Main()
{
int val = 0; /********ref和out不同处***********/
Method(ref val);
// val is now 44
}
}
传递到 ref 参数的参数必须最先初始化。这与 out 不同,out 的参数在传递之前不需要显式初始化。(
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value; /********ref和out不同处***********/
Method(out value);
// value is now 44
}
}
62.取该页面的 绝对路径
string strurl = Request.Url.AbsolutePath;
63.转换日期 注意年月的大小写不同
return dataTime.ToString("yyyy-MM-dd HH:mm:ss");
64.SQL语句 使用 or and 的用法
select * from wlog where pid='211' and ldate='2006-06-30' or pid='211' and ldate='2006-06-29'
也就是说 以 or为分组条件 以and为一组
65.between语句 检索字符型 日期 什么时间到什么时间之间的记录。
select * from wlog where pid='211' and ldate between '2006-06-25' and '2006-06-30' and pid='211'
66. 在gridview中 找控件checkboxlist
SqlDataReader sdr = sc.ExecuteReader(CommandBehavior.CloseConnection);
((CheckBoxList)DataGrid1.FindControl("CheckBoxList2")).DataSource = sdr;
((CheckBoxList)DataGrid1.FindControl("CheckBoxList2")).DataTextField = "权限名称";
((CheckBoxList)DataGrid1.FindControl("CheckBoxList2")).DataBind();
在treeview中找节点
string notepath="顶层note的Value\次层的note的Value\第三层的note的Value";
if (TreeView1.FindNode(notepath) != null)
TreeView1.FindNode(notepath).Checked = true;
67.在gridview中 隐藏主键字段
先邦定字段到 gridview
设置字段id为 gridview的DataKeyName属性
获取该行编号 用
for (int i = 0; i < GridView1.Rows.Count; i++)
GridView1.DataKeys[i].Value.ToString();
68.如果查询记录为空,则返回0
select name,isnull((select num from b where name=a.name),0) from a
isnull((select num from b where name=a.name),0)
就是如果里面的select语句为null,那么返回0
69.鼠标 单击 datagrid的某行 则该行 呈现别的颜色 用于选择某行记录作处理。
private void dataGrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ( ( e.Item.ItemType == ListItemType.Item ) || ( e.Item.ItemType == ListItemType.AlternatingItem ) || ( e.Item.ItemType == ListItemType.SelectedItem ) )
{
e.Item.Attributes.Add( "OnMouseOver", "this.style.backgroundColor = '#BED3E9';this.style.cursor='hand'");
e.Item.Attributes.Add( "OnMouseOut", "this.style.backgroundColor='white';");
}
}
70.将纯数字导入Excel时数据格式仍保持不变的方法
办法就是利用Excel的特性
查看例子
代码
<%@ Page language="c#" AutoEventWireup="true" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
string data1 = "000000001";
long data2 = 123456789123456789;
System.IO.StringWriter sw = new System.IO.StringWriter();
sw.WriteLine("原数字1\t转换后数字1\t原数字2\t转换数字2");
for(int i= 0;i<10;i++)
{
sw.WriteLine(data1 +"\t" + "=\"" + data1 + "\"\t" + data2 +"\t" + "=\"" + data2.ToString() + "\"");
}
sw.Close();
Response.Buffer= true;
Response.Charset="";
Response.AppendHeader("Content-Disposition","attachment;filename=aa.xls");
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.Write(sw);
Response.End();
}
</script>