2015年9月15日 星期二

[JQuery]取得當下DropDownList選取的值(不需Postback)

$('#CountyDDL option:selected').val();
↑這方法想取得選取後的值, 必須要Postback
這不是我想要的

如果想隨時切換, 請愛用this (radio button也適用)
   <script type="text/javascript">
            $(document).ready(function () {
                var index = $("#FormView1_Label").text() - 1; //利用此變數, 控制顯示的時期
                if (index == -1) index = 0;
                    $("#FormView1_Caption").text(index);
                $("#FormView1_TextBox").change(function () {
                    index = $(this).val()-1; //此處使用「$(this).val()」是重點。「-1」單純是運算需要
                    $("#FormView1_Caption").text(index);
                });
            })

2015年9月14日 星期一

[W7]開啟新注音輸入法中隱藏的缺字

遇到一位友人想要在新注音輸入「雫」
由於不確定新注音的讀音,於是用「輸入法整合器」去查詢拼法

OK, 確認是「ㄋㄚˇ」... 但是照拼音卻找不到字,這時能確定新注音中有,只是要去哪開啟!

開啟罕見字的方法

  • 語言列上右鍵→點「設定值」
  • 選好「新注音」→再點「內容」



  • 在「一般」的頁籤下方,接近底部有個「字元集設定」


  • 將最後一列「允許使用中文標準交換碼全字庫 (CNS11643)所定義的字根輸入」勾選後點確定就完成了




如果有很特殊的需求,可以把能打勾的都勾一勾,就有更多罕見字可以使用
而且會很貼心的用顏色提醒

2015年9月6日 星期日

[ASP.NET]利用各列的button取得該列的其他參數

「當點下該列的button控制項後, 可以取得第一欄的值(未轉換成template field)」

要做這效果, 分為兩大項
  1. 取得該button所在的Row或取得Row Index
  2. 取得該Row的各Cell資料
1. 利用「Click」事件取得該Button所在Row
 protected void Button1_Click(object sender, System.EventArgs e)
    {
        //Get the button that raised the event
        Button btn = (Button)sender;

        //Get the row that contains this button
        GridViewRow gvr =(GridViewRow)btn.NamingContainer;
    } 

重點在於先sender轉型成button, 在利用.NamingContainer實作該row
如果想要取得Row Index, 則用「int rowindex = gvr.RowIndex;」

2.取得該Row中的指定Cell資料
單純文字: gvr.Cells[0].Text;
使用Template的特定控制項:gvr.FindControl("Button1");


可用的方法還挺多的(這裡此論CodeBehind), 上面是最簡單的方法
以下就來作補充說明
資料來源:Get GridView RowIndex upon button click

除了用「Click」事件外另外有「Command」及「SelectedIndexChange」這兩種常用的事件
取得方法都不太一樣
「Command」事件(適合用在性質相似但功能略有不同的控制項)
這是先取得Row Index, 再取得Row
要使用這事件,必須要在html中設定「CommandName」及「CommandArgument」

  • CommandName:隨喜好設定
  • CommandArgument:<%# Container.DataItemIndex %>

接下來到*.cs中轉寫function
protected void Expo_Command(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "forCSR")
        {
            //Get rowindex
            int rowindex =Convert.ToInt32(e.CommandArgument);
            //Get Row
            GridViewRow gvr = GridView1.Rows[rowindex];
        }
    }

「SelectedIndexChange」事件
不外乎是使用「.NamingContainer」
DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {

        //Get the dropdownlist that raised the event
        DropDownList ddl = (DropDownList)sender;

        //Get the row that contains this dropdown
        GridViewRow gvr = (GridViewRow)ddl.NamingContainer;

        //Get rowindex
        int rowindex = gvr.RowIndex;
    }  



延伸閱讀:
Q:「RowIndex」和「DataItemIndex」有什麼差別?
A:都可以用來取得GridView中的index
差別在於RowIndex是每頁分開算(一律從 0 ~ n 的相對index)
而DataItemIndex則是位於Binding中的絕對index
資料來源:What is difference between RowIndex and DataItemIndex?

NamingContainer取得 GridView的列數(RowIndex)、對應的資料表 這一列的主索引鍵
GridView RowCommand 事件

