2017-07-05
2017-07-13 更新
傳參數
從網址接收參數,剖析所要數值 (避免被 Injection )
要先判斷是否 null 才能取值
protected void Page_Load(object sender, EventArgs e) { if (!(String.IsNullOrEmpty(Request.QueryString["id"]))) { Int32.TryParse(Request.QueryString["id"].ToString(), out id); } } |
傳參數檢視
<asp:TemplateField HeaderText="檢視" InsertVisible="False"> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" Text="" NavigateUrl='<%# String.Format("~/Manage/View2.aspx?id={0}", Eval("Id")) %>' CssClass="btn btn-primary btn-xs" Target="_blank">檢視</asp:HyperLink> </ItemTemplate> <ItemStyle Wrap="False" /> </asp:TemplateField> |
同時隱藏第8欄位的標題和欄位內容
DetailsView2.Fields[8].Visible = true; |
處理換行
處理換行 (在 SqlDataSource 就處理,只適用 GridView 等支援 HtmlEncode="false" 的,而 DetailsView 等不支援 HtmlEncode 屬性的不適用 )
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT REPLACE([ItemDetail],char(10),'<br />') AS [ItemDetail] FROM [MyTable] WHERE [Id]=@Id"> <SelectParameters> <asp:QueryStringParameter Name="Id" QueryStringField="id" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> |
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Caption="標題"> <Columns> <asp:BoundField DataField="Id" HeaderText="編號" SortExpression="Id" HtmlEncode="false" /> </Columns> </asp:GridView> |
處理換行 ( 在 DataBound 處理)
protected void DetailsView2_DataBound(object sender, EventArgs e) { var dv = sender as DetailsView; Label Label_ItemDetail = (Label)(DetailsView2.FindControl("Label_ItemDetail")); if (Label_ItemDetail != null) Label_ItemDetail.Text = Label_ItemDetail.Text.Replace("\n", "<br/>"); } |
不換行
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" ItemStyle-Wrap="False" ></asp:BoundField> |
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundField> |
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" ItemStyle-Wrap="True" > <ItemStyle Wrap="False"></ItemStyle> </asp:BoundField> |
唯讀
唯讀
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" ReadOnly="True"></asp:BoundField> |
流水編號
流水編號
<asp:TemplateField HeaderText="#" ItemStyle-Wrap="false" ItemStyle-Width="1px"> <ItemTemplate> <%#Container.DataItemIndex+1 %> </ItemTemplate> </asp:TemplateField> |
日期處理
只抓取日期欄位的 年、月、日
SELECT id, CONVERT(char(10),[MyDate],111) AS [MyDate] FROM MyTable |
只抓取日期欄位的 年
SELECT id, Year(MyDate) FROM MyTable |
只抓取日期欄位的 年月日
<asp:Label ID="StartAuditTimeLabel" runat="server" Text='<%# Eval("StartAuditTime","{0:d}") %>' /> |
只抓取日期欄位的 年月日
<asp:TextBox ID="TextBox_MyDate" runat="server" Width="200px"></asp:TextBox> 改為 <asp:TextBox ID="TextBox_MyDate" runat="server" Width="200px" CssClass="date"></asp:TextBox> |
如果 SQL Server 2016 中欄位設定為 NOT NULL,且預設值 0-0-0 (也就是 1900年1月1日0時0分0秒),如果想顯示,可如下處理 (不然就欄位設定為 允許 NULL )
protected void DetailsView1_DataBound(object sender, EventArgs e) { DetailsView dv = sender as DetailsView; //或 DetailsView dv = (DetailsView)sender; Label Label_SubmitDate = (Label)dv.FindControl("Label_SubmitDate"); string submitDate = Label_SubmitDate.Text.Substring(0, 4); if (submitDate == "1900") { Label_SubmitDate.Visible = false; } } |
protected void GridView_TechnicalDetectionFound_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow) { if ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState == DataControlRowState.Alternate)) { string submitDate = ""; Label Label_SubmitDate = (Label)(e.Row.FindControl("Label_SubmitDate")); if (Label_SubmitDate != null) { submitDate = Label_SubmitDate.Text.Substring(0, 4); if (submitDate == "1900") { Label_SubmitDate.Visible = false; } } } if (e.Row.RowState != DataControlRowState.Edit) { // 編輯模式 } } } |
SQL Server 資料庫欄位為 NOT NULL 處理
NOT NULL 時候,SqlDataSource 處理 ( String 格式的欄位要增加 DefaultValue="" ConvertEmptyStringToNull="false")
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:pmsConnectionString %>" InsertCommand="INSERT INTO [MyTable] ([MyID], [MyName], [MyAddr]) VALUES (@MyID, @MyName, @MyAddr )" SelectCommand="SELECT * FROM MyTable WHERE [MyID] = @id" > <SelectParameters> <asp:QueryStringParameter DefaultValue="0" Name="id" QueryStringField="id" Type="Int32" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="MyID" Type="Int32" /> <asp:Parameter Name="MyName" Type="String" DefaultValue="" ConvertEmptyStringToNull="false" /> <asp:Parameter Name="MyAddr" Type="String" DefaultValue="" ConvertEmptyStringToNull="false" /> <asp:Parameter Name="AdminGroup" Type="Boolean" /> <asp:Parameter Name="BirthDayDate" Type="DateTime" /> </InsertParameters> </asp:SqlDataSource> |
(待續)
相關
[研究] [C#][ASP.NET][WebForm] 雜記
http://shaurong.blogspot.com/2017/07/caspnetwebform.html
[研究][C#][ASP.NET] 隱藏 GridView 欄位
http://shaurong.blogspot.com/2017/07/caspnet-gridview.html
[研究][ASP.NET] GridView 的 BoundField 的 ItemStyle-Wrap 輸出到瀏覽器時被轉譯成甚麼?
http://shaurong.blogspot.com/2017/07/aspnet-gridview-boundfield-itemstyle.html
[研究] 用 CSS 的 word-break: break-word; 可強迫 中的文字在字中換行
http://shaurong.blogspot.com/2017/07/css-word-break-break-word-td.html
[研究][C#][ASP.NET][WebForm] GridView 事件執行順序
http://shaurong.blogspot.com/2017/07/caspnetwebform-gridview.html
[研究] jQuery UI Datepicker 月曆 安裝與試用
http://shaurong.blogspot.com/2017/07/jquery-ui-datepicker.html
[研究] [C#] [ASP.NET] [JavaScript] 彈出視窗
http://shaurong.blogspot.com/2017/06/c-aspnet.html
沒有留言:
張貼留言