代码备忘录
1、遍历枚举
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
/// 使用枚举填充下拉框
/// </summary>
/// <param name="DdlType">下拉框</param>
/// <param name="project">typeof(枚举)</param>
public static void FillEnumDdl(DropDownList DdlType,Type project)
{
DdlType.Items.Clear();
foreach (string s in Enum.GetNames(project))
{
string text = s;
string value = Enum.Format(project, Enum.Parse(project, s), "d");
if(s!="销售" && s!="销售机会")
DdlType.Items.Add(new ListItem(text, value));
}
}
2、List转换为数组
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
List<string> lstStr = new List<string>();
lstStr.Add("A");
lstStr.Add("B");
lstStr.Add("C");
string [] arrStr = new string [lstStr.Count];
lstStr.CopyTo(arrStr);//此方法得到字符串数组
3、Repeater行号
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//显示行号
<%# Container.ItemIndex %>
//通过行号输出字符串
<%# Container.ItemIndex==3 ? "<div> hello33333 </div>" : string.Empty %>
4、获取Google查询参数
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function getGoogleSearchKeyword(){
var lastUrl = document.referrer.toString();
//http://www.google.com/search?hl=en&q=javaeye& btnG=Search
if(!lastUrl.match(/google\.com\/search\?/))return;
var p = lastUrl.indexOf('?');
var params = lastUrl.substr(p+1).split('&');
for(var i=0,l=params.length;i<l;i++){
if(params[i].indexOf("q=")+1){
return params[i].split("=")[1];
}
}
}
5、遍历Hashtable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
方法一
foreach (System.Collections.DictionaryEntry de in objHasTab)
{
//注意HastTable内存储的默认类型是object,需要进行转换才可以输出
Console.WriteLine(de.Key.ToString());
Console.WriteLine(de.Value.ToString());
}
方法二
System.Collections.IDictionaryEnumerator enumerator = objHashTablet.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Key); // Hashtable关健字
Console.WriteLine(enumerator.Value); // Hashtable值
}
6、获取Select选中值
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<select name=sel onchange="bao(this.options[this.options.selectedIndex].value)">
<option value="">请选择
<option value="1">Item 1
<option value="2">Item 2
<option value="3">Item 3
</select>
<script>
function bao(s)
{
txt.value+=s;
//选择后,让第一项被选中,这样,就有Change啦.
document.all.sel.options[0].selected=true;
}
</script>
<textarea id=txt></textarea>
7、jQuery and UpdatePanels
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
$(document).ready(function() {
doReady();
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function(s, e) {
doReady();
});
});
function doReady() {
$('.roundedCorners').corners();
}
8、文件路径
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1、c#根据绝对路径获取 带后缀文件名、后缀名、文件名。
string str =" F:\test\Default.aspx";
string filename = System.IO.Path.GetFileName(str);//文件名 “Default.aspx”
string extension = System.IO.Path.GetExtension(str);//扩展名 “.aspx”
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(str);// 没有扩展名的文件名 “Default”
2、c#根据绝对路径获取 带后缀文件名、后缀名、文件名,使用 Split 函数。
string str = =" F:\test\Default.aspx";
char[] delimiterChars = { '.', '\\' };
string[] Mystr = str.Split(delimiterChars);
string sheetName = Mystr[Mystr.Length - 2];);// 没有扩展名的文件名 “Default”