2015年8月20日 星期四

[ASP.NET]防止使用者用參數進入無權限的網頁

大致分為四個階段

  1. 建立SQL連線
  2. 利用adapter將資料到入DataTable
  3. 判斷DataTable的某欄位資料是否匹配
  4. 決定是否跳轉到特定頁面


由於會使用到ADO.NET
務必要記得要用「using System.Data.SqlClient;」

會回傳boolean的值(階段1~3)
protected bool isProjectStop()
{
    string connStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["連線字串"].ConnectionString;
    string strSQL = @"產生資料集的SQL語法";
    DataTable dt = new DataTable();
    using (SqlConnection conn = new SqlConnection(connStr))
    {
        //傳參數避免Injection和型態轉換錯誤
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand(strSQL, conn);
        da.SelectCommand = cmd;
        cmd.Parameters.Add("@變數", SqlDbType.VarChar, 12).Value =Request.QueryString["參數名"];//用「@變數」要篩選的條件
        da.Fill(dt);//將DataAdapter篩選後的資料放入DataTable
    }

//依照欄位內容, 去回傳false, true
    if (dt.Rows[0]["欄位名"].ToString() == "S")
        return false;
    else
        return true;
}


利用boolean去執行(階段4)
if (isProjectStop()) //判斷案號是否停用, 是則導到指定頁
               Response.Redirect("Default.aspx", true);


2015年8月19日 星期三

[EEP]Server端資料模組 XXX 未啟動!

【錯誤訊息】

在VS中〈用瀏覽器檢視〉來查看單頁時出現:
「GetSqlCommand return error: Server端資料模組 XXX未啟動!」

※「XXX」為server dll的檔名

【解決方法】

(※發生可能性的頻率, 僅為個人常遇到的機率, 不見得適用各位看倌)
可能性高:
開啟登入畫面「InfoLogin.aspx」後,將「方案/Solution」選擇到該DLL所在的Solution並「登入」後,再回到VS中開啟單頁

可能性中:
檢查「EEPNetServer」→「File」→「Package Manager」中相應的Solution是否要正確掛載DLL。必要時將「EEPNetServer」關閉重啟

可能性極低:
檢查「EEPManager」→「System」→「Solution Define」中的「Solution」是否
有打錯字


2015年8月16日 星期日

取得不同GridView正在選取的那列的某欄數值

最初的需求,是點下Button後,會將A正在選取的某欄數值丟給B使用
(此為C#語法, 重點在於橘字)

無「樣板化」 - 單一控制項
lbTest.Text = gView.Rows[gView.SelectedIndex].Cells[1].Text;
橘字→GridView的ID.Rows[第n列].Cells[第n欄]
最後的「.Text」則要視取出的內容而定,單純文字就用「.Text」

有「樣板化」- 可能為單一或複數個控制向
lbTest.Text = (gView.Rows[gView.SelectedIndex].Cells[1].FindControl("Label1") as Label).Text;
橘字與上面相同
綠字由於該Cells不再只有單一控制項,就必須要用「FindControl("元件ID")」去找到目標
再用「as 類別(諸如:Label, Textbox...)」去將型別定下來以利後續操作


2015年8月5日 星期三

[VS]關閉Visual Studio的自動編號ID

在VS中複製貼上某段程式碼時, 發現ID跑掉了!!

<asp:Label ID="name"></asp:Label>
        ↓↓
<asp:Label ID="Label1"></asp:Label>

特別是處理FormView時, 很方腦筋阿!
只好硬著頭皮, 用記事本在千行中的程式碼中進行複製貼上
從這點來看, 很明顯是VS的某個貼心功能(使用版本為VS2012)

其實只要簡單幾個動作就可以關閉/開啟這功能
  1. 在工具列的「工具」→「選項」
  2. 在Dialog中的「文字編輯器」→「HTML」(視情況調整)→「其他」
  3. 取消「在原始碼檢視中貼上時自動編號ID項目」的勾選


這樣ID就不會再亂跑了!



友人對此功能的補充:

  • 因為要避免某些情況下遇到同ID物件的情形,所以自動幫你把重複的ID更改