1 XML数据操作
1.1 读取XML格式的字符串
XmlDocument doc = new XmlDocument();
doc.LoadXml(S);
string name= doc.SelectSingleNode("//name").InnerText;
1.2 读取XML文件里的字符串
XmlDocument doc = new XmlDocument();
doc.Load(GetFilePath("config.xml"));
string key = doc.SelectSingleNode("//key").InnerText;
1.3 XML文件数据绑定DataSet
DataSet ds= new DataSet();
ds.ReadXml(HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.PhysicalApplicationPath) + "xml/name.xml");
1.4 XML文件数据绑定DropDownList
DataSet sort = new DataSet();
sort.ReadXml(Server.HtmlEncode(Request.PhysicalApplicationPath) + "xml/name.xml");
this.DropDownList1.DataSource = sort;
this.DropDownList1.DataTextField = "Name";
this.DropDownList1.DataValueField = "ID";
this.DropDownList1.DataBind();
2 引用资源文件的四种方法,前台3种,后台1种
前台页面直接引用文本:<%= Resources.Resource1.String1%>
前台控件文本显示:
<asp:Label ID="Label1" runat="server" Text="<%$ Resources: Resource1, String2 %>">
</asp:Label>
前台JS文件显示:
document.getElementById("Label1").innerHTML ="<%= Resources.Resource1.String3 %>";
后台显示:this.Label1.Text = App_GlobalResources.Resource1.String3;
//后台也可以Resources.Resource1.String3,不知道怎么引用,干脆直接全球化文件夹。
3 后台弹出alert
Page.ClientScript.RegisterStartupScript(GetType(),"","alert('删除成功!');",true);
4 DropDownList 插入请选择
this.DropDownList1.Items.Insert(0, new ListItem("请选择", “-1”));
5 计算一段代码的执行时间和暂停时间protected void Button_OnClick(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();//实例化一个计时器
watch.Start();//开始计时
Thread.Sleep(5000);//暂停5秒
watch.Stop();//结束计时
string time = watch.ElapsedMilliseconds.ToString(); //获取当前实例测量得出的总运行时间(以毫秒为单位)
}
6 分割字符串去掉空项
string[] b= a.Split(new char[] { '|' },StringSplitOptions.RemoveEmptyEntries);
string[] c= a.Split(char.Parse("|"));//包括空项
7 dataset, datatable 插入列 和数据
dsl.Tables[0].Columns.Add(new DataColumn("zplist", typeof(string)));
for (int i = 0; i < dsl.Tables[0].Rows.Count; i++)
{
dsl.Tables[0].Rows[i]["zplist"] = "ppppppppp";
}
8
获取CheckBoxList当前选择项索引
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = Request.Form["__EVENTTARGET"];
int index = Convert.ToInt32(s.Substring(s.LastIndexOf("$") + 1));
string selectText = this.CheckBoxList1.Items[index].Text;
string selectValue = this.CheckBoxList1.Items[index].Value;
}
9 字符串变成控件名
form1.FindControl("shebei1").Visible=true;