个人使用ASP.NET方法总结

 

www.houxiren.com
以下代码都是从该网站中提取出来的关键点。
本人能力有限,不喜勿喷!



一、            富文本编辑器

用法:

1、项目根目录下---->JavaScriptà xhe1.14 使用富文本编辑器需要先将xhe1.14这个文本包放到项目中。以便调用。

2、在使用的界面上引入xhe1.14包里面的xheditor-1.1.14-zh-cn.min.js文件

3、在页面中使用 代码如下:

<textarea   cols="100" rows="12"   runat="server" id="Textarea1"name="elm3"class="xheditor-simple"></textarea>

   Class虽然不能定位,但也一定要设置,而且必须为固定:class="xheditor-simple" name="elm3"这个属性也是一样。不能改变。(除非修改xheditor-1.1.14-zh-cn.min.js

注:此控件为HTML控件asp.net后台代码不能调用,只能使用Ajax.

 

 

二、            asp.net  ajax用法

 

1、asp.net使用ajax对数据操作必须要用一般处理程序配合使用。即使用ajax将数据提交给一般处理程序,一般处理程序就像是asp.net后台代码一样。可以对数据操作。

案例:

前台脚本代码:

JavaScript脚本,即Ajax使用Post方法提交(还有一个Josn格式忘记怎么用了)数据到adminDltPost.ashx一般处理程序中操作数据。

       function updetepost(PostID) {

          if (confirm("是否删除该贴子?") == false) return;

           $.post("/AdminForum/AdminAJAX/adminDltPost.ashx",{ "postID": PostID }, function (data) {     if (data == '1') {

                    alert("¦?y¨¦|");

                    window.location.reload(); //刷新当前页面

                     } else {

                      alert("¦?yº¡ì㨹");

}

  });

          }

一般处理程序:

        public voidProcessRequest(HttpContext context)

        {  context.Response.ContentType = "text/plain";

UserKJManage manage = new UserKJManage();

            int id = Convert.ToInt32(context.Request["postID"]);

            int i = manage.UserdeletePost(id);

            if (i > 0)

            {  context.Response.Write("1");   return;

            }

        }

在一般处理程序中要使用什么属性的时候一般都要先用context,然后在context点才能使用想要的属性。

 

 

三、            Repeater循环,双层嵌套循环

两个Repeater循环:

<asp:Repeater ID="rptPostMaxtype" runat="server" OnItemDataBound="rpt_OnItemDataBound">

   <ItemTemplate>

      <asp:Label ID="lblPostTopID" style="display:none;" runat="server" Text='<%#Eval("PostTopLisrtID")%>'></asp:Label>

        <li><a href='#'> <%#Eval("PostTopListName")%> </a> </li>

         <ul>

<asp:Repeater ID="rptpostMintype"runat="server"OnItemDataBound="rptType_OnItemdatebound">

          <ItemTemplate>

             <li>  <asp:Literal ID="ltrCount" runat="server" ></asp:Literal>

             <a onclick='aa(<%#Eval("postTypeID")%>);' target="_blank">

             <asp:Literal ID="lbltypeName" runat="server" Text='<%#Eval("typeName")%>'> </asp:Literal>

             </a>  </li>

          </ItemTemplate>

     </asp:Repeater>                  

 </ul>

 </ItemTemplate>

</asp:Repeater>

注:上图灰色部分代码属于第层Repeater 的循环。

1、两个Repeater循环:需要循环的数据都要放到 ItemTemplate 里面进行循环才可以使用,在ItemTemplate里面的asp控件(如上面的Label)在后台是不能直接调用的。

需要转换,如:Label lblof = e.Item.FindControl("lblPostTopID")as Label;

第一层Repeater循环后台代码最简单:

//两个Repeater 外面那个循环,只读取第一层的数据

 public void getposttypeMax()

        {    //mian是自己写的数据源

            this.rptPostMaxtype.DataSource =mian.getPostTopLisrt();

            this.rptPostMaxtype.DataBind();

        }

 

第二层 Repeater循环后台代码如下:

//第二层 Repeater 循环要用第一层Repeater 的OnItemDataBound 事件来触发

public voidrpt_OnItemDataBound(object sender, RepeaterItemEventArgs e)

 {

            Label lblID = e.Item.FindControl("lblPostTopID") asLabel;

            int id = Convert.ToInt32(lblID.Text);

           Session["PostMianTypeID"] =id; //将小类ID放进Session

            PostTopList p = (PostTopList)e.Item.DataItem;

            if (p != null)

            {   int i =p.PostTopLisrtID;

                Repeater rptPost = e.Item.FindControl("rptpostMintype") as Repeater;

               rptPost.DataSource = mian.getpostTypeList(i);

               rptPost.DataBind();

            } else {

                return;

            }

        }

 

读取第二层的数据时需要使用到第一层Repeater OnItemDataBound 事件来触发第二层循环:

 

2、Repeater循环时顺便生成序号:这里使用第二层的数据生成序号:

//生成序号

public voidrptType_OnItemdatebound(object sender, RepeaterItemEventArgs e)

        {

            Literal ltrCount = e.Item.FindControl("ltrCount") asLiteral;

            Repeater rptPost = (Repeater)sender;

            if (ltrCount != null&& rptPost != null)

            {

               ltrCount.Text = (rptPost.Items.Count + 1 + ":").ToString();

            }

        }

这里在第二层Repeater里面生成序号需要Repeater的OnItemDataBound 事件来触发

 

效果图如下

 

 

 

 

四、            记录本页面地址,跳转到某页面后再跳转回来。使用于返回上一页面

 

例:从A页面记录A页地址,跳转到B页面,B页面操作完成后再跳转到A页面

 用户未登录,需要发表时,记录本页面地址,跳转到登录页面,登录后自动跳转回原来的页面

本页面记录A地址:

string thisURL =Server.UrlEncode(Request.Url.ToString()); //记录本页面的URL

      Response.Redirect("UserLogin.aspx?thisurl="+ thisURL + "");

 

登录页面:

protected string thisurl =“”;

                   //记录从什么页面跳转过来的地址

            thisurl= Server.UrlDecode(Request["thisurl"].ToString());

            if (name != ""&& pwd != "")

            {

               user = userman.UserSubmit(name, pwd);

                if (user != null)

                {

                   Session["userid"] =user.UserID ;

                   Response.Redirect(thisurl + "&&id=" + user.UserID);

                }

                else

                {

                   this.lblErrer.Text = "账户名或密码错误";

                }

            }

 

 

五、            JQuery多面板

 

<div id="miandiv" class="duomianban_mianDIV">

  //标题列表

     <ul>

          <li><a href="#div1"><div class="biaotilandiv">帖子管理 </div></a></li>

          <li><a href="#div2"><div class="biaotilandiv">发布通知 </div></a></li>

      </ul>

     //某标题下具体内容在DIV内

     <div id="div1">

                            帖子管理

     </div>

<div id="div2">

                            发布通知

     </div>

</div>

使用多面板,需要引用JQ的UI包,如下图,

引用顺序:1、jquery-ui.css

          2、jquery.ui.core.js

          3、jquery.ui.widget.js

          4、jquery.ui.tabs.js

 

效果图:

 

 

六、            两个DropDownList关联取数

  根据第一个DropDownList 数据的ID 传给第二个DropDownList进行取数

//大类

<asp:DropDownList ID="DDLtype" runat="server" CssClass="posttypeListDIV"

   onselectedindexchanged="DDLtype_SelectedIndexChanged"AutoPostBack="True">  </asp:DropDownList>

 //根据大类ID取对应的小类

<asp:DropDownList ID="DrdListminpost" runat="server" CssClass="posttypeListDIV" ></asp:DropDownList>

 

//第一个DropDownList先绑定数据

   public voidgetDelPostmaxType()

   {

    DataTable datatable=newDataTable();

    datatable =man.getPostMaxTypeIDList();

    if (datatable != null)

    {

        this.DDLtype.DataSource = datatable;

        this.DDLtype.DataTextField = "PostTopListName";

        this.DDLtype.DataValueField = "PostTopListID";

        this.DDLtype.DataBind();

        this.DrdListminpost.Items.Insert(0, "????¤¨¤"); //第二个DropDownList默认

       }

     }

 

DropDownList 是asp.net下拉控件绑定数据虽然可以使用SqlDataSource数据源进行绑定,但是这样页面及web.config文件中会生成很多的链接数据库的代码,这些代码不利于维护。

所以要自己对DropDownList进行绑定数据源:绑定的是整个表中的某列的信息。需要使用DataTable。

 

分成三层,这是在数据访问层的代码:

//此段代码为DLL层链接数据库返回DataTable 类型的数据给前台代码绑定数据(与DropDownList没有直接相连的关系) 给上面那段代码的DropDownList提供数据。

 

public DataTablegetPostMaxTypeIDList()

  {

     DataTable datatable = newDataTable();

     datatable = DBHelper.GetDataTable("selectPostTopListID,PostTopListName from PostTopList");

    datatable.Clone();

      return datatable;

   }

 

 

第二个DropDownList  需要根据第一个DropDownList  的onselectedindexchanged事件来绑定数据源。

第一个DropDownList 的属性必须设置为AutoPostBack =true;使用onselectedindexchanged事件才有效果

protected voidDDLtype_SelectedIndexChanged(object sender, EventArgs e)

 {

    int ID =Convert.ToInt32(DDLtype.SelectedValue);

    if (ID == 0)

     {

       //如果 ID = 0 则没有任何结果

      }else {

        this.DrdListminpost.DataSource =man.getPostMintypeList(ID);

        this.DrdListminpost.DataTextField = "TypeName";

        this.DrdListminpost.DataValueField = "PostTypeID";

        this.DrdListminpost.DataBind();              

      }

   }

 

 

 

 

 

 

 

 

 

 

七、IP归属地:新浪接口

俩个 script都要使用进去

   <script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script>

    <script type="text/javascript">

    //format?ºy¦¨¨?¤¦Ì??º? js/json

    //ip 要查¨¦IP 可选

    //反回?格式如下:

//var remote_ip_info = {"ret":1,"start":"117.89.35.0","end":"117.89.35.255","country":"\u4e2d\u56fd","province":"\u6c5f\u82cf","city":"\u5357\u4eac","district":"\u767d\u4e0b","isp":"\u7535\u4fe1","type":"","desc":""};

varname = (remote_ip_info["country"] +',' + remote_ip_info["province"]+ ',' + remote_ip_info['city']) //中国,江苏省,南京市,电信

   name=中国,江苏省,南京市,电信;

</script>

 

 

八、   JS获取DropDownList 的值

 

.Aspx

<div class="diaocha_towDIV">现在工作(省): </div><asp:DropDownList ID="DDL_sheng"runat="server"  CssClass="shengshi_div_width"

                   onselectedindexchanged="DDL_sheng_SelectedIndexChanged" AutoPostBack="True">  </asp:DropDownList>      

 

Js:

 <script type="text/javascript">

var select1 = document.all.<%=DDL_sheng.ClientID %>;

   var selectvalue =select1.options[select1.selectedIndex].value;

   alert(selectvalue);   //获取DropDownList 的值

</ script>

 

 

 

 

 

 

 

 

 

九、统计网站访问量:Global.asax

 

需要使用全局类Global.asax 在其中两个方法做统计 Session_Start和Session_End

1、Session_Start 

protected void Session_Start(objectsender, EventArgs e)

 {      //获取访问者IP"REMOTE_ADDR"固定写法

      stringipAddress = Request.ServerVariables["REMOTE_ADDR"];

      //stringlaiyuan=Page.Request.UserHostName //获取访问者的来源

string PcName =System.Environment.UserDomainName; //获取当前设备名称

      DateTimeipDatetime = DateTime.Now; //获取访问时间

      //BLL将访问者的IP保存到数据库中

      MianPageManagecont = new MianPageManage();

      cont.AddSessionUser(ipAddress, PcName,ipDatetime);

      stringpageurl = Request.Url.ToString();//获取用户访问的页面

      //判断是否访问默认页面:固定写法IPStat.aspx

      if(pageurl.EndsWith("IPStat.aspx"))

      { Application.Lock();//锁定变量

          //为页面访问变量+1

     Application["StatCount"]= int.Parse(Application["StatCount"].ToString()) + 1;

          Application.UnLock();//解锁

      }

     Session.Timeout = 10; //设定超时时间10分钟

       Application.Lock();//锁定变量 

//访问总人数+1 

Application["countSession"]= Convert.ToInt32(Application["countSession"]) + 1;

// Application["onlineWhx"] =(int)Application["onlineWhx"] + 1; //在线人数+1   

      Session["login_name"]= null;

      Application.UnLock();//解锁

}

 

2、Session_End

protected void Session_End(object sender, EventArgse)

   {

 // ¨会话结束时运行的代码

//注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 InProc 时,才会引发 Session_End 事件。           

//如果会话模式设置为 StateServer SQLServer,则不会引发该事件。

  //锁定变量

     Application.Lock();

      Application["onlineWhx"] = (int)Application["onlineWhx"] - 1; //在线人数-1

      Session["login_name"] = null;

      //解锁

     Application.UnLock();

   }

 

 

 

 

 

 

 

 

10、判断浏览器类型

 

//判断当前浏览器如果为IE就隐藏?<footerid="footer">

//还有一种后台¬判断

<script language="JavaScript" type="text/javascript">

if ((navigator.userAgent.indexOf('MSIE') >= 0) &&(navigator.userAgent.indexOf('Opera') <0))

  {

      $("#IE_DIVhide").hide();

  }

if ((navigator.userAgent.indexOf('MSIE') >= 0) 

    && (navigator.userAgent.indexOf('Opera') < 0)){

    alert('你是使用IE')

}else if (navigator.userAgent.indexOf('Firefox') >= 0){

    alert('你是使用Firefox')

}else if (navigator.userAgent.indexOf('Opera') >= 0){

    alert('你是使用Opera')

}else{

    alert('你是使用其他的浏览器浏览网页!')

}

</script>

 

 

 

 

 

 

 

 

 

 

 

 

11、JS 记录本页面地址,登录后跳转回来

普通页面JS:

varthisURL=encodeURIComponent(window.location.href);

               window.location.href="/UserLogin.aspx?thisurl="+ thisURL + "";

JS判断 到用户未登录的情况下可以使用该代码,记录本页面的地址,然后转去登录页面,在登录页面记录从哪里跳过来的地址,登录后在跳转回去

 

 

登录页面JS:

 

            //获取从什么页面跳转过来的地址

string  thisurl =Server.UrlDecode(Request["thisurl"].ToString());

                   if (name != ""&& pwd != "")

                   {

                       user = userman.UserSubmit(name, pwd);

                       if (user != null)

                       {

                            Session["userid"] = user.UserID ;

                           Response.Redirect(thisurl + "&&id=" + user.UserID);

                       }

                       else

                       {

                            this.lblErrer.Text = "账户名或密码错误";

                       }

                   }

 

 

 

 

 

 

 

 

 

12、使用Chart控件绑定数据;

   Aspx前台页面:

<asp:Chart ID="Chart1" runat="server" Height="282px" Width="521px">  

 <Series>  <asp:Series Name="Series1"ChartType="Bar"XValueMember="TypeName"YValueMembers="CountValue"Label="#PERCENT{P}">

 <SmartLabelStyleIsMarkerOverlappingAllowed="True" IsOverlappedHidden="False" />

     </asp:Series>   </Series> 

 <ChartAreas>

   <asp:ChartArea Name="ChartArea_Post">

    <AxisX><MajorGrid Enabled="false"/></AxisX>

    <AxisY><MajorGrid Enabled="false"/></AxisY>

    </asp:ChartArea>

    </ChartAreas>

</asp:Chart>

 

 

 

 

 

.cs后台代码:

 

public void getpostReportList()

{

   DataTabledata = new DataTable();

   data = mian.getPostTypeReport();

if (data != null)

   {

     this.Chart1.DataSource= data;             

this.Chart1.ChartAreas["ChartArea_Post"].AxisY.Title = "论坛帖子统计";

 

//使用®?StringAlignment.Far;需要引用:using System.Drawing;

//设置轴标题的名称所在位置位后

this.Chart1.ChartAreas["ChartArea_Post"].AxisY.TitleAlignment =StringAlignment.Far;

    this.Chart1.DataBind();//绑定数据

     }

}

 

 

效果:

 

 

 

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屎涂行者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值