Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim chkCate As CheckBox
Dim lblID As Label
Dim i As Integer
For i = 0 To DataList2.Items.Count - 1
chkCate = DataList2.Items(i).FindControl("CheckBox1")
lblID = DataList2.Items(i).FindControl("ID_Cars")
If chkCate.Checked = True Then
'ส่งข้อมูลไปกับลิงค์-------------------------------------
Dim url As String = " "
url = "Compare.aspx?ID_Cars=" & lblID.Text & ""
Response.Redirect(url, True)
End If
Next
End Sub
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Public Partial Class MyDataList
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
DataList1.DataSource = GetDataForDataList()
DataList1.DataKeyField = "ID"
DataList1.ItemDataBound += New DataListItemEventHandler(AddressOf DataList1_ItemDataBound)
DataList1.DataBind()
End If
Button1.Click += New EventHandler(AddressOf Button1_Click)
End Sub
Protected Sub DataList1_ItemDataBound(sender As Object, e As DataListItemEventArgs)
Dim Dr As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
DirectCast(e.Item.FindControl("Label1"), Label).Text = (CInt(Dr("ID")) + 1).ToString() & ". tungman"
DirectCast(e.Item.FindControl("RadioButtonList1"), RadioButtonList).DataSource = GetDataForRadioButtonList()
DirectCast(e.Item.FindControl("RadioButtonList1"), RadioButtonList).DataTextField = "Name"
DirectCast(e.Item.FindControl("RadioButtonList1"), RadioButtonList).DataValueField = "ID"
DirectCast(e.Item.FindControl("RadioButtonList1"), RadioButtonList).DataBind()
DirectCast(e.Item.FindControl("CheckBox1"), CheckBox).Text = String.Format("tungman{0}", (CInt(Dr("ID")) + 1).ToString())
End If
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim chk As String = String.Empty
Dim count As Integer = 0
For Each item As DataListItem In DataList1.Items
Dim aCheckBox As CheckBox = DirectCast(item.FindControl("CheckBox1"), CheckBox)
If aCheckBox.Checked = True Then
If count <> 0 Then
chk += ","
End If
chk += DataList1.DataKeys(item.ItemIndex).ToString()
count = count + 1
End If
Next
Response.Redirect(String.Format("~/Compare.aspx?ID_Cars={0}", chk))
End Sub
' สร้าง DataTable เพื่อเป็นตัวอย่างไม่ต้องสนใจ
Private Function GetDataForDataList() As DataTable
Dim Dt As New DataTable()
Dt.Columns.Add(New DataColumn("ID", System.Type.[GetType]("System.Int32")))
For i As Integer = 0 To 2
Dim Dr As DataRow = Dt.NewRow()
Dr("ID") = i
Dt.Rows.Add(Dr)
Next
Return Dt
End Function
' สร้าง DataTable เพื่อเป็นตัวอย่างไม่ต้องสนใจ
Private Function GetDataForRadioButtonList() As DataTable
Dim Dt As New DataTable()
Dt.Columns.Add(New DataColumn("ID", System.Type.[GetType]("System.Int32")))
Dt.Columns.Add(New DataColumn("Name", System.Type.[GetType]("System.String")))
For i As Integer = 0 To 4
Dim Dr As DataRow = Dt.NewRow()
Dr("ID") = i
Dr("Name") = "Item" & i.ToString()
Dt.Rows.Add(Dr)
Next
Return Dt
End Function
End Class