2017年8月7日 星期一

[ASP.NET] 資料元件按鈕 取得編號

Repeater的按鈕取得元件項目/編號
Protected Sub LinkBut_RoleEdit_Click(sender As Object, e As System.EventArgs)
        Dim but As LinkButton = CType(sender, LinkButton)
        'Dim commandArgument As String = but.CommandArgument
        Dim item As RepeaterItem = CType(but.NamingContainer, RepeaterItem)
        Dim index As Integer = item.ItemIndex

        Dim lb_role_id As Label = Repeater1.Items(index).FindControl("lb_role_id")

    End Sub

或

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs)
    Dim index As String = CType(CType(sender, System.Web.UI.WebControls.DropDownList).Parent, RepeaterItem).ItemIndex
    Dim v_code As String = CType(Repeater1.Items(index).FindControl("DropDownList1"), DropDownList).SelectedValue
    Dim v_remark As TextBox = CType(Repeater1.Items(index).FindControl("tb_remark"), TextBox)

End Sub

DataList按鈕取得元件項目/編號
Dim index As Integer = CType(CType(sender, LinkButton).BindingContainer, DataListItem).ItemIndex
Dim fileType As Label = DataList1.Items(index).FindControl("lb_fileType")


GridView按鈕取得元件項目/編號
 Protected Sub but_update_Click(sender As Object, e As System.EventArgs)
        Dim index As Integer = CType(CType(sender, System.Web.UI.WebControls.Button).Parent.Parent, GridViewRow).RowIndex
        Dim id As Integer = GridView1.DataKeys(index)("menu_id").ToString()
        Dim menu_value As String = CType(GridView1.Rows(index).FindControl("tb_menu_value"), TextBox).Text
        Dim disabled As String = CType(GridView1.Rows(index).FindControl("cb_EditDisabled_yn"), CheckBox).Checked.ToString

2017年7月24日 星期一

Session TimeOut設定

Asp.Net的相關TimeOut設定
https://dotblogs.com.tw/rainmaker/archive/2010/01/11/12955.aspx

如何讓 ASP.NET 使用 Session 資料時不要再自動消失
https://blog.miniasp.com/post/2008/01/12/How-to-avoid-Session-data-loss-in-ASPNET.aspx

2017年7月14日 星期五

將使用者重新導向至其他網頁

使用瀏覽器將使用者重新導向至其他網頁

  1. 將 Response 物件的 BufferOutput 屬性設定為 true。
  2. 呼叫 Response 物件的 Redirect 方法,然後將想要重新導向使用者的網頁 URL 傳遞給它。
    下列程式碼範例,示範了如何根據區域變數 UserLanguage (在別處設定) 的內容來重新導向網頁。
    Response.BufferOutput = True
    If UserLanguage = "English" Then
        Response.Redirect("http://www.microsoft.com/gohere/look.htm")
    ElseIf UserLanguage = "Deutsch" Then
        Response.Redirect("http://www.microsoft.com/gohere/look_deu.htm")
    ElseIf UserLanguage = "Español" Then
        Response.Redirect("http://www.microsoft.com/gohere/look_esp.htm")
    End If
    
    Response.BufferOutput = true;
    if (UserLanguage == "English")
    {
        Response.Redirect("http://www.microsoft.com/gohere/look.htm");
    }
    else if (UserLanguage == "Deutsch")
    {
        Response.Redirect("http://www.microsoft.com/gohere/look_deu.htm");
    }
    else if (UserLanguage == "Español")
    {
        Response.Redirect("http://www.microsoft.com/gohere/look_esp.htm");
    }
    

若要使用伺服器端方法將使用者重新導向至其他網頁

  • 呼叫 Transfer 方法,然後將想要重新導向使用者的網頁名稱傳遞給它。
    下列程式碼範例,示範了如何重新導向至其他網頁。
    Protected Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Server.Transfer("Page2.aspx", True)
    End Sub
    
    protected void Button1_Click(object sender, System.EventArgs e)
    {
       Server.Transfer("Page2.aspx", true);
    }

Response.Redirect與Server.Transfer差異

簡單來說,Response.Redirect會由Client送出兩次的Request,Server.Transfer則是一次。

詳細說明可參考這篇文章:
https://dotblogs.com.tw/jimmyyu/2009/11/10/11503

2017年6月22日 星期四

note (片段)

----------------------------------------------------------------------------------------------------------------------------------------
GridView 數字欄位加千分號

<asp:Label ID="lb_text" runat="server" Text='<%# Eval("CONSOLIDATED_REVENUE","{0:N0}") %>'></asp:Label>
or
<asp:BoundField DataField="CONSOLIDATED_REVENUE" HeaderText="合併營收" DataFormatString="{0:N0}"
    SortExpression="CONSOLIDATED_REVENUE" ItemStyle-HorizontalAlign="Center"  >
</asp:BoundField>

----------------------------------------------------------------------------------------------------------------------------------------

GridView & DataList --DataBind取值方法

##############
http://scenic0327-tek.blogspot.tw/2012/03/blog-post_29.html
##############
http://www.blueshop.com.tw/board/FUM20041006161839LRJ/BRD20110730191358HNV.html

If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lnkView As New LinkButton()
            lnkView.ID = "patent_id"
            lnkView.Text = TryCast(e.Row.DataItem, DataRowView).Row("patent_id").ToString()
            AddHandler lnkView.Click, AddressOf ViewDetails
            lnkView.CommandArgument = TryCast(e.Row.DataItem, DataRowView).Row("patent_id").ToString()
            e.Row.Cells(1).Controls.Add(lnkView)

            'For i As Integer = 0 To e.Row.Cells.Count - 1 'row的值

            '    xxx.Text += e.Row.Cells(i).Text & "<br/>"
            '    'If e.Row.Cells(i).ClientID = "img" Then
            '    '    xxx.Text = "Y"
            '    'End If
            'Next

            For i As Integer = 0 To GridView1.HeaderRow.Cells.Count - 1 '標題的值
                If GridView1.HeaderRow.Cells(i).Text = "簡圖" Then
                    If TryCast(e.Row.DataItem, DataRowView).Row("img").ToString() <> "" Then
                        Dim img As New Image()
                        img.ID = "img"
                        img.Width = 100

                        img.ImageUrl = "FileStagingPath/" & TryCast(e.Row.DataItem, DataRowView).Row("img").ToString()
                        e.Row.Cells(i).Controls.Add(img)
                    End If
                End If
            Next

            'Dim txtCountry As New TextBox()
            'txtCountry.ID = "name"
            'txtCountry.Text = TryCast(e.Row.DataItem, DataRowView).Row("name").ToString()
            'e.Row.Cells(2).Controls.Add(txtCountry)
        End If
##############


Imports System.IO

'判斷FileUpload檔案是否存在
If (FileUpload_Img.HasFile) Then
End If

'取得上傳檔案的集合
Dim hfc As HttpFileCollection = Request.Files

For j As Integer = 0 To hfc.Count - 1
   Dim fileName As String = Mid(hfc(j).FileName, InStrRev(hfc(j).FileName, "\") + 1, Len(hfc(j).FileName)) '檔案名稱(含副檔名)
   Dim fileNameEx As String = Mid(hfc(j).FileName, InStrRev(hfc(j).FileName, "."), Len(hfc(j).FileName)) '副檔名
   Dim fileSize As String = hfc(j).ContentLength '檔案大小
Next


'判斷上船的目錄路徑是否存在
If My.Computer.FileSystem.DirectoryExists(saveDir1) Then
End If

'建立目錄路徑
My.Computer.FileSystem.CreateDirectory(saveDir1)

'判斷目錄是否有相同檔案名稱
If File.Exists(filePath) Then
End if

'變更檔案名稱
File.Move("ttt.txt", "ttt-1.txt")


https://msdn.microsoft.com/zh-tw/library/cc148994.aspx

#####################
資料繫結

<%# DataBinder.Eval(Container, "DataItem.description")%>

######################
DataList

DataList刪除
<asp:ImageButton ID="imgBut_delete" runat="server" ImageUrl="images/CNSREJ16.GIF" CommandName="delete" />

要加入DataKeyField
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" DataKeyField="detailed_id" >

Protected Sub DataList1_DeleteCommand(source As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.DeleteCommand
    Dim keyID As Integer = Convert.ToInt32(DataList1.DataKeys(e.Item.ItemIndex))
End Sub


DataList 資料內容處理
 Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            '處理子層DataList2的資料
            Dim dl2 As DataList = CType(e.Item.FindControl("DataList2"), DataList)
            'Dim hidden2 As HiddenField = DirectCast(e.Item.FindControl("lb_needDate"), HiddenField)
            'Dim QID As String = hidden2.Value

            'If QID = 33 Then
            For Each li As DataListItem In dl2.Items
                Dim lbl As Label = DirectCast(e.Item.FindControl("lb_needDate"), Label)
                If lbl IsNot Nothing Then
                    If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then
                        lbl.Visible = False
                    End If
                End If
            Next
            'End If
        End If
    End Sub


#####
檔案上傳
多個檔案上傳




2017年6月14日 星期三

CSS 語法,文字換行,強迫不換行。

CSS 文字換行的 style 為 word-wrap ,透過這個屬性可以指定換行的方式,並可以強制文字不換行。
而 CSS 還有個空白長度的設定,透過 white-space 可以移除連續空白。
  • 文字強迫換行 word-break: break-all;
  • 依單字換行 word-wrap:break-word;
  • 死都不換行 white-space:nowrap;

換行設定, word-wrap & word-break

文字強迫換行,太長的英文字,會自動切成兩半。

  • word-break: break-all; 若一個單字過長,超過畫面的寬度,則將單字切斷,並換行。

word-break: break-all;
Before Moammar Long language string Long language string Long language string Long language string, there were the Phoenicians.
從上面的 DEMO 中可以看到 language 這個單字被切成兩斷。

文字自動換行,長英文字不切斷

  • word-wrap:break-word; 若一個單字過長,超過畫面的寬度,則將單字換行(不切斷單字)。

word-wrap:break-word;
Before Moammar Long language string Long language string Long language string Long language string, there were the Phoenicians.
不切斷英字單字對於一般的讀者是比較好懂的,唯一的缺點是右邊會出現很大的一塊空白。

空白設定 : white-space

保留原始空白與換行

  • white-space:pre; 保留原始資料的空白與換行,功能與 HTML tag 中的 <pre> 相同。
white-space:pre;
Before Moammar Gadhafi, there were the Phoenicians. And the Greeks.

強迫文字不換行

  • white-space:nowrap; 移除連續空白,移除換行符號(n r), 強迫文字不換行。
white-space:nowrap;
Before Moammar Gadhafi, there were the Phoenicians. And the Greeks. 

保留原始空白,但文字自動換行

  • white-space:pre-wrap; 保留所有空白,自動換行。
white-space:pre-wrap;
Before Moammar Gadhafi, there were the Phoenicians. And the Greeks. The Romans.

連續空白,取代成一個空白。

  • white-space: pre-line ,這個語法可以會自動將多個空白轉換成一個空白
white-space: pre-line;
Before Moammar Gadhafi, there were the Phoenicians. And the Greeks. The Romans. 

2017年6月3日 星期六

OracleConnection.BeginTransaction 方法 ()

Public Sub RunOracleTransaction(ByVal connectionString As String)
    Using connection As New OracleConnection(connectionString)
        connection.Open()

        Dim command As OracleCommand = connection.CreateCommand()
        Dim transaction As OracleTransaction

        ' Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
        ' Assign transaction object for a pending local transaction
        command.Transaction = transaction

        Try
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
            command.ExecuteNonQuery()
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")
        Catch e As Exception
            transaction.Rollback()
            Console.WriteLine(e.ToString())
            Console.WriteLine("Neither record was written to database.")
        End Try
    End Using
End Sub

2017年5月7日 星期日

[ASP.NET] Page事件發生先後順序

PreInit 事件:執行預先初始化的工作,在ASP.NET 2.0中,若要動態調整主版頁面 (Master Page)、佈景主題 (Theme) 時,要在這個事件中調整。
Init 事件:執行初始化工作。
InitCompleted 事件:在完成初始化工作後引發。
Preload 事件:執行預先載入的工作。
Load 事件:執行載入的工作,大多數的網頁都擁有 Page_Load 事件處理常式,使用者控制項 (user control) 中也有 Page_Load 事件常式,都會在此時呼叫。
控制項的 PostBack 變更通知:當網頁偵測到是 PostBack 要求時,會引發 PostBack 訊息通知的事件。
控制項的 PostBack 相關事件:當網頁偵測到是 PostBack 要求時,會引發 PostBack 訊息指定的控制項的事件。
LoadCompleted 事件:執行載入完成後的工作。
PreRender 事件:處理在產生 HTML 結果前的工作。
SaveStateCompleted 事件:處理頁面狀態(ViewState 與 ControlState)儲存完成後的事件。
Render 事件:處理產生 HTML 的工作。
Unload 事件:處理結束網頁處理時的工作。

沒有使用 Master Page 情況下:
Page.PreInit
Page.Init
Page.InitComplete
Page.PreLoad
Page.Load
Page.LoadComplete
Page.PreRender
Page.PreRenderComplete

使用了 MasterPage 情況, MasterPage 與 ContentPage 事件順序:
ContentPage.PreInit
Master.Init
ContentPage.Init
ContentPage.InitComplete
ContentPage.PreLoad
ContentPage.Load
Master.Load
ContentPage.LoadComplete
ContentPage.PreRender
Master.PreRender

ContentPage.PreRenderComplete