TOP

[C#] Post Data With New Window - 2

前一篇Post資料的方式,會遇到一種狀況,當輸入或輸出的資料需要在Page_Load從資料庫或是其他的程式判斷之後才填寫,或是要使用DropDownList選擇項目再Post時,就會有點難題。

因為利用另外一個偷吃步的方式來達成相同的效果。


1.在要送資料的Page_Load內,建立Session儲存要傳送的資料,利用Window.open開啟PostData.aspx。

    protected void Page_Load(object sender, EventArgs e)
    {

            Session.Add("name", Name.Text.Trim());

            string strUrl = string.Format("{0}/PostData.aspx", ConfigurationManager.AppSettings["WebRootUrl"]);
            ScriptManager.RegisterStartupScript(this, this.GetType(),
                "recordOpenUrl",
                "window.open('" + strUrl + "','recordOpenUrl','" + "" + "');", true);

     }


2.建立一個新的PostData.aspx頁,準備讀取session資料,並且利用JavaScript自動submit該資料的form。


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
<script language="JavaScript" type="text/javascript" defer>
    document.formid.submit()
</script>
   
</head>
<body>
    <form name="formid" method="post" action="https://www.streamwing-gridow.com/signup/sfdc">
        <div>
            <input id = "name"  type="hidden" name="first_name" value=" <%=Session["name"] %>" />
        </div>
    </form>
</body>
</html>


特別注意:此為自動送出資料的關鍵。

<script language="JavaScript" type="text/javascript" defer>
    document.formid.submit()
</script>

3.chrome及firefox的瀏覽器上執行此JavaScript
要在body內加上onload
<body onload="document.formid.submit()">

TOP

[C#]Post Data With New Window

在C#內要傳送method為Post的資料至其他頁面,且還要是新的視窗。

1.主要還是利用javascript的方式做處理
放javascript方式有兩種

(1)直接將javascript放ASPX內的Head裡面
<head runat="server">

    <script type="text/javascript">    



 function openWindowWithPost(url, name, keys, values) { //開新視窗,這邊我不指定新視窗的網址
     var newWindow = window.open("", name, "height=700, width=600, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no");
     if (!newWindow) return false;
     var html = "";
     html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
     if (keys && values && (keys.length == values.length))
         for (var i = 0; i < keys.length; i++)
         html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
     html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";
     newWindow.document.write(html);
     return newWindow;
 }

 function Post(url,title) { //這個function會把網頁中所有的form找出來,並且找出每個form所包含的elements
     var keys = [];
     var values = [];
     for (var i = 0; i < document.forms.length; i++) {
         for (var j = 0; j < document.forms[i].elements.length; j++) {
             if (document.forms[i].elements[j].name != undefined &&
                document.forms[i].elements[j].value != undefined) {
                 keys[keys.length] = document.forms[i].elements[j].name;
                 values[values.length] = document.forms[i].elements[j].value;
             }
         }
     }
     openWindowWithPost(url, title, keys, values);
 }
</script>
</head>

(2)
將javascript的程式放仙tools.js內,aspx看起來也比較乾淨。
<head runat="server">
    <script type="text/javascript" src="Js/tools.js"></script>
</head>

2.在C#的Codebehind程式碼內。
    protected void Page_Load(object sender, EventArgs e)
    {

      Page.RegisterStartupScript("Show", "<script language=\"JavaScript\">Post('網址','名稱');</script>");
    }

3.HTML內的輸入方式要改為input,不可以使用textbox,不然javascript會抓不到資料。
                <input id = "name"  type="text" name="name" value="name" />

參考資料:
http://www.dotblogs.com.tw/puma/archive/2008/09/03/5288.aspx
http://takamai.pixnet.net/blog/post/33804587-asp.net-%E9%85%8D%E5%90%88javascript%E9%96%8B%E6%96%B0%E8%A6%96%E7%AA%97%E4%B8%A6%E4%B8%94%E4%BD%BF%E7%94%A8post%E5%82%B3%E9%81%9E



TOP

[Share]撰寫程式的觀念

最近在寫程式的時後,一直被主管再教育撰寫程式的觀念

分享給大家這個觀念,有的時後觀念比會跑的程式還要重要!!!

目前Microsoft Visual Studio的程式已經開發至2012的版本,程式中使用到的的物件或屬性也越來越多且簡單,但也相對的因為越來越多,造成我常常只會使用熟悉的去撰寫程式。

1.時時要學習及注意官方改變的物件及屬性。

2.撰寫程式時要懂得先查詢本身物件是否已經提供屬性使用,不要像我常傻傻的去找有沒有什麼其他的物件可以來做,兜了一大圈浪費一堆時間。

3.確實瞭解PM的需求,且再次確認PM說的是否正確,因為常常發生PM或測試工程師說的狀況跟工程師測出來的結果不相同。

4.撰寫程式時,需要考慮到每一個可能造成程式的錯誤的原因。
   我想應該有人會說這不是廢話嗎!!
拿個例子來說:如果程式要從web.config取得某一個預設資料時,又因為人的疏忽在Web.config的預設值沒有加入該數值,造成程式抓不到資料時所造成的錯誤!


TOP

[C#]GridView 排序及箭頭方向

不使用SqlDataSource的Sorting方式

因為資料來源不是從SqlDataSource取得,所以Sorting的時後要自已用ViewState去記錄選擇的是那個LinkButton及方向,如果使用SqlDataSource就不會有這個問題。

<asp:GridView ID="GridView1" runat="server"
                        onrowdatabound="GridView1_RowDataBound" AllowPaging="True"
                        onpageindexchanging="GridView1_PageIndexChanging"
                        AutoGenerateColumns="False"
                        CellPadding="4" AllowSorting="true"
                        onsorting="GridView1_Sorting"
                        onrowcreated="GridView1_RowCreated">
</asp:GridView>




 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell tc in e.Row.Cells)
            {
                if (tc.HasControls() == true)
                {
                    if (((LinkButton)tc.Controls[0]).Text == "Duration")
                    {
                        ((LinkButton)tc.Controls[0]).Text = GetLocalResourceObject("Duration").ToString();
                    }
                    else if (((LinkButton)tc.Controls[0]).Text == "RecordingName")
                    {
                        ((LinkButton)tc.Controls[0]).Text = GetLocalResourceObject("RecordingName").ToString();
                    }
                    else if (((LinkButton)tc.Controls[0]).Text == "FileSize")
                    {
                        ((LinkButton)tc.Controls[0]).Text = GetLocalResourceObject("FileSize").ToString();
                    }

                    if (((LinkButton)tc.Controls[0]).CommandArgument == ViewState["SortDir"].ToString())
                    {
                        if (ViewState["mySorting"].ToString() == "ASC")
                        {
                            tc.Controls.Add(new LiteralControl("↓"));
                        }
                        else
                        {
                            tc.Controls.Add(new LiteralControl("↑"));
                        }
                    }
                }
            }
        }


    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["mySorting"] == null)
        {
            e.SortDirection = SortDirection.Ascending;
            ViewState["mySorting"] = "ASC";
        }
        else
        {
            if (ViewState["mySorting"].ToString() == "ASC")
            {
                e.SortDirection = SortDirection.Descending;
                ViewState["mySorting"] = "DESC";
            }
            else
            {
                e.SortDirection = SortDirection.Ascending;

                ViewState["mySorting"] = "ASC";
            }

            ViewState["SortDir"] = e.SortExpression;
        }
        GetGridView1Data();
    }

TOP

[C#]取得兩個字串之間的字串



string strNewWord = "";
string strWord = "<ABC>WORD</ABC>";

//取得兩個字串之間的字串
strNewWord = strWord.Split(new String[] { "</ABC>", "<ABC>" });