快速上手wap網站開發

 

近來工作比較緊張,一直想寫一些東西,無奈沒有時間,現在開發的市移動wap論壇終於告一段落,現在將開發過程簡單記錄一下,以備日後參考,都是一些簡單的使用過程,可爲初次接觸wap開發的提供一點點參考,高手可以忽略。

  開發工具:vs2008     模擬器:vs自帶仿真管理器   framework版本:2.0

一。配置環境:

     vs2008中已經沒有了新建wap的選項,所需的wap模板需要從網上下載:

    下載地址:http://www.brsbox.com/filebox/down/fc/911fb64660cbf75f42146ac021d642fe,這是我的網絡硬盤,下載下來是一個ASPNETMobileTemplates.rar的文件,根據裏面的說明將文件拷貝到所需的文件夾。

       模擬器可用openware((官方免費註冊下載地址)http://developer.openwave.com/dvl/tools_and_sdk/phone_simulator/),也可用vs自帶的設備仿真器(要先安裝ActiveSync,),在工具—設備仿真管理器—選擇pocket pc 2003中的pocket pc 2003 se仿真器—右鍵點解連接,然後再右鍵點擊插入底座,運行後即可使用,不過在仿真管理器中地址不要用local,要用本機ip地址

 

二。建立數據庫:

       數據庫採用sqlserver,建立一個名爲wapDB的數據庫,如下圖:

 

      

       然後添加一個用戶表userinfo,如下圖:

 

 

      

       爲數據庫添加一條記錄,如下圖:

 

 

       建立表document,用來存儲發佈的文章,表結構如下圖:

 

       先爲document表添加20條數據,用來顯示,如下圖:

 

       至此,數據庫建立完畢,下面我們將採用vs2008來具體開發。

 

三。建立工程,開始開發:

        首先,我們建立一個testWap的項目,如下圖:

 

 

       將新建項目默任生成的default.aspx刪除,新建一個login.aspx的mobile web form模板(在第一步環境配置中按照說明將ASPNETMobileTemplates.rar中的文件拷貝到各個文件夾後,就會在新建項目中最下面的模板中顯示mobile模板了),如下圖:

        建立好以後,按照上述方法再添加一個index.aspx的文件。

        至此,我們所需的文件已經全部建立完成,login.aspx用來登錄,登錄後到index.aspx頁面,此頁面用來分頁顯示document文章表中的內容,並且可以添加文章記錄。(注意,做好網頁後,需要在記事本中將我們剛纔建立的login.aspx、index.aspx打開重新保存一下,保存編碼改爲utf-8,覆蓋原文件即可,這樣做是因爲項目採用utf-8編碼,如果不這樣的話,頁面含有中文的話就會顯示爲亂碼。),如下圖:

 

       然後開始編碼,具體編碼和asp.net中的編碼過程一樣,不同的就是換成了mobile控件,這裏需要注意的vs下開發wap不支持可視化設計,我們只能在後臺手工編碼,當添加<mobile>控件的時候,只要打上<m就會出現你所需要的mobile控件,mobile控件的具體有哪些和都有什麼屬性請參考其他文檔,日後若有時間,我會將mobile控件的使用說明詳細介紹一下,這裏給大家引薦一個網址,這裏面有mobile控件的介紹和使用說明,http://www.wapkf.com/article/aspnet-wap/Article_005_3.html

我們這裏只用到了objectlist控件和textbox、textview控件以及command、Label控件,command控件其實就是button按鈕,在mobile裏叫command。

       這裏我們建立三個文件:

        login.aspx:登錄頁面

        index.aspx:分頁顯示文章頁面,帶有快速發表

        view.aspx:顯示文章具體內容頁面

三個頁面源代碼:

login.aspx     前臺代碼具體如下:

 

  1. <%@ Page Language="C#" AutoEventWireup="true" Inherits="testWap.login" Codebehind="login.aspx.cs" %>
  2. <%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <body>
  5. <mobile:Form id="Form1" runat="server">    <!--表單-->
  6.       <mobile:Label ID="z1" Runat="server" Font-Size="Large" ForeColor="#3333cc">登錄窗口</mobile:Label>
  7.       <!--lbl_out:信息標籤,初始隱藏,登錄失敗後或退出系統時顯示信息-->
  8.       <mobile:Label ID="lbl_out" Runat="server" ForeColor="Red" Visible="false"></mobile:Label> 
  9.       用戶名:<br />
  10.       <mobile:TextBox ID="tb_User" Runat="server" Size="10" ></mobile:TextBox><!--用戶名輸入框:tb_User-->
  11.       密碼:<br />
  12.       <mobile:TextBox ID="tb_Pwd" Runat="server" Size="10" Password="True"></mobile:TextBox><!--密碼輸入框:tb_Pwd-->
  13.       <mobile:Command ID="Button1" Runat="server" OnClick="Button1_OnClick" >登錄</mobile:Command><!--登錄按鈕-->
  14. </mobile:Form>
  15. </body>
  16. </html>

login.aspx.cs     後臺代碼具體如下:

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.Mobile;
  8. using System.Web.SessionState;
  9. using System.Web.UI;
  10. using System.Web.UI.MobileControls;
  11. using System.Web.UI.WebControls;
  12. using System.Web.UI.HtmlControls;
  13. using System.Data.SqlClient;
  14. namespace testWap
  15. {
  16.     public partial class login : System.Web.UI.MobileControls.MobilePage
  17.     {
  18.         protected void Page_Load(object sender, EventArgs e)
  19.         {
  20.             #region 系統退出時將信息標籤lbl_out賦值並且顯示
  21.             if (Session["loginOutInfo"] != null)
  22.             {
  23.                 string outInfo = Session["loginOutInfo"].ToString();
  24.                 this.lbl_out.Text = outInfo;
  25.                 this.lbl_out.Visible = true;
  26.                 Session.Clear();
  27.             }
  28.             #endregion
  29.         }
  30.         /// <summary>
  31.         /// 登錄驗證
  32.         /// </summary>
  33.         /// <param name="sender"></param>
  34.         /// <param name="e"></param>
  35.         protected void Button1_OnClick(object sender, EventArgs e)
  36.         {
  37.             string username = this.tb_User.Text.Trim();
  38.             string userpwd = this.tb_Pwd.Text.Trim();
  39.             string strCon = "Data Source=(local);Database=wapDB;Uid=sa;Pwd=zxkj";
  40.             string strSql = "select * from userinfo where user_name='"+username+"' and user_pwd='"+userpwd+"'";
  41.             SqlConnection conn = new SqlConnection(strCon);
  42.             conn.Open();
  43.             SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
  44.             DataSet ds = new DataSet();
  45.             da.Fill(ds);
  46.             conn.Close();
  47.             int rowCount = ds.Tables[0].Rows.Count;
  48.             if (rowCount > 0)
  49.             {
  50.                 Session["username"] = ds.Tables[0].Rows[0]["user_name"].ToString().Trim();
  51.                 Response.Redirect("index.aspx");
  52.             }
  53.             else
  54.             {
  55.                 this.lbl_out.Text = "用戶名密碼錯誤,請重新登錄!";
  56.                 this.lbl_out.Visible = true;
  57.             }
  58.         }
  59.     }
  60. }

index.aspx     前臺代碼具體如下:

  1. <%@ Page Language="C#" AutoEventWireup="true" Inherits="testWap.index" Codebehind="index.aspx.cs" %>
  2. <%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <body>
  5. <mobile:Form id="Form1" runat="server">    <!--表單-->
  6. <mobile:Label ID="lbl_uname" Runat="server"></mobile:Label>
  7. <mobile:Label ID="wt" Runat="server" Font-Size="Large" ForeColor="Red">文章列表</mobile:Label>
  8. <mobile:ObjectList ID="ObjectList1" Runat="server"><!--ObjectList控件-->
  9.   <DeviceSpecific>
  10.     <Choice>
  11.       <ItemTemplate>
  12. <mobile:Link Runat="server" Text='<%# ((ObjectListItem)Container)["doc_title"] %>' NavigateUrl='<%# "view.aspx?id="+((ObjectListItem)Container)["doc_id"]%>' ID="Title" NAME="Title" Wrapping="Wrap">
  13. </mobile:Link>
  14.       </ItemTemplate>
  15.     </Choice>
  16.   </DeviceSpecific>
  17. </mobile:ObjectList>
  18. <br />
  19. <mobile:Label id="lbl_page" runat="server" Visible="False">1</mobile:Label><!--頁碼:lbl_page-->
  20. <mobile:Label id="lbl_pagecount" runat="server" Visible="False">1</mobile:Label><!--總頁數:lbl_pagecount-->
  21. <mobile:Link ID="lnk_top" Runat="server" BreakAfter="False">首頁 | </mobile:Link><mobile:Link id="lnk_pre" runat="server"> 上一頁</mobile:Link>
  22. <mobile:Link id="lnk_end" runat="server" Visible="False" BreakAfter="false">尾頁 | </mobile:Link><mobile:Link id="lnk_next" runat="server"> 下一頁</mobile:Link>
  23. <br />
  24. <mobile:Label id="lbl_fabu" Runat="server">發佈文章:</mobile:Label>
  25. <mobile:Label ID="lbl_error" Runat="server" Visible="false" ForeColor="Red"></mobile:Label>
  26. <mobile:TextBox ID="tb_title" Runat="server"></mobile:TextBox>
  27. <mobile:TextBox ID="tb_content" Runat="server"></mobile:TextBox>
  28. <mobile:Command ID="Button2" Runat="server" OnClick="Button2_OnClick" BreakAfter="false">發表</mobile:Command><mobile:Command ID="Button1" Runat="server" OnClick="Button1_OnClick">退出</mobile:Command>
  29. </mobile:Form>
  30. </body>
  31. </html>

index.aspx.cs     後臺代碼具體如下:

 

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.Mobile;
  8. using System.Web.SessionState;
  9. using System.Web.UI;
  10. using System.Web.UI.MobileControls;
  11. using System.Web.UI.WebControls;
  12. using System.Web.UI.HtmlControls;
  13. using System.Data.SqlClient;
  14. namespace testWap
  15. {
  16.     public partial class index : System.Web.UI.MobileControls.MobilePage
  17.     {
  18.         protected void Page_Load(object sender, EventArgs e)
  19.         {
  20.             if (Session["username"] == null)
  21.             {
  22.                 Session["loginOutInfo"] = "登錄時間到,請重新登錄!";
  23.                 Response.Redirect("login.aspx");
  24.             }
  25.             this.lbl_uname.Text = "歡迎您:"+(string)Session["username"];
  26.             if (Session["ok"] != null)
  27.             {
  28.                 this.lbl_error.Text = "發表成功!";
  29.                 this.lbl_error.Visible = true;
  30.                 Session["ok"] = null;
  31.             }
  32.             if (!IsPostBack)
  33.             {
  34.                 Bind();
  35.             }
  36.         }
  37.         private void Bind()
  38.         {
  39.             string rPage = Request.QueryString["Page"];
  40.             int page = 1;
  41.             if (rPage != null)
  42.             {
  43.                 try
  44.                 {
  45.                     page = int.Parse(rPage);
  46.                 }
  47.                 catch
  48.                 {
  49.                     page = 1;
  50.                 }
  51.             }
  52.             Session["page"] = page;
  53.             PagedDataSource ps = new PagedDataSource();
  54.             string strCon = "Data Source=(local);Database=wapDB;Uid=sa;Pwd=zxkj";
  55.             string strSql = "select * from document order by doc_id desc";
  56.             SqlConnection conn = new SqlConnection(strCon);
  57.             conn.Open();
  58.             SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
  59.             DataSet ds = new DataSet();
  60.             da.Fill(ds);
  61.             conn.Close();
  62.             ps.DataSource = ds.Tables[0].DefaultView;
  63.             ps.AllowPaging = true;
  64.             ps.PageSize = 5;
  65.             ps.CurrentPageIndex = page - 1;
  66.             this.lnk_top.Visible = true;
  67.             this.lnk_pre.Visible = true;
  68.             this.lnk_next.Visible = true;
  69.             this.lnk_end.Visible = true;
  70.             this.lnk_top.NavigateUrl = "index.aspx?page=1";
  71.             this.lnk_pre.NavigateUrl = "index.aspx?page=" + (page - 1);
  72.             this.lnk_next.NavigateUrl = "index.aspx?page=" + (page + 1);
  73.             this.lnk_end.NavigateUrl = "index.aspx?page=" + ps.PageCount;
  74.             if (page == 1)
  75.             {
  76.                 this.lnk_top.Visible = false;
  77.                 this.lnk_pre.Visible = false;
  78.             }
  79.             if (page == ps.PageCount)
  80.             {
  81.                 this.lnk_next.Visible = false;
  82.                 this.lnk_end.Visible = false;
  83.             }
  84.             if (ps.PageCount == 1)
  85.             {
  86.                 this.lnk_top.Visible = false;
  87.                 this.lnk_pre.Visible = false;
  88.                 this.lnk_next.Visible = false;
  89.                 this.lnk_end.Visible = false;
  90.             }
  91.             this.lbl_pagecount.Text = Convert.ToString(ps.PageCount);
  92.             this.ObjectList1.DataSource = ps;
  93.             this.ObjectList1.DataBind();
  94.         }
  95.         protected void Button2_OnClick(object sender, EventArgs e)
  96.         {
  97.             string title = this.tb_title.Text.Trim();
  98.             string content = this.tb_content.Text.Trim();
  99.             if (title == "" || title == null || content == "" || content == null)
  100.             {
  101.                 this.lbl_error.Text = "文章標題或內容不能爲空!!";
  102.                 this.lbl_error.Visible = true;
  103.                 return;
  104.             }
  105.             string strSql = "insert document values('" + title + "','" + content + "')";
  106.             string strCon = "Data Source=(local);Database=wapDB;Uid=sa;Pwd=zxkj";
  107.             SqlConnection conn = new SqlConnection(strCon);
  108.             conn.Open();
  109.             SqlCommand com = new SqlCommand(strSql, conn);
  110.             com.ExecuteNonQuery();
  111.             conn.Close();
  112.             Session["ok"] = "ok";
  113.             Response.Redirect("index.aspx");
  114.         }
  115.         protected void Button1_OnClick(object sender, EventArgs e)
  116.         {
  117.             try
  118.             {
  119.                 Session.Clear();
  120.                 Session["loginOutInfo"] = "退出成功!";
  121.             }
  122.             catch
  123.             {
  124.                 Session["loginOutInfo"] = "退出成功!";
  125.             }
  126.             Response.Redirect("login.aspx");
  127.         }
  128.     }
  129. }

view.aspx    前臺代碼具體如下:

 

 

<%@ Page Language="C#" AutoEventWireup="true" Inherits="testWap.view" Codebehind="view.aspx.cs" %> <%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %> <html xmlns="http://www.w3.org/1999/xhtml" > <body> <mobile:Form id="Form1" runat="server"> <mobile:Label ID="Label1" Runat="server" ForeColor="#0000cc" >帖子內容</mobile:Label> <mobile:Label ID="z1" Runat="server" ForeColor="#0066ff"> 標題:</mobile:Label> <mobile:TextView ID="tv_title" Runat="server" Wrapping="Wrap"></mobile:TextView> <mobile:Label ID="z2" Runat="server" ForeColor="#0066ff"> 內容:</mobile:Label> <mobile:TextView ID="tv_Content" Runat="server" Wrapping="Wrap"></mobile:TextView> <mobile:Link ID="lnk_FanHui" Runat="server" BreakAfter="false">返回上層</mobile:Link> |  <mobile:Command ID="Command1" Runat="server" OnClick="Button1_OnClick">退出</mobile:Command> </mobile:Form> </body> </html>

view

.aspx.cs     後臺代碼具體如下:
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.Mobile;
  8. using System.Web.SessionState;
  9. using System.Web.UI;
  10. using System.Web.UI.MobileControls;
  11. using System.Web.UI.WebControls;
  12. using System.Web.UI.HtmlControls;
  13. using System.Data.SqlClient;
  14. namespace testWap
  15. {
  16.     public partial class view : System.Web.UI.MobileControls.MobilePage
  17.     {
  18.         protected void Page_Load(object sender, EventArgs e)
  19.         {
  20.             if (Session["username"] == null || Session["page"] == null)
  21.             {
  22.                 Session["loginOutInfo"] = "登錄時間到,請重新登錄!";
  23.                 Response.Redirect("login.aspx");
  24.             }
  25.             int docid = int.Parse(Request["id"].ToString());
  26.             string strCon = "Data Source=(local);Database=wapDB;Uid=sa;Pwd=zxkj";
  27.             string strSql = "select * from document where doc_id=" + docid;
  28.             SqlConnection conn = new SqlConnection(strCon);
  29.             conn.Open();
  30.             SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
  31.             DataSet ds = new DataSet();
  32.             da.Fill(ds);
  33.             conn.Close();
  34.             this.tv_title.Text = ds.Tables[0].Rows[0]["doc_title"].ToString();
  35.             this.tv_Content.Text = ds.Tables[0].Rows[0]["doc_content"].ToString();
  36.             int page = (int)Session["page"];
  37.             this.lnk_FanHui.NavigateUrl = "index.aspx?page="+Convert.ToString(page);
  38.         }
  39.         protected void Button1_OnClick(object sender, EventArgs e)
  40.         {
  41.             try
  42.             {
  43.                 Session.Clear();
  44.                 Session["loginOutInfo"] = "退出成功!";
  45.             }
  46.             catch
  47.             {
  48.                 Session["loginOutInfo"] = "退出成功!";
  49.             }
  50.             Response.Redirect("login.aspx");
  51.         }
  52.     }
  53. }

以上程序完畢,這是個簡單的發表分頁的wap項目,比較簡單,僅供參考,內容還有很多不完善之處,比如登錄的驗證需要完善,數據庫連接可以放到config中,這裏只是爲了簡單而寫成這樣的。

 

在wap開發中的一點心得:

     需將文件另保存爲utf-8格式。

     wap控件每個默認後面自動分行,若想不分行需將控件的BreakAfter屬性置爲False。

     wap控件中的內容自動分行:Wrapping="Wrap"

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章