---- การผ่านค่าพารามิเตอร์ ไปและกลับ ระหว่าง MasterPage/Page/User Control (Delegate)
- ถ้าใช้ delegate ก็จะไม่พบปัญหาพวกการผ่านค่าครับ
เพราะจะส่งอะไรก็ส่งผ่าน event arg (บางทีอาจจะต้องสร้าง class arg เองก็มี)
Quote:
---- ยืนยันตัวตนของ User Control ให้ได้ (User Control State) ยกตัวอย่างเช่น
Event Page Load
------- Page.PanelContainnerXXX.Controls.Clear()
------- Page.PanelContainnerXXX.Controls.Add("your User Control")
------- ...
End Event Page Load
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim ddlLostState As DropDownList = New DropDownList()
ddlLostState.ID = "dynOHmyGod"
ddlLostState.Items.Add("A")
ddlLostState.Items.Add("B")
ddlLostState.Items.Add("C")
ddlLostState.Items.Add("D")
PlaceHolder1.Controls.Add(ddlLostState)
End If
End Sub
End Class
สร้าง class ชื่อ UserControlClickEventArgs.cs ไว้ที่ folder app_code
UserControlClickEventArgs.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public delegate void UserControlClickEventHandler(object source, UserControlClickEventArgs e);
/// <summary>
/// Summary description for UserControlEventArgs
/// </summary>
public class UserControlClickEventArgs : EventArgs
{
private object _eventArg;
public UserControlClickEventArgs(object EventArg)
{
//
// TODO: Add constructor logic here
//
_eventArg = EventArg;
}
public object EventArg
{
get { return _eventArg; }
}
}
if (!IsPostBack)
{
ASP.webusercontrol_ascx webUserControl = new ASP.webusercontrol_ascx();
webUserControl.Click += new UserControlClickEventHandler(webUserControl_Click);
this.PlaceHolder1.Controls.Add(webUserControl);
}
คนตั้งกระทู้ยังไม่เชื่อมั่นในตัวเองเลยกับ User Controls
แต่เชื่อหรือไม่ว่าผมใช้ SourceCode แค่นี้จริงฯ กับ All With out All & Basic Data
โดยใช้ Dynamic Load User Controls
โดยใช้ Source Code ไม่ถึง 2 ร้อยบรรทัด
Code (VB.NET)
Imports System.Web.UI
Imports System.Reflection
Imports WL_Common.GridManager
Imports WL_Security.Crypter
Imports I_AM_Dinosorus
Public Class usrGoodLuckRights
Inherits System.Web.UI.UserControl
Sub New()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If ViewState("curPagesysMenus") Is Nothing Then
Dim lstmsrRoles = DirectCast(WL_Session.GlobalSession.Current.msrRoles, List(Of WL_Model.msrRoles)).FindAll(Function(x) x.RoleID > 900 And x.RoleID < 1200)
cboRoles.Items.Add(New ListItem() With {.Text = "โปรดเลือก...", .Value = "-1"})
For Each r In lstmsrRoles
cboRoles.Items.Add(New ListItem() With {.Text = r.RoleName, .Value = r.RoleID.ToString()})
Next
cboRoles.SelectedIndex = 0
Dim lstmsrUsers = msrUsers.GetList().ToList()
cboUsers.Items.Add(New ListItem() With {.Text = "โปรดเลือก...", .Value = "-1"})
For Each r In lstmsrUsers
cboUsers.Items.Add(New ListItem() With {.Text = r.UserName, .Value = r.UserID.ToString()})
Next
cboUsers.SelectedIndex = 0
Dim dt = sysMenus.GetListHierarchyDataTable(4, "GoodLuck")
dt.Columns.Add(New DataColumn() With {.ColumnName = "ACL", .DataType = GetType(String), .DefaultValue = "0000000000"})
dt.PrimaryKey = New DataColumn() {dt.Columns("MenuCode")} '{dt.Columns("MenuCode"), dt.Columns("PageID")}
dt.AcceptChanges()
ViewState("curPagesysMenus") = dt
Repeater1.DataSource = dt
Repeater1.DataBind()
Repeater2.DataSource = dt
Repeater2.DataBind()
End If
End Sub
#Region "Repeater Manager"
'Thank : #095316 คุณ กำลังเครียด/คุณ ห้ามตอบเกินวันละ 2 กระทู้ (www.thaicreate.com)
Private Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Call RepeaterMgr(e)
End Sub
Private Sub Repeater2_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater2.ItemDataBound
Call RepeaterMgr(e)
End Sub
Private Sub RepeaterMgr(ByVal e As RepeaterItemEventArgs)
If ((e.Item.ItemType = ListItemType.Item) OrElse (e.Item.ItemType = ListItemType.AlternatingItem)) Then
Dim drv As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
Dim lblMenuName As Label = DirectCast(e.Item.FindControl("lblMenuName"), Label)
lblMenuName.Text = drv.Item("xTitleTH").ToString()
'***** Fixed Bug's Item.DataItem are all null when postback.
lblMenuName.Attributes.Add("MenuID", drv.Item("MenuID"))
lblMenuName.Attributes.Add("ACL", drv.Item("ACL"))
'*****
Enumerable.ToList(Of Control)((From c In Enumerable.Cast(Of Control)(e.Item.Controls)
Where (c.GetType Is GetType(CheckBox))
Select c)).ForEach(Sub(c As Control)
Dim cb As CheckBox = DirectCast(c, CheckBox)
Try
cb.Checked = If(drv.Item("ACL").ToString().Substring(Convert.ToSByte(cb.ID.Substring(1)), 1) = "1", True, False)
Catch ex As Exception
cb.Checked = False
End Try
End Sub)
End If
End Sub
Protected Sub SaveData(sender As Object, e As EventArgs)
'Test only
Dim btnSave = DirectCast(sender, Button)
If btnSave.ID = "btnSaveU" Then
If cboUsers.SelectedValue = "-1" Then
'alert("Please select...");
Return
End If
Else
If cboRoles.SelectedValue = "-1" Then
'alert("Please select...");
Return
End If
End If
'End Test only
Dim strACL As String '= String.Empty
Dim s As New StringBuilder()
s.Append("MERGE INTO msrFormPerm AS T ") 'Target
s.Append("USING ( VALUES ")
Dim dt As DataTable = DirectCast(ViewState("curPagesysMenus"), DataTable)
Dim dr As DataRow = dt.Rows(0)
Dim _iCount As Integer = 0 'Check Change.
For Each item In Me.Repeater1.Items
strACL = String.Empty
If ((item.ItemType = ListItemType.Item) OrElse (item.ItemType = ListItemType.AlternatingItem)) Then
'Dim drv As DataRowView = DirectCast(item.DataItem, DataRowView) 'item.DataItem are all null
Dim lblMenuName As Label = DirectCast(item.FindControl("lblMenuName"), Label)
Enumerable.ToList(Of Control)((From c In Enumerable.Cast(Of Control)(item.Controls)
Where (c.GetType Is GetType(CheckBox))
Select c)).ForEach(Sub(c As Control)
strACL &= If(DirectCast(c, CheckBox).Checked, "1", "0")
End Sub)
'If Not lblMenuName.Attributes.Item("ACL").Equals(strACL) Then
' _iCount += 1
'End If
s.Append("(999, ") 'Maybe Fixed Branch 999
s.Append(lblMenuName.Attributes.Item("MenuID") & ", ")
If btnSave.ID = "btnSaveU" Then
s.Append("0, ") '0 = Users ID, Otherwise is Group ID
s.Append(cboUsers.SelectedValue & ", ")
Else
s.Append(cboRoles.SelectedValue & ", ")
s.Append("0, ") '0 = Groups ID, Otherwise is User ID
End If
s.Append("'" & strACL & "'), ") 'Rem*****
End If
Next
s.Remove(s.Length - 2, 2) 'Remove End of Rem***** [,Space(1)]
s.Append(") AS S (BranchID, PageID, RoleID, UserID, ACL) ") 'Source
s.Append("ON T.BranchID = S.BranchID AND T.PageID = S.PageID AND T.RoleID = S.RoleID AND T.UserID = S.UserID ")
s.Append("WHEN MATCHED AND S.ACL = N'0000000000' THEN ")
s.Append("DELETE ")
s.Append("WHEN MATCHED THEN ")
s.Append("UPDATE SET ACL = S.ACL ")
s.Append("WHEN NOT MATCHED BY TARGET AND S.ACL <> N'0000000000' THEN ")
s.Append("INSERT (BranchID, PageID, RoleID, UserID, ACL) VALUES (S.BranchID, S.PageID, S.RoleID, S.UserID, S.ACL);")
'If _iCount > 0 Then
Dim Result = WL_Database.WL_SQLHelperExt.SetScalar(s.ToString(), 180) ' 3 x 60 = 3 นาที
If Result IsNot Nothing Then
Dim strMsg As String = "<br> จำนวน : <b>" & DirectCast(Result, Integer) & "</b> Rows"
DirectCast(Page.Master, WLMasterPageGoodLuck).ShowHideModalPopupWarning(WL_Common.PageManager.GetLabelText("SaveChangeComplete") & strMsg, True, False, "show")
End If
'End If
End Sub
#End Region
Protected Sub cboUsers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboUsers.SelectedIndexChanged
Call cboSelectMgr(cboUsers.SelectedValue, "b.UserID", Repeater1)
End Sub
Protected Sub cboRoles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboRoles.SelectedIndexChanged
Call cboSelectMgr(cboRoles.SelectedValue, "b.RoleID", Repeater2)
End Sub
Private Sub cboSelectMgr(ByVal value As String, ByVal fieldName As String, ByVal rpt As Repeater)
Dim s As New StringBuilder()
s.Append("Select a.MenuCode, b.* From sysMenus a Inner Join msrFormPerm b On a.MenuID = b.PageID ")
s.Append("Where a.IsActive = 1 And " & fieldName & " = " & value)
Dim dtsysMenus As DataTable = DirectCast(ViewState("curPagesysMenus"), DataTable)
Dim dtmsrFormPerm As DataTable = WL_BLL.GetDataTableByQuery.GetData(s.ToString())
dtsysMenus.Columns.Remove("ACL")
dtsysMenus.Columns.Add(New DataColumn() With {.ColumnName = "ACL", .DataType = GetType(String), .DefaultValue = "0000000000"})
For Each r As DataRow In dtmsrFormPerm.Rows
Dim dr = dtsysMenus.Rows.Find(r.Item("MenuCode"))
If dr IsNot Nothing Then
dr.Item("ACL") = r.Item("ACL") 'Default 0000000000
End If
Next
dtsysMenus.AcceptChanges()
ViewState("curPagesysMenus") = dtsysMenus
rpt.DataSource = dtsysMenus
rpt.DataBind()
End Sub
End Class
Imports WL_Session.GlobalSession
Imports WL_Common.PageManager
Public Class usrGoodLuckRights
Inherits System.Web.UI.UserControl
Sub New() '***** Important *****
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If ViewState("curPagesysMenus") Is Nothing Then
Dim lstmsrRoles = DirectCast(Current.msrRoles, List(Of WL_Model.msrRoles)).FindAll(Function(x) x.RoleID > 900 And x.RoleID < 1200)
cboRoles.Items.Add(New ListItem() With {.Text = "โปรดเลือก...", .Value = "-1"})
For Each r In lstmsrRoles
cboRoles.Items.Add(New ListItem() With {.Text = r.RoleName, .Value = r.RoleID.ToString()})
Next
cboRoles.SelectedIndex = 0
Dim lstmsrUsers = msrUsers.GetList().ToList()
cboUsers.Items.Add(New ListItem() With {.Text = "โปรดเลือก...", .Value = "-1"})
For Each r In lstmsrUsers
cboUsers.Items.Add(New ListItem() With {.Text = r.UserName, .Value = r.UserID.ToString()})
Next
cboUsers.SelectedIndex = 0
Dim lstsysMenus = WL_BLL.sysMenus.GetListHierarchy(4, "GoodLuck")
ViewState("curPagesysMenus") = lstsysMenus
Repeater1.DataSource = lstsysMenus
Repeater1.DataBind()
Repeater2.DataSource = lstsysMenus
Repeater2.DataBind()
End If
End Sub
'Thank : #095316 คุณ กำลังเครียด/คุณ ห้ามตอบเกินวันละ 2 กระทู้ (www.thaicreate.com)
Private Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Call RepeaterMgr(e)
End Sub
Private Sub Repeater2_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater2.ItemDataBound
Call RepeaterMgr(e)
End Sub
Private Sub RepeaterMgr(ByVal e As RepeaterItemEventArgs)
If ((e.Item.ItemType = ListItemType.Item) OrElse (e.Item.ItemType = ListItemType.AlternatingItem)) Then
Dim lstObj As WL_Model.sysMenus = DirectCast(e.Item.DataItem, WL_Model.sysMenus)
Dim lblMenuName As Label = DirectCast(e.Item.FindControl("lblMenuName"), Label)
lblMenuName.Text = lstObj.xTitleTH 'Dynamic Language.
'*****Fixed Bug's Item.DataItem ARE ALL NULL When PostBack.*******
lblMenuName.Attributes.Add("MenuID", lstObj.MenuID)
lblMenuName.Attributes.Add("ACL", lstObj.ACL)
'*****************************************************************
Enumerable.ToList(Of Control)((From c In Enumerable.Cast(Of Control)(e.Item.Controls)
Where (c.GetType Is GetType(CheckBox))
Select c)).ForEach(Sub(c)
Dim cb As CheckBox = DirectCast(c, CheckBox)
Try
cb.Checked = If(lstObj.ACL.ToString().Substring(Convert.ToSByte(cb.ID.Substring(1)), 1) = "1", True, False)
Catch ex As Exception
cb.Checked = False
End Try
End Sub)
End If
End Sub
Protected Sub SaveData(sender As Object, e As EventArgs)
Dim btnSaveID As String = DirectCast(sender, Button).ID
Dim rpt As Repeater = If(btnSaveID.Equals("btnSaveU"), Repeater1, Repeater2)
If btnSaveID = "btnSaveU" AndAlso cboUsers.SelectedValue = "-1" Then
Return 'alert("Please select...");
ElseIf btnSaveID = "btnSaveG" AndAlso cboRoles.SelectedValue = "-1" Then
Return 'alert("Please select...");
End If
Dim strACL As String
Dim lstcurRigths As New List(Of WL_Model.msrFormPerm)
For Each item In rpt.Items
strACL = String.Empty
If ((item.ItemType = ListItemType.Item) OrElse (item.ItemType = ListItemType.AlternatingItem)) Then
'*****Dim drv As DataRowView = DirectCast(item.DataItem, DataRowView) item.DataItem are all null *****
Dim lblMenuName As Label = DirectCast(item.FindControl("lblMenuName"), Label)
Enumerable.ToList(Of Control)((From c In Enumerable.Cast(Of Control)(item.Controls) Where (c.GetType Is GetType(CheckBox))
Select c)).ForEach(Sub(c)
strACL &= If(DirectCast(c, CheckBox).Checked, "1", "0")
End Sub)
lstcurRigths.Add(New WL_Model.msrFormPerm With {.BranchID = 999, .PageID = lblMenuName.Attributes.Item("MenuID"),
.RoleID = If(btnSaveID.Equals("btnSaveU"), 0, cboUsers.SelectedValue),
.UserID = If(btnSaveID.Equals("btnSaveG"), cboRoles.SelectedValue, 0),
.ACL = strACL
})
End If
Next
Dim Result = WL_Database.WL_SQLHelperExt.SetScalar(WL_BLL.msrFormPerm.SetInsertCommand(lstcurRigths), 180) 'TimeOut 3 x 60 = 3 นาที
If Result IsNot Nothing Then
DirectCast(Page.Master, WLMasterPageGoodLuck).ShowHideModalPopupWarning(GetLabelText("SaveChangeComplete") & "<br> จำนวน : <b>" & DirectCast(Result, Integer) & "</b> Rows", True, False, "show")
End If
End Sub
Protected Sub cboUsers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboUsers.SelectedIndexChanged
Call cboSelectMgr(cboUsers.SelectedValue, "UserID", Repeater1)
End Sub
Protected Sub cboRoles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboRoles.SelectedIndexChanged
Call cboSelectMgr(cboRoles.SelectedValue, "RoleID", Repeater2)
End Sub
Private Sub cboSelectMgr(ByVal value As String, ByVal fieldName As String, ByVal rpt As Repeater)
Dim lstcurRights = WL_BLL.msrFormPerm.GetPagesACL(fieldName, value)
Dim lstObj = DirectCast(ViewState("curPagesysMenus"), List(Of WL_Model.sysMenus))
For Each r In lstObj
Dim tmp = lstcurRights.Find(Function(x) x.PageID = r.MenuID)
r.ACL = If(tmp Is Nothing, "0000000000", tmp.ACL)
Next
ViewState("curPagesysMenus") = lstObj
rpt.DataSource = lstObj
rpt.DataBind()
End Sub
End Class