ASP.NET常用方法及屬性
發表日期:2019-01-05 文章編輯: 瀏覽次數:4586 標簽:
截取字符串
<%# Tool.StringTruncat(Eval("title").Tostring(),20,"...") %>
去掉IP最后一個數
<%#Eval("ip").Tostring().SubString(0,Eval("ip").Tostring().LastIndexOf(".")+1)+ "*" %>
Ajax的使用
在使用ajax的文件中加上ScriptManager控件
在使用ajax的區域使用UpdatePanel控件的ContentTemplate節點包括
ajax的彈出框
ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "click", "alert('保存成功')", true);
//獲取性別 數據庫中sex字段0為男,1為女
前臺:<img src='images/<%#GetSexImage(Eval("sex"))' %>
后臺:
public string GetSexImage(object sex)
{
return sex.Tostring()=="1"?"girl.gif":"boy.gif";
}
//獲取登錄的次數
litlogincount.Text=new DAL.Login_logDAL().CalcCount("username='"+User.Identity.Name+"'").Tosring();
//獲取是否回復 三元運算
<%#(Eval("reply") == null || Eval("reply").ToString() == "") ? "否" : "是" %>
//獲取作品表中的作品數量
<%#GetImgCount(Eval("id")) %>
public string GetImgCount(object workid)
{
return DAL.Work_imgDAL().CalcCount("workid="+workid).ToString();
}
//獲取評級
int pj = 5;
if (rad1.Checked)
{
pj = 1;
}
if (rad2.Checked)
{
pj = 2;
}
if (rad3.Checked)
{
pj = 3;
}
if (rad4.Checked)
{
pj = 4;
}
if (rad5.Checked)
{
pj = 5;
}
//string pj=pj.ToString();
//0新品上市、1特價商品、2VIP商品
//賦值
int istj = chk.Items[0].Selected ? 1 : 0; //chk為CheckBoxList
int isxp = chk.Items[1].Selected ? 1 : 0;
int isvip = chk.Items[2].Selected ? 1 : 0;
//獲取
if (model.istj == 1)
{
chk.Items[0].Selected = true;
}
if (model.isxp == 1)
{
chk.Items[1].Selected = true;
}
if (model.isvip == 1)
{
chk.Items[2].Selected = true;
}
//前臺去掉html標簽并截取字符
<%# GetBody(Eval("body"))%>
public string GetBody(object body)
{
string str=body.ToString();
str=Utility.Tool.GetNoHTMLString(str);
str=Utility.Tool.StringTruncat(str,38,"...");
return str;
}
//顯示作品,即一個ul中有3個li,循環ul
<asp:Literal ID="litHTML" runat="server"></asp:Literal>
//綁定列表
private void BindRep()
{
//獲取列表anp的頁大小為9即3X3
DataSet ds = dao.GetList("*", "createdate", "desc", anp.PageSize, anp.CurrentPageIndex, GetCond());
int size = 3; // 每次取出的元素個數
// 要循環的次數
int num = ds.Tables[0].Rows.Count % size == 0 ? ds.Tables[0].Rows.Count / size : ds.Tables[0].Rows.Count / size + 1;
StringBuilder sb = new StringBuilder(); //SB拼接
for (int i = 0; i < num; i++)
{
sb.Append("<ul class='WEDING_CESE'>");
var query = ds.Tables[0].AsEnumerable().Skip(i * size).Take(size);
for (int y = 0; y < query.Count(); y++)
{
DataRow row = query.ElementAt(y);
if (i == 0 && y == 1)
{
sb.Append("<li style='background:none;'><a href='syzp_content.aspx?id=" + row["id"] + "' title='' target=''><img src='upload/" + row["img"] + "' alt='' class='FILTER' width='150' height='182'/></a></li>");
}
else
{
sb.Append("<li><a href='syzp_content.aspx?id=" + row["id"] + "' title='' target=''><img src='upload/" + row["img"] + "' alt='' class='FILTER' width='150' height='182'/></a></li>");
}
}
sb.Append("</ul>");
}
litHTML.Text = sb.ToString();
}
//Flash的圖片輪播
1、拷入flpicsocoll.rar文件中的images和template中的swf文件
2、把首頁中要采用輪播的地方加上<script>代碼,并修改大小及文字高度
3、創建數據庫 bbs_flash表 id,createdate,title,url,img
4、采用動軟生成bbs_flash代碼,加上分頁、計算記錄數代碼
其中采用GetListArray分頁方法
Public List<Model.Flash>GetListArray(string fields,.......)
{
把DataSet List中的分頁方法拷入
其中 return db.ExcuteDataSet(dbCommand); 改為:
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
while (dataReader.Read())
{
list.Add(ReaderBind(dataReader));
}
}
return list;
}
修改前臺的<script>中的重復區域代碼為<%= GetFlashJS()%>
后臺在Page_load之前加上
//生成FLASH中的相關JS
public string GetFlashJS()
{
DAL.PagesetDAL psdal = new Xiaobin.BBS.DAL.PagesetDAL();
List<Model.Pageset> list = psdal.GetListArray("*", "id", "asc", 5, 1, "fl='flash'");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
Model.Pageset ps = list[i];
int index = i + 1;
sb.Append("linkarr[" + index + "]='" + ps.url + "';picarr[" + index + "] ='upload/" + ps.img + "';textarr[" + index + "]='" + ps.title + "';");
}
return sb.ToString();
}
//面包屑導航
1、在母版頁中導航上加上如:論壇>>首頁 改為litsitemap
//導航
public void SetSitemap(string str)
{
litsitemap.Text = str;
}
2、在所有采用了母版頁的頁面中在 page_load中加上
//注: this.Master為母版頁 TopDown為母版頁的名稱
(this.Master as TopDown).SetSitemap("<a href='default.aspx'>"首頁</a>");
在user文件夾中的頁面中加上
(this.Master as TopDown).SetSitemap("<a href='default.aspx'>用戶中心</a> >> <a href='default.aspx'>用戶中心首頁</a>");
保留N位小數的方法
function formatFloat(src, pos) {
return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}
ASP.NET文本框中原樣輸出html代碼
Server.HtmlEncode(內容); //編碼 顯示原代碼用編碼
Server.HtmlDeCode(內容); //解碼
Server.URLEncode //GET提交及url中的編碼
Server.URLDecode //GET提交及url中的解碼
調用My97DatePicker日歷
1、引用My97DatePicker/WdatePicker.js文件
<input id="d11" type="text" onClick="WdatePicker()"/>
網頁中插入Flash
<object>
<embed width="100%" height="100%" name="plugin" src="/swf/980_475.swf" type="application/x-shockwave-flash">
</object>
插入透明的Flash
<EMBED height=295 pluginspage=http://www.macromedia.com/go/getflashplayer src=swf/nva.swf type=application/x-shockwave-flash width=1280 wmode="transparent" quality="high"></EMBED>