Rendering ASP.NET Script References into the Html Header
http://weblog.west-wind.com/posts/2010/Jan/02/Rendering-ASPNET-Script-References-into-the-Html-Header
主頁 » ASP
Category Archives: ASP
[ASP.Net] 跨網頁傳遞資料
http://www.dotblogs.com.tw/antony77/archive/2010/04/05/14417.aspx
使用 HttpWebRequest 來作
http://www.coolsun.idv.tw/modules/xhnewbb/viewtopic.php?topic_id=1273
http://coding.anyun.tw/2012/02/22/using-httpwebrequest-post-data/
http://www.dotblogs.com.tw/joysdw12/archive/2012/12/04/85380.aspx
//取得當前網址及路徑 string urlPath = ""; string[] segments = Request.Url.Segments; for (int i = 0; i <= segments.Length - 2; i++) { urlPath = urlPath + segments[i]; } string bankUrl = string.Format("http://{0}:{1}{2}RedirectHandler.ashx", Request.Url.Host, Request.Url.Port, urlPath); String parm = "BillerSeqNo=2014052361106509&Currency=NTD&Amount=1235500"; HttpTool http = new HttpTool(); NameValueCollection ackColl = http.SendRequest(bankUrl, parm); if (ackColl == null) { string alertScript = "處理失敗,請重新操作!<br />(ACK Null)"; return; } string parm1 = ackColl["BillerSeqNo"]; string parm2 = ackColl["Currency"];
HttpTool.cs
using System; using System.Web; using System.Text; using System.Net; using System.IO; using System.Collections.Specialized; public class HttpTool { public NameValueCollection SendRequest(string iTargetUrl, string iParam) { //此處的編碼要注意,是否會錯編 "&" 及 "=" 符號 byte[] postData = Encoding.UTF8.GetBytes(iParam); HttpWebRequest request = HttpWebRequest.Create(iTargetUrl) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Timeout = 10000; request.ContentLength = postData.Length; request.AllowAutoRedirect = false; // 禁止重新導向網頁 // 寫入 Post Body Message 資料流 using (Stream st = request.GetRequestStream()) { st.Write(postData, 0, postData.Length); } NameValueCollection qsColl = null; // 取得回應資料 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.Found) // 判斷是否為 302 { // 取得返回的Header中的Location參數,Location含有重新導向的網址資訊 Uri redirectUrl = new Uri(response.Headers["Location"]); // 回傳全部參數的集合 // Parse the query string variables into a NameValueCollection. qsColl = HttpUtility.ParseQueryString(redirectUrl.Query, Encoding.UTF8); } } return qsColl; } }
RedirectHandler.ashx
using System; using System.Web; using System.Collections.Specialized; public class RedirectHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string url = @"http://127.0.0.1/AppPayCB/PaymentResult.aspx"; NameValueCollection pColl = context.Request.Params; string all = string.Format(@"{0}?BillerSeqNo={1}&Currency={2}", url, pColl["BillerSeqNo"], pColl["Currency"]); context.Response.Redirect(all); } public bool IsReusable { get { return false; } } }
在 visual studio 2010 安裝 JQuery
- 必需先安裝 NuGet Package Manager。透過 Visual Studio 2010 的擴充管理員進行安裝,開啟後點選 線上圖庫 分類,然後在右上角的搜尋文字框輸入 NuGet 就可以查到 NuGet Package Manager 項目。
- 開啟專案,工具 >> 程式庫套件管理員 >> 管理方案的 NuGet 套件,在視窗右上角輸入 jQuery 進行搜尋,然後找到 jQuery x.x.x 之後點選 Install 進行安裝。
- 如果安裝失敗,可以由:工具 >> 程式庫套件管理員 >> 套件管理器主控台,開啟之後的視窗其實是 PowerShell 的執行視窗,輸入的是 PowerShell 的 Cmdlets,例如安裝 jQuery 套件的指令就是 Install-Package jQuery。
詳細的說明請參考微軟的網頁:介紹好用 Visual Studio 2010 擴充套件
ASP C# 以POST方式傳送參數並轉向瀏覽網頁
在 ASP.Net C# 中,以POST方式傳送參數到別的網頁,並轉向瀏覽此網頁
基本上使用 Redirect and POST 此篇文章的方法
http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET
在使用 UpdatePanel 的區域內,則使用以上方法之 function 的 Control,需對 ScriptManager 做註冊的動作。
其用意為,以 PostBackTrigger 方法,更新整個頁面。否則,在執行階段會有 thread 的 error exception。
以 LinkButton 的 click 事件為例
protected void lbtnETPR_Click(object sender, EventArgs e) { LinkButton lbtnETPR = sender as LinkButton; GridViewRow gvRow = lbtnETPR.NamingContainer as GridViewRow; string adno = gvRow.Cells[0].Text; DataRow[] row = pEventDs.Tables["pEvents"].Select("ammadno = '" + adno + "'"); string crtno = row[0]["ammpatno"].ToString(); NameValueCollection data = new NameValueCollection(); data.Add("crtno", crtno); data.Add("hospno", "01"); data.Add("notshowfg", "Y"); HttpHelper.RedirectAndPOST(this.Page, "http://192.168.1.1/tpr/tprshow.aspx", data); }
靜態的註冊方式:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:LinkButton ID="lbtnETPR" runat="server" OnClick="lbtnETPR_Click" Text="ETPR" /> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="lbtnETPR" /> </Triggers> </asp:UpdatePanel>
如果,control 是位於 GridView 元件的自定欄位(TemplateField)內,則需使用動態的註冊方式:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="GridView1" runat="server" DataKeyNames="ammadno" OnRowDataBound="GridView1_RowDataBound" > <Columns> <asp:TemplateField HeaderText="Order"> <ItemTemplate> <asp:LinkButton ID="lbtnETPR" runat="server" OnClick="lbtnETPR_Click" Text="ETPR" /> <asp:LinkButton ID="lbtnPrscrp" runat="server" Onclick="lbtnPrscrp_Click" Text="Order" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType != DataControlRowType.DataRow) return; //相當於在 aspx 中, 加入 UpdatePanel 之 PostBackTrigger 的作用 ToolkitScriptManager1.RegisterPostBackControl(e.Row.FindControl("lbtnETPR")); }