ASP.NET ClientID เป็น Property ไว้สำหรับแสดงค่า ClientID ของ Control แต่ล่ะตัวที่ถูก Generate ไปยังฝั่ง Client ซึ่งจะมีประโยชน์หากต้องการเขียนร่วมกับภาษา Client Script เช่น JavaScript หรือ VBScript
Language Code : VB.NET || C#
Framework : 1,2,3,4
ClientID.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ClientID.aspx.vb" Inherits="Client" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ThaiCreate.Com ASP.NET - ClientID</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid id="myDataGrid" runat="server" AutoGenerateColumns="False"
DataKeyField="CustomerID">
<Columns>
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate> <asp:CheckBox ID="chkDel" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:Label ID="Delete" runat="server" Text="Delete"></asp:Label>
</HeaderTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="CustomerID">
<ItemTemplate>
<asp:Label id="lblCustomerID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Email">
<ItemTemplate>
<asp:Label id="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="CountryCode">
<ItemTemplate>
<asp:Label id="lblCountryCode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Budget">
<ItemTemplate>
<asp:Label id="lblBudget" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Used">
<ItemTemplate>
<asp:Label id="lblUsed" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid><br />
<asp:Label ID="lblText" runat="server" Text="Client ID"></asp:Label>
<asp:TextBox ID="txtClientID" runat="server" Width="187px"></asp:TextBox>
</form>
</body>
</html>
ClientID.aspx.vb
Imports System.Data
Imports System.Data.OleDb
Partial Class Client
Inherits System.Web.UI.Page
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Dim strSQL As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/mydatabase.mdb") & ";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
If Not Page.IsPostBack() Then
BindData()
End If
End Sub
Protected Sub BindData()
strSQL = "SELECT * FROM customer"
Dim dtReader As OleDbDataReader
objCmd = New OleDbCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
'*** BindData to DataGrid ***'
myDataGrid.DataSource = dtReader
myDataGrid.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
objConn.Close()
objConn = Nothing
End Sub
Protected Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles myDataGrid.ItemDataBound
'*** Check Delete ***'
Dim chkDel As CheckBox = CType(e.Item.FindControl("chkDel"), CheckBox)
If Not IsNothing(chkDel) Then
With chkDel
chkDel.Attributes.Add("OnClick", "document.form1." & Me.txtClientID.ClientID & ".value = '" & chkDel.ClientID & "'")
End With
End If
End Sub
End Class
Screenshot
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2008-10-31 12:14:55 /
2009-07-26 23:11:16 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|