分類
ASP.NET Web 前端

[ASP.NET] 在TextBox輸入內容後自動跳轉到指定UI元件

需求:

有些資料欄位在UI上設計為分段或分區輸入,如下方的手機欄位分為三個區塊。

使用者希望能夠在輸入完前一個區塊後自動跳轉到後一個區塊(1跳2,2跳3)
以下使用JavaScript來實作需求,首先實作跳轉方法。

            //輸入內容到限制長度後跳轉至指定元件
            function JumpToSpecifiedItem(currentItem, specificedItem) {
                if (currentItem.value.length == currentItem.maxLength) {
                    currentItem.form.elements[specificedItem].focus();
                }
            }

方法的第一個參數為目前使用者正在輸入的UI元件,這邊是透過maxLength來判斷是否要跳轉。
第二個參數為要跳轉的目標UI元件,該元件透過呼叫focus來達到跳轉。
接著就是UI元件部分。

<label id ="userPhoneNumberLabel">手機</label>
<div class="form-inline">
<asp:TextBox runat="server" ID="userPhoneNumberFirstSection" MaxLength="4" onKeyUp="JumpToSpecifiedItem(this,'ctl00$MainContent$userPhoneNumberSecondSection')"/>
<asp:TextBox runat="server" ID="userPhoneNumberSecondSection" MaxLength="3" onKeyUp="JumpToSpecifiedItem(this,'ctl00$MainContent$userPhoneNumberThirdSection')"/>
<asp:TextBox runat="server" ID="userPhoneNumberThirdSection" MaxLength="3" placeholder="000"/>
</div>

重點為

1.要設定ID
2.要設定MaxLength(因為是透過該值來判斷是否跳轉,當然也可以使用別的值)
3.呼叫JumpToSpecifiedItem方法,第2個參數要確認實際的ID值。(透過實際網頁上點擊F12來確認)
 
 

分類
ASP.NET git 使用紀錄 Web 前端

在ASP.NET Webform專案中加入專屬的 gitignore file

不用到處搜尋 .gitignore 檔案啦,先移動到專案的根目錄再輸入以下指令


dotnet new gitignore

就會在根目錄產生 .gitignore file,內容如下


## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# Tye
.tye/
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*[.json, .xml, .info]
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# Ionide - VsCode extension for F# Support
.ionide/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
##
## Visual studio for Mac
##
# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/
# Mac bundle stuff
*.dmg
*.app
# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# JetBrains Rider
.idea/
*.sln.iml
##
## Visual Studio Code
##
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

至於 dotnet 只是啥
請參考 https://docs.microsoft.com/zh-tw/dotnet/core/tools/dotnet

分類
Web 前端

ASP.NET 如何套用 CSS檔

在想引入的.cs 檔的 <asp:Content ID=”Content1″ ContentPlaceHolderID=”MainContent” runat=”server”>下方加入

<link rel=”stylesheet” href=”Content/CommonStyle.css”>

如下:


<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
      <link rel="stylesheet" href="Content/CommonStyle.css">

分類
Web 前端

BootStrap form-inline 水平排列 範例

以下為使用 form-inline 來達到水平排列內容的範例。


<div class="container">
  <div class="row">
    <div class='col-sm-6'>
      <div class="form-group">
        <label>手機</label>
	  <div class="form-inline">
	     <asp:TextBox runat="server" class="form-control input-sm required" ID="userPhoneNumberFirstSection" Width="55px" MaxLength="4" placeholder="0000" en />
	     <asp:Label runat="server" Style="font-size: larger" class="text">-</asp:Label>
	     <asp:TextBox runat="server" class="form-control input-sm required" ID="userPhoneNumberSecondSection" Width="45px" MaxLength="3" placeholder="000" />
	     <asp:Label runat="server" Style="font-size: larger" class="text">-</asp:Label>
	     <asp:TextBox runat="server" class="form-control input-sm required" ID="userPhoneNumberThirdSection" Width="45px" MaxLength="3" placeholder="000" />
	   </div>
	 </div>
       </div>
     </div>
  <div class="row">
    <div class='col-sm-6'>
      <div class="form-group">
        <label>生日</label>
	  <div class="form-inline">
	    <asp:TextBox runat="server" class="form-control input-sm required" ID="userBirthdayYear" Width="55px" MaxLength="4" placeholder="YYYY" />
	    <asp:Label runat="server" Style="font-size: larger" class="text">-</asp:Label>
	    <asp:TextBox runat="server" class="form-control input-sm required" ID="userBirthdayMonth" Width="45px" MaxLength="2" placeholder="MM" />
            <asp:Label runat="server" Style="font-size: larger" class="text">-</asp:Label>
	    <asp:TextBox runat="server" class="form-control input-sm required" ID="userBirthdayDay" Width="45px" MaxLength="2" placeholder="DD" />
	   </div>
	 </div>
       </div>
     </div>
   </div>

效果如下

使用form-inline達到水平排列
分類
Web 前端

form role= "form" 導致 TextBox 內容被清空

當網頁上的某個伺服器控制項被設定為 AutoPostBack = true 時,代表該控制項的資料或內容改變時就會自動觸發 PostBack

預設的情況下其他控制項並不會受到影響,也就是說其內容值不會改變

若其他控制項是被<form role = “form”>包圍住,只要觸發 PostBack 就會導致其值被清空。如下

<form role="form">
  <div class="form-group">
    <label>ID</label>
      <div class="form-inline">
	<asp:TextBox runat="server" class="required" ID="userPhoneNumber"/>
      </div>
  </div>
</form>

最外層是 <form role = “form”> 導致只要觸發 PostBack 其中的 userPhoneNumber 的值就會清空。

如果在PostBack時發現某些控制項會自動清空,但又找不到其他邏輯。
不妨找找是不是外層被<form role = “form”> 包住了。

分類
Web 前端

Bootstrap.v3.Datetimepicker 簡單紀錄

因為BootStrap預設沒有日期或時間的UI元件,搜尋一下發現其他開發者提供的好用外掛

Bootstrap.v3.Datetimepicker!!


首先我的環境是ASP.NET和最原始的WebForm專案

1.安裝Bootstrap.v3.Datetimepicker

直接使用NuGet套件管理員安裝,如下

2.使用 Bootstrap.v3.Datetimepicker

2.1在Site.Master的 <head> 區塊輸入


<link rel="stylesheet" href="Content/bootstrap-datetimepicker.css" />

2.2在Site.Master 的<body> 區塊輸入


<script src="Scripts/jquery-3.4.1.min.js"></script>
<script src="Scripts/moment.min.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
<script src="Scripts/bootstrap-datetimepicker.js"></script>

3. Bootstrap.v3.Datetimepicker 的宣告和使用

Datetimepicker可同時使用日期和時間,也可以單獨使用日期或時間。

3.1 同時使用日期和時間


<div class="container">
   <div class="row">
      <div class='col-sm-6'>
         <div class="form-group">
            <div class='input-group date' id='datetimepicker1'>
               <input type='text' class="form-control" />
               <span class="input-group-addon">
               <span class="glyphicon glyphicon-calendar"></span>
               </span>
            </div>
         </div>
      </div>
      <script type="text/javascript">
         $(function () {
             $('#datetimepicker1').datetimepicker();
         });
      </script>
   </div>
</div>

畫面如下

日期和時間

3.2 只有時間


    <div class="container">
        <div class="row">
            <div class='col-sm-3'>
                <div class="form-group">
                    <div class='input-group date' id='dateAndtime'>
                        <input type='text' class="form-control" />
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-time"></span>
                        </span>
                    </div>
                </div>
            </div>
            <script type="text/javascript">
                $(function () {
                    $('#dateAndtime').datetimepicker({
                        format: 'LT'
                    });
                });
            </script>
        </div>
    </div>

畫面如下

只有時間

3.3 只有日期


    <div class="container">
        <div class="row">
            <div class='col-sm-3'>
                <div class="form-group">
                    <div class='input-group date' id='dateAndtime'>
                        <input type='text' class="form-control" />
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>
            </div>
            <script type="text/javascript">
                $(function () {
                    $('#dateAndtime').datetimepicker({
                    format: 'DD/MM/YYYY'
                    });
                });
            </script>
        </div>
    </div>

只有日期

可以看到差異只在選擇哪個icon和在js中呼叫datetimepicker的格式

參考官網 https://getdatepicker.com/4/