摘要:上集,Ch.10-3 .FindControl()方法的補充範例 -- ASP.NET 專題實務(上集 )
ASP.NET 專題實務(上集 )
Ch.10-3-2 .FindControl()方法的補充範例
HTML畫面(C#)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">
.style1 {
color: #FF0000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
當您按下<strong>[選取]按鈕,</strong>就會抓到那一列的 <strong>title欄位的 [值]</strong><br />
<br />
請先將 GridView的 <strong>title欄位 </strong>設定為<span class="style1"><strong>樣板</strong></span><br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataKeyNames="id" DataSourceID="SqlDataSource1" GridLines="Vertical"
onselectedindexchanging="GridView1_SelectedIndexChanging" >
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="test_time" HeaderText="test_time"
SortExpression="test_time" />
<asp:TemplateField HeaderText="title" SortExpression="title">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString(請自己修改) %>"
SelectCommand="SELECT [id], [test_time], [title] FROM [test]" >
</asp:SqlDataSource>
<br />
<br />
</div>
</form>
</body>
</html>
=======================================================================================
Code Behind (C#)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label lb = (Label)GridView1.Rows[0].FindControl("Label1");
Response.Write(lb.Text);
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Label lb = (Label)GridView1.Rows[e.NewSelectedIndex].FindControl("Label1");
Response.Write(lb.Text);
}
Code Behind (VB)
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim lb As Label = CType(GridView1.Rows(0).FindControl("Label1"), Label)
Response.Write(lb.Text)
End If
End Sub
Protected Sub GridView1_SelectedIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging
Dim lb As Label = CType(GridView1.Rows(e.NewSelectedIndex).FindControl("Label1"), Label)
Response.Write(lb.Text)
End Sub
範例下載 -- Ch.10-3-2_CS.rar
我將思想傳授他人, 他人之所得,亦無損於我之所有;
猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson
線上課程教學,遠距教學 (Web Form 約51hr) https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015
線上課程教學,遠距教學 (ASP.NET MVC 約75~100hr) https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab
ASP.NET MVC線上課程 第一天 免費看 (5.5小時) 寫信給我,不要私訊 -- mis2000lab (at) yahoo.com.台灣 或 school (at) mis2000lab.net
ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, "完整" 試聽。
......... facebook社團 https://www.facebook.com/mis2000lab ......................
......... YouTube (ASP.NET) 線上教學影片 https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/
Blog文章 "附的範例" 無法下載,請看 https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download
請看我們的「售後服務」範圍(嚴格認定)。
......................................................................................................................................................
[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講 事先錄製好的影片,並非上課側錄! 觀看時,有如「一對一」跟您面對面講課。

ASP.NET MVC 5 => .NET Core MVC 線上教學 累積時數約 100小時...... 第一天(5.5小時)完整內容,"免費"讓您評估
