2015年12月31日 星期四

如何讓postback後仍維持在同一個位置

如何讓postback後仍維持在同一個位置

1. 在webconfig檔設定
在<system.web></system.web>之間增加下列程式
<pages maintainScrollPositionOnPostBack="true"></pages>

2. 在aspx檔設定
<@page MaintainScrollPositionOnPostback="true" .......>

3. 在.vb檔設定
Page.MaintainScrollPositionOnPostBack = true

2015年12月28日 星期一

asp.net 檔案列表-列出指定路徑下所有文檔、子資料夾

.VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '取得路徑
    Dim dirinfo As DirectoryInfo = New DirectoryInfo(MapPath("~"))
    Dim sortList As FileInfo() = dirinfo.GetFiles()
  
    '取得目錄
    Dim sortFile As DirectoryInfo() = dirinfo.GetDirectories()
    For j As Integer = 0 To sortFile.Length - 1
        Label1.Text += sortFile(j).FullName & "<br/>"
    Next

    For Each item As FileInfo In sortList
        Label1.Text += item.FullName & "<br/>"
    Next
End Sub


2015年12月21日 星期一

ListBox 項目的向上 向下移動

ASP<asp:ListBox ID="ListBox1" runat="server" Height="100px" Width="200px">    <asp:ListItem>台灣</asp:ListItem>    <asp:ListItem>日本</asp:ListItem>    <asp:ListItem>英國</asp:ListItem>    <asp:ListItem>西班牙</asp:ListItem>    <asp:ListItem>義大利</asp:ListItem></asp:ListBox><asp:Button ID="Button1" runat="server" Text="向上" /><asp:Button ID="Button2" runat="server" Text="向下" />


.VB
向下
rotected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim index As Integer = ListBox1.SelectedIndex

   If index > 0 Then
       Dim item As ListItem = ListBox1.Items(index)
       ListBox1.Items.RemoveAt(index)
       ListBox1.Items.Insert(index - 1, item)
   End If
nd Sub

向下
rotected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
   Dim index As Integer = ListBox1.SelectedIndex

   If index < ListBox1.Items.Count - 1 Then
       Dim item As ListItem = ListBox1.Items(index)
       ListBox1.Items.RemoveAt(index)
       ListBox1.Items.Insert(index + 1, item)

   End If
End Sub


DataList包 DataList (DataList 的大腸包小腸)

ASP
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
  <ItemTemplate>
      <a><asp:Label ID="lb_title" runat="server" Text='<%# Eval("title") %>'></asp:Label></a>
      <asp:Label ID="lb_no" runat="server" Text='<%# Eval("no") %>'></asp:Label>
      <div>
          <asp:DataList ID="DataList2" runat="server">
            <ItemTemplate>
               <a><asp:Label ID="lb_title_s" runat="server" Text='<%# Eval("title") %>'></asp:Label></a>
            </ItemTemplate>
          </asp:DataList>
      </div>
  </ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:xxx.web.access %>" 
    ProviderName="<%$ ConnectionStrings:xxx.web.access.ProviderName %>" >
</asp:SqlDataSource>



.VB
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
        Dim dl As DataList = Nothing
        Dim labTypeID As Label = Nothing

        dl = CType(e.Item.FindControl("DataList2"), DataList)
        labTypeID = CType(e.Item.FindControl("lb_no"), Label)

        Dim connString1 As String = WebConfigurationManager.ConnectionStrings("xxx.web.access").ConnectionString
        Dim conn As New OracleConnection(connString1)
        conn.Open()

        Dim Cmd1 As New OracleCommand
        Cmd1.Connection = conn
        Cmd1.CommandText = "select * from eip_web_item_content where id=4 and disable_date is null and content=" & labTypeID.Text & " order by ordernum"
        Dim dr1 As OracleDataReader = Cmd1.ExecuteReader
        Dim dt1 As New DataTable
        dt1.Load(dr1)
        dr1.Close()
        conn.Close()
        '<=DataList2的DataSource來源

        dl.DataSource = dt1
        dl.DataBind()
    End If

End Sub


畫面