Answered by:
Help with Server Template Control issue

Question
-
User563335125 posted
I will welcome any help on this.
The background: I'm doing an upgrade of a program from ASP.net 1.1 up to VS.Net 2008.
I'm trying to re-create a sharable control (because more than one program uses this control, and
we would like to be able to, should an error come up, fix it in one place instead of 5).
So I'm creating a custom web control. (I'm new to this so forgive me if my explanation is shoddy).
The control has a dropdown, a textbox and a button (this makes up the search, it allows you to select a type,
put in some words to search on, and a button to initiate the search). The search populates a datagrid.
The datagrid needs to have checkboxes that allow the user to select whichever ones they want and then they can click on an
"Add" button underneath the grid to add the selected rows into another grid that will be saved to the database.
My problem is that currently, when trying to do a custom template field for the gridview checkbox column
(because the checkbox field, when bound, all the checkboxes are disabled, and I've only found the only way to enable them
is to enable editing on that single row, so it isn't feasible for usablitiy) that when I send a postback, the checkboxes disappear.
I've been trying to implement a check/uncheck all header checkbox, and I have an event handler for it to fire when the
header checkbox is checked or unchecked, it will do the same to all the under checkboxes. Currently that postback causes
all of the checkboxes in that row to disappear... even if there is data bound to them.
Can anyone help me? Here is my code so far:
<ToolboxData("<{0}:Search runat=server></{0}:ISIOrgSearch2>")> _
Public Class Search
Inherits CompositeControl
Implements INamingContainer
Implements IPostBackDataHandler
#Region "Properties"
Private mBusinessTier As BusinessTier
Private WithEvents mUpdatePanel As New UpdatePanel
Private WithEvents mSearchDropDown As New DropDownList
Private WithEvents mWordSearchTextBox As New TextBox
Private WithEvents mSearchButton As New Button
Private WithEvents mSearchGridView As New GridView
Private WithEvents mAddAccessButton As New Button
Private WithEvents mEntireAccessCheckBox As New CheckBox
Private WithEvents mAccessGridView As New GridView
Private WithEvents mRemoveAccessButton As New Button
Private WithEvents mSearchHeaderCheckbox As New CheckBox
Private WithEvents mAccessHeaderCheckbox As New CheckBox
Private mAccessData As DataTable
Private mTitle As String = ""
Private mSkinGridID As String = ""Public Property GridSkinID() As String
Get
Return mSkinGridID
End Get
Set(ByVal value As String)
mSkinGridID = value
End Set
End Property
Public Property accessDT() As DataTable
Get
Return mAccessData
End Get
Set(ByVal value As DataTable)
mAccessData = value
End Set
End Property
Public Sub New()
End Sub
Thanks again for your help. I've been wracking my brain for the past week trying to get this to work. Any other ideas would be appreciated.
Private Sub InitializeVariables()
mBusinessTier = New BusinessTier(varToStr(Page.Session("ConnectionString")))
End Sub
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
Controls.Clear()
InitializeVariables()
' Design mode display
If DesignMode Then
Controls.Add(New LiteralControl("<span>Search Control ID=" & Me.ID & "</span><br />"))
ChildControlsCreated = True
ClearChildViewState()
Exit Sub
End If
' Check that partial update is allowed.
Dim sm As ScriptManager = ScriptManager.GetCurrent(Page)
mUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional
mUpdatePanel.EnableViewState = True
mUpdatePanel.ChildrenAsTriggers = True
With mUpdatePanel.ContentTemplateContainer.Controls
.Clear()
' DropDownlist
mSearchDropDown = New DropDownList
With mSearchDropDown
.ID = "searchTypeDropDown"
.Attributes.Add("style", "float:left; margin-left:8px;")
AddHandler mSearchDropDown.SelectedIndexChanged, AddressOf searchDropDown_SelectedIndexChanged
' TODO: Add items to listbox.
.Items.Add(New ListItem("Name", "Name"))
.Items.Add(New ListItem("Code", "Code"))
.Items.Add(New ListItem("Owner Name", "OwnerName"))
.Items.Add(New ListItem("Extension", "Extension"))
If varToBool(Page.Session("isAdmin")) Then
.Items.Add(New ListItem("Authorization Code", "AuthCode"))
End If
End With
.Add(mSearchDropDown)
.Add(New LiteralControl("<label for=""" & Me.ID & "_wordSearchTextBox"" style=""float:left;margin-left:20px"">Word Search:</label>"))
' Search textbox
mWordSearchTextBox = New TextBox
With mWordSearchTextBox
.ID = "wordSearchTextBox"
.Attributes.Add("style", "float:left; margin-left:8px;")
End With
.Add(mWordSearchTextBox)
mSearchButton = New Button
With mSearchButton
.ID = "searchButton"
.Text = "Search"
.Attributes.Add("style", "float:left;margin-left:30px;")
If Len(mCssClassButton) > 0 Then
.CssClass = mCssClassButton
End If
AddHandler mSearchButton.Click, AddressOf searchButton_Click
End With
.Add(mSearchButton)
.Add(New LiteralControl("</div><br />"))
.Add(New LiteralControl("<div style=""width:100%;"" align=""left"">Search Result:</div>"))
With mSearchGridView
.AutoGenerateColumns = False
.AllowPaging = False
.EnableTheming = True
.EnableViewState = True
.SkinID = mSkinGridID
End With
.Add(mSearchGridView)
Dim p As New Panel
With p
.Attributes.Add("style", "width:100%;text-align:right;clear:both;")
.ID = "buttonPanel"
With .Controls
mAddAccessButton = New Button
With mAddAccessButton
.Attributes.Add("style", "float:right; margin-right: 15px;")
.ID = "AddAccessButton"
.Text = "Add"
If Len(mCssClassButton) > 0 Then .CssClass = mCssClassButton
AddHandler mAddAccessButton.Click, AddressOf addButton_Click
End With
.Add(mAddAccessButton)
End With
End With
.Add(p)
.Add(New LiteralControl("<br />"))
'add mEntireAccessCheckBox here
'add mAccessGridView here
Dim PNL As New Panel
With PNL
.Attributes.Add("style", "width:100%;text-align:right;clear:both;")
.ID = "removeButtonPanel"
With .Controls
mRemoveAccessButton = New Button
With mRemoveAccessButton
.Attributes.Add("style", "float:right; margin-right: 15px;")
.ID = "RemoveAccessButton"
.Text = "Remove"
If Len(mCssClassButton) > 0 Then .CssClass = mCssClassButton
AddHandler mRemoveAccessButton.Click, AddressOf removeButton_Click
End With
.Add(mRemoveAccessButton)
End With
End With
.Add(PNL)
End With
Me.Controls.Add(mUpdatePanel)
Me.ChildControlsCreated = True
Me.ClearChildViewState()
End Sub
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
' REQUIRED to hook PostBack
MyBase.OnInit(e)
Page.RegisterRequiresPostBack(Me)
End Sub
Private Sub Search_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Page.RegisterRequiresPostBack(Me)
End Sub
Protected Sub searchButton_Click(ByVal sender As Object, ByVal e As EventArgs)
'when the search button is clicked, populate the search grid.
InitializeVariables()
Dim doNotSearchIDs As New ArrayList
Dim mAccessGridViewDT As New DataTable
If mAccessGridView IsNot Nothing AndAlso mAccessGridView.Rows.Count > 0 Then
For x As Integer = 0 To mAccessGridView.Rows.Count
Dim accessRow As Control = mAccessGridView.Controls(0).Controls(x)
Dim currentCheckBox As CheckBox = accessRow.FindControl("accessCheckbox")
If currentCheckBox.Checked Then
doNotSearchIDs.Add("ID") 'name with correct column name, find a way to extract data out of this row.
End If
Next
End If
Dim whereClause As String = varToStr(Page.Session("WhereClause"))
Dim userID As String = varToStr(Page.Session("userid"))
Dim searchData As New DataTable("SearchData")
Dim orgNames As String() = varToStr(Page.Session("OrgNames")).Split("|")
searchData = mBusinessTier.GetOrgSearch(mWordSearchTextBox.Text, mSearchDropDown.SelectedValue.ToString, doNotSearchIDs, userID, whereClause)
searchData.Columns.Add(New DataColumn("Level"))
searchData.Columns.Add(New DataColumn("IsChecked"))
'This is used to convert values into quick-user friendly data instead of just IDs
If searchData IsNot Nothing AndAlso searchData.Rows.Count > 0 Then
For Each dr As DataRow In searchData.Rows
dr("Level") = orgNames(varToInt(dr("Lvl")))
dr("IsChecked") = False
Next
searchData.AcceptChanges()
End If
BindSearchGrid(searchData)
mUpdatePanel.Update()
End Sub
Private Sub BindSearchGrid(ByVal searchData As DataTable)
Dim name As New System.Web.UI.WebControls.BoundField
Dim level As New System.Web.UI.WebControls.BoundField
Dim parent As New System.Web.UI.WebControls.BoundField
Dim checkField As New TemplateField
checkField.HeaderTemplate = New GridViewCheckboxTemplate(ListItemType.Header, "isChecked")
checkField.ItemTemplate = New GridViewCheckboxTemplate(ListItemType.Item, "isChecked")
name.DataField = "Name"
name.HeaderText = "Org. / Person Name"
level.DataField = "Level"
level.HeaderText = "Level"
parent.DataField = "DisplayName"
parent.HeaderText = "Parent"
With mSearchGridView
mSearchGridView.Columns.Clear()
.Columns.Add(checkField)
.Columns.Add(orgPersonName)
.Columns.Add(level)
.Columns.Add(parent)
.DataSource = searchData
.DataBind()
mSearchHeaderCheckbox = mSearchGridView.HeaderRow.FindControl("gridCheckBox")
Dim headerCheck As CheckBox = mSearchGridView.HeaderRow.FindControl("gridCheckBox")
headerCheck.AutoPostBack = True
headerCheck.Attributes.Add("onCheckChanged", "OnSearchCheckChanged")
If headerCheck IsNot Nothing Then
AddHandler headerCheck.CheckedChanged, AddressOf OnSearchCheckChanged
End If
.DataBind()
.Visible = True
.Enabled = True
End With
End Sub
Private Sub OnSearchCheckChanged(ByVal sender As Object, ByVal e As EventArgs) Handles mSearchHeaderCheckbox.CheckedChanged
Dim newCheckedState As Boolean = Not previousCheckedState
Dim checkField As New TemplateField
checkField.HeaderTemplate = New GridViewCheckboxTemplate()
checkField.ItemTemplate = New GridViewCheckboxTemplate()
mSearchGridView.Columns(0) = checkField
Dim headerCheck As CheckBox = mSearchGridView.HeaderRow.FindControl("gridCheckBox")
headerCheck.AutoPostBack = True
headerCheck.Attributes.Add("onCheckChanged", "OnSearchCheckChanged")
If mSearchHeaderCheckbox IsNot Nothing Then 'headerCheck IsNot Nothing Then
AddHandler headerCheck.CheckedChanged, AddressOf OnSearchCheckChanged
End If
If mSearchHeaderCheckbox IsNot Nothing Then
For Each dr As GridViewRow In mSearchGridView.Rows
Dim checkbox As CheckBox = dr.FindControl("gridCheckBox")
If checkbox IsNot Nothing Then
checkbox.Checked = mSearchHeaderCheckbox.Checked
End If
Next
End If
Page.Session("SearchCheckBoxSelected") = newCheckedState
End Sub
End Class
Public Class GridViewCheckboxTemplate
Implements ITemplate
Implements INamingContainer
Dim mType As ListItemType
Dim mColumnName As String
Public Sub New(ByVal type As ListItemType, ByVal colName As String)
mType = Type
mColumnName = colName
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim check As New CheckBox
check.ID = "gridCheckBox"
If mType = ListItemType.Item Then
AddHandler check.DataBinding, AddressOf BindData
End If
container.Controls.Add(check)
End Sub
Public Sub BindData(ByVal sender As Object, ByVal e As EventArgs)
Dim check As CheckBox = CType(sender, CheckBox)
Dim container As GridViewRow = CType(check.NamingContainer, GridViewRow)
Dim dataValue As Object = DataBinder.Eval(container.DataItem, mColumnName)
If dataValue IsNot Nothing Then
check.Checked = varToBool(dataValue)
End If
End Sub
End ClassTuesday, April 8, 2008 4:41 PM
Answers
-
User-16411453 posted
start Here first, remove the new word, and with events.
Private mUpdatePanel As UpdatePanel()
Private mSearchDropDown As DropDownListShould remark this out for debugging, don't need the Me in your code, In CS they use 'this' alot, in vb I don't think it matters - I have to go and bowl now, later
If DesignMode Then Controls.Add(New LiteralControl("<span>Search Control ID=" & [ID] & "</span><br />")) ChildControlsCreated = True ClearChildViewState() Exit Sub
End If
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 9, 2008 9:12 PM
All replies
-
User563335125 posted
I'm completely stumped on this one. I'm going to work on some other things for now in hopes that someone can help me on this, or at least point me in the direction of where I could go for some examples of using stuff like this with the programmatic templatefield.Wednesday, April 9, 2008 2:59 PM -
User-16411453 posted
I've looked at it several times, I don't do grids. Code looks OK, but you left the line numbers in, and no one wants to spend alot of time stripping out numbers. Edit your post without the line numbers and I'll load it up.
Wednesday, April 9, 2008 9:04 PM -
User-16411453 posted
start Here first, remove the new word, and with events.
Private mUpdatePanel As UpdatePanel()
Private mSearchDropDown As DropDownListShould remark this out for debugging, don't need the Me in your code, In CS they use 'this' alot, in vb I don't think it matters - I have to go and bowl now, later
If DesignMode Then Controls.Add(New LiteralControl("<span>Search Control ID=" & [ID] & "</span><br />")) ChildControlsCreated = True ClearChildViewState() Exit Sub
End If
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 9, 2008 9:12 PM -
User563335125 posted
I have removed the line numbers. Sorry, I didn't think about the cut-copy problem... I was hoping someone could reference a line number directly.
Thanks again!
Thursday, April 10, 2008 10:40 AM -
User563335125 posted
OK, I've removed the WithEvents and the new, but the event doesn't fire now for the header checkbox. Any ideas?
Thursday, April 10, 2008 11:10 AM -
User-16411453 posted
I tried to copy and paste your code, but it came out with no linefeeds, just a straight line of code.
In my opinion, you have lots of errors in your code, Too Many to list all. Mostly control object errors.
The With Events, exposes the entire control to the user. You have to use it carefully. Better to not use it, then use it if you have to.
Private WithEvents mUpdatePanel As New UpdatePanel
Private mUpdatePanel As UpdatePanel ' Use this one
I prefer the following, Dim the control in create child controls, instead of a global private object. You declaring the object as new twice, once in global private and once in CreateChildControls.
Dim mSearchDropDown As DropDownList
mSearchDropDown = New DropDownList
With mSearchDropDown
.ID = "searchTypeDropDown"
.Attributes.Add("style", "float:left; margin-left:8px;") ' This should be
.Attributes.Style.Add(HtmlTextWriterStyle.Float, "left")
.Attributes.Style.Add(HtmlTextWriterStyle.padding-left, "8px")
AddHandler mSearchDropDown.SelectedIndexChanged, AddressOf searchDropDown_SelectedIndexChanged
' TODO: Add items to listbox.
.Items.Add(New ListItem("Name", "Name"))
.Items.Add(New ListItem("Code", "Code"))
.Items.Add(New ListItem("Owner Name", "OwnerName"))
.Items.Add(New ListItem("Extension", "Extension"))
If varToBool(Page.Session("isAdmin")) Then
.Items.Add(New ListItem("Authorization Code", "AuthCode"))
End If
End With
On the Me Word, and ID, I dont't know where ID comes from. There's [ID], which comes from the controls ID Assigment in the properties panel .
This came from one of your lines of code, I rewrote it the long way so you get the idea of dynamic control creation practices.
Dim Literal1 As LiteralControl
Literal1 = New LiteralControl
Literal1.Text = "<span>Search Control ID=" & [ID] & "</span>")
Controls.Add(Literal1)
Just FYI, a label control just produces span tags, you could of just made a label control with the text being the literal1.Text.
I find it strange how advance the code module is, but the code module has lots of object errors in it. I'm surprised it renders at all actually. I think this is the source of your anomolies.
Thursday, April 10, 2008 12:53 PM -
User-16411453 posted
On the Checkbox issue, you have to raise the event yourself through code. That's why you have checkbox issues in the first place.
Your really better off fixing the controls so they render correctly and stay in place on the page and in state, then go back and add the data to the control, then add the final events needed to fnish the mechanics of the control. You'll have way less problems with the final product, and you'll get it to actually 100% work.Public Class AskQuestion_Control Inherits WebControl Implements IPostBackDataHandler Public Event CheckChanged As EventHandler Public Event ButtonClick As EventHandler Public imgHeader As System.Web.UI.WebControls.Image
OnCheckChanged(EventArgs.Empty)
End Sub
Protected Overridable Sub OnCheckChanged(ByVal e As EventArgs)
RaiseEvent CheckChanged(Me, e)
End SubThursday, April 10, 2008 1:09 PM -
User563335125 posted
jkirk, thanks for your response. I don't know why the insert code funtionality on this site made it all go wonky, but I apologize... I'll try to get it working again.
I was declaring it as a private global variable so hopefully I'd be able to access the controls from different functions on the page (Like the Create Child controls, the Bind data, and then in the Add and Remove on click events). The literal stuff is something a coworker had suggested. It currently doesn't render that correctly so I'll be changing it to an asp label or something that just sits there and it won't render funny anymore. I have since removed all of the "with events" and the "news" from the declarations of the global controls. I have also removed the "me" usage from the code, as that was also suggested by a coworker. Sorry for not refining it more, but I wanted to show you what I was trying to do with the code. I'll post an updated version of the code once I can format it again.
The major thing I'm trying to get are those checkboxes. Whenever a postback occurs, it causes the checkboxes to disappear. If I remove the "With Events" from the global checkbox, it no longer fires when it is located in the header of the gridview.
Thursday, April 10, 2008 2:15 PM -
User563335125 posted
On the Checkbox issue, you have to raise the event yourself through code. That's why you have checkbox issues in the first place.
Your really better off fixing the controls so they render correctly and stay in place on the page and in state, then go back and add the data to the control, then add the final events needed to fnish the mechanics of the control. You'll have way less problems with the final product, and you'll get it to actually 100% work.
Public Class AskQuestion_Control Inherits WebControl Implements IPostBackDataHandler Public Event CheckChanged As EventHandler Public Event ButtonClick As EventHandler Public imgHeader As System.Web.UI.WebControls.Image
Public Overridable Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
OnCheckChanged(EventArgs.Empty)
End Sub
Protected Overridable Sub OnCheckChanged(ByVal e As EventArgs)
RaiseEvent CheckChanged(Me, e)
End Sub<ToolboxData("<{0}:Search runat=server></{0}:Search>")> _ Public Class Search Inherits CompositeControl Implements INamingContainer Implements IPostBackDataHandler #Region "Properties" Private mBusinessTier As BusinessTier Private mUpdatePanel As UpdatePanel Private mSearchDropDown As DropDownList Private mWordSearchTextBox As TextBox Private mSearchButton As Button Private mSearchGridView As GridView Private mAddAccessButton As Button Private mEntireAccessCheckBox As CheckBox Private mAccessGridView As GridView Private mRemoveAccessButton As Button Private mSearchHeaderCheckbox As CheckBox Private mAccessHeaderCheckbox As CheckBox Private mAccessData As DataTable Public Event CheckChanged As EventHandler Public Event ButtonClick As EventHandler Private mCssClassDropList As String = "" Private mCssClassFindText As String = "" Private mCssClassBody As String = "" Private mCssClassButton As String Private mCssClassTextBox As String Private mCSSClassGrid As String = "" Private mSkinGridID As String = "" Private mIsDirectory As Boolean = False Private mTitle As String = "" Private mPathToArray As String = "" Private misCode As Boolean = False Private chkBox As String = "" 'Private dq As String = Chr(34) 'Private sq As String = Chr(39) <Bindable(True), Category("Layout"), DefaultValue("cmd")> _ Public Property CssClassButton() As String Get Return mCssClassButton End Get Set(ByVal value As String) mCssClassButton = value End Set End Property <Bindable(True), Category("Layout"), DefaultValue("")> _ Public Property CssClassTextBox() As String Get Return mCssClassTextBox End Get Set(ByVal value As String) mCssClassTextBox = value End Set End Property <Bindable(True), Category("Layout"), DefaultValue("grid")> _ Public Property CSSClassGrid() As String Get Return mCSSClassGrid End Get Set(ByVal value As String) mCSSClassGrid = value End Set End Property Public Property GridSkinID() As String Get Return mSkinGridID End Get Set(ByVal value As String) mSkinGridID = value End Set End Property Public Property isDirectory() As Boolean Get Return mIsDirectory End Get Set(ByVal Value As Boolean) mIsDirectory = Value End Set End Property Public Property accessDT() As DataTable Get Return mAccessData End Get Set(ByVal value As DataTable) mAccessData = value End Set End Property Public Sub New() mIsDirectory = False mTitle = "" mPathToArray = "" chkBox = "" misCode = False End Sub Public Property Title() As String Get Return mTitle End Get Set(ByVal value As String) mTitle = value End Set End Property #End Region #Region "Methods" Private Sub InitializeVariables() mBusinessTier = New BusinessTier(varToStr(Page.Session("ConnStr"))) End Sub Protected Overrides Sub CreateChildControls() MyBase.CreateChildControls() Me.Controls.Clear() InitializeVariables() ' Design mode display If DesignMode Then Controls.Add(New LiteralControl("<span>ISIOrgSearch Control ID=" & Me.ID & "</span><br />")) ChildControlsCreated = True ClearChildViewState() Exit Sub End If ' Check that partial update is allowed. Dim sm As ScriptManager = ScriptManager.GetCurrent(Page) If mUpdatePanel Is Nothing Then mUpdatePanel = New UpdatePanel End If mUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional mUpdatePanel.EnableViewState = True mUpdatePanel.ChildrenAsTriggers = True With mUpdatePanel.ContentTemplateContainer.Controls .Clear() Dim searchLabel As New Label searchLabel.Text = "Search For:" .Add(searchLabel) ' DropDownlist mSearchDropDown = New DropDownList With mSearchDropDown .ID = "searchTypeDropDown" .Attributes.Add("style", "float:left; margin-left:8px;") AddHandler mSearchDropDown.SelectedIndexChanged, AddressOf searchDropDown_SelectedIndexChanged ' TODO: Add items to listbox. .Items.Add(New ListItem("Org. Name", "OrgName")) .Items.Add(New ListItem("Org. Code", "OrgCode")) .Items.Add(New ListItem("Owner Name", "OwnerName")) .Items.Add(New ListItem("Extension", "Extension")) If varToBool(Page.Session("isAdmin")) Then .Items.Add(New ListItem("Auth. Code", "AuthCode")) End If End With .Add(mSearchDropDown) Dim wordSearchLabel As New Label wordSearchLabel.Text = "Word Search:" .Add(wordSearchLabel) ' Search textbox mWordSearchTextBox = New TextBox With mWordSearchTextBox .ID = "wordSearchTextBox" .Attributes.Add("style", "float:left; margin-left:8px;") End With .Add(mWordSearchTextBox) mSearchButton = New Button With mSearchButton .ID = "searchButton" .Text = "Search" .Attributes.Add("style", "float:left;margin-left:30px;") If Len(mCssClassButton) > 0 Then .CssClass = mCssClassButton End If AddHandler mSearchButton.Click, AddressOf searchButton_Click End With .Add(mSearchButton) Dim searchResultLabel As New Label searchResultLabel.Text = "Search Result:" .Add(searchResultLabel) If mSearchGridView Is Nothing Then mSearchGridView = New GridView End If With mSearchGridView .AutoGenerateColumns = False .AllowPaging = False .EnableTheming = True .EnableViewState = True .SkinID = mSkinGridID End With .Add(mSearchGridView) Dim P As New Panel With P .Attributes.Add("style", "width:100%;text-align:right;clear:both;") .ID = "buttonPanel" With .Controls mAddAccessButton = New Button With mAddAccessButton .Attributes.Add("style", "float:right; margin-right: 15px;") .ID = "AddAccessButton" .Text = "Add" If Len(mCssClassButton) > 0 Then .CssClass = mCssClassButton AddHandler mAddAccessButton.Click, AddressOf addButton_Click End With .Add(mAddAccessButton) End With End With .Add(P) .Add(New LiteralControl("<br />")) Dim PNL As New Panel With PNL .Attributes.Add("style", "width:100%;text-align:right;clear:both;") .ID = "removeButtonPanel" With .Controls mRemoveAccessButton = New Button With mRemoveAccessButton .Attributes.Add("style", "float:right; margin-right: 15px;") .ID = "RemoveAccessButton" .Text = "Remove" If Len(mCssClassButton) > 0 Then .CssClass = mCssClassButton AddHandler mRemoveAccessButton.Click, AddressOf removeButton_Click End With .Add(mRemoveAccessButton) End With End With .Add(PNL) End With Me.Controls.Add(mUpdatePanel) Me.ChildControlsCreated = True Me.ClearChildViewState() End Sub Protected Overrides Sub OnInit(ByVal e As System.EventArgs) ' REQUIRED to hook PostBack MyBase.OnInit(e) Page.RegisterRequiresPostBack(Me) End Sub Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData End Function Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent OnSearchCheckChanged(mSearchHeaderCheckbox, EventArgs.Empty) End Sub Protected Overridable Sub OnCheckChanged(ByVal e As EventArgs) RaiseEvent CheckChanged(Me, e) End Sub ' Required for LoadPostData() event to fire. Private Sub Search_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init Page.RegisterRequiresPostBack(Me) End Sub Protected Sub searchDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 'no need to do anything here. This is just here for future expansion should it be needed. End Sub Protected Sub searchButton_Click(ByVal sender As Object, ByVal e As EventArgs) 'when the search button is clicked, populate the search grid. InitializeVariables() Dim doNotSearchIDs As New ArrayList Dim mAccessGridViewDT As New DataTable 'placeholder until we finalize this. If mAccessGridView IsNot Nothing AndAlso mAccessGridView.Rows.Count > 0 Then For x As Integer = 0 To mAccessGridView.Rows.Count 'GateUserTypeDT.Rows Dim accessRow As Control = mAccessGridView.Controls(0).Controls(x) Dim currentCheckBox As CheckBox = accessRow.FindControl("accessCheckbox") If currentCheckBox.Checked Then doNotSearchIDs.Add("ID") 'name with correct column name, find a way to extract data out of this row. End If Next End If Dim whereClause As String = varToStr(Page.Session("WhereClause")) Dim userID As String = varToStr(Page.Session("userid")) Dim searchData As New DataTable("SearchData") Dim orgNames As String() = varToStr(Page.Session("OrgNames")).Split("|") searchData = mBusinessTier.GetOrgSearch(mWordSearchTextBox.Text, mSearchDropDown.SelectedValue.ToString, doNotSearchIDs, userID, whereClause) searchData.Columns.Add(New DataColumn("Level")) searchData.Columns.Add(New DataColumn("IsChecked")) If searchData IsNot Nothing AndAlso searchData.Rows.Count > 0 Then For Each dr As DataRow In searchData.Rows dr("Level") = orgNames(varToInt(dr("Lvl"))) dr("IsChecked") = False Next searchData.AcceptChanges() End If BindSearchGrid(searchData) mUpdatePanel.Update() End Sub Private Sub BindSearchGrid(ByVal searchData As DataTable) Dim orgPersonName As New System.Web.UI.WebControls.BoundField Dim level As New System.Web.UI.WebControls.BoundField Dim parent As New System.Web.UI.WebControls.BoundField Dim checkField As New TemplateField checkField.HeaderTemplate = New GridViewCheckboxTemplate(ListItemType.Header, "isChecked") checkField.ItemTemplate = New GridViewCheckboxTemplate(ListItemType.Item, "isChecked") orgPersonName.DataField = "Name" orgPersonName.HeaderText = "Org. / Person Name" level.DataField = "Level" level.HeaderText = "Level" parent.DataField = "DisplayName" parent.HeaderText = "Parent" With mSearchGridView 'add columns for checkbox, Person Name (name column), Level (level column), 'Parent (Parent Column but redo how it looks) mSearchGridView.Columns.Clear() .Columns.Add(checkField) .Columns.Add(personName) .Columns.Add(level) .Columns.Add(parent) .DataSource = searchData .DataBind() mSearchHeaderCheckbox = mSearchGridView.HeaderRow.FindControl("gridCheckBox") If mSearchHeaderCheckbox Is Nothing Then mSearchHeaderCheckbox = New CheckBox End If mSearchHeaderCheckbox.AutoPostBack = True AddHandler mSearchHeaderCheckbox.CheckedChanged, AddressOf OnSearchCheckChanged .DataBind() .Visible = True .Enabled = True .EditIndex = -1 End With End Sub Private Sub BindAccessGrid(ByVal accessData As DataTable) Dim personName As New System.Web.UI.WebControls.BoundField Dim level As New System.Web.UI.WebControls.BoundField Dim parent As New System.Web.UI.WebControls.BoundField Dim checkField As New TemplateField checkField.ItemTemplate = New GridViewCheckboxTemplate(ListItemType.Item, "isChecked") personName.DataField = "Name" personName.HeaderText = "Person Name" level.DataField = "Level" level.HeaderText = "Level" parent.DataField = "DisplayName" parent.HeaderText = "Parent" With mAccessGridView 'add columns for checkbox, Org. / Person Name (name column), Level (level column), If Not .Columns.Count > 0 Then .Columns.Add(checkField) 'once the check field is added to the grid, find the grid, go to the header and add an event. .Columns.Add(personName) .Columns.Add(level) .Columns.Add(parent) End If .DataSource = accessData .DataBind() mAccessHeaderCheckbox = mAccessGridView.HeaderRow.FindControl("gridCheckBox") Page.Session("SearchCheckBoxSelected") = False AddHandler mAccessHeaderCheckbox.CheckedChanged, AddressOf OnSearchAccessCheckChanged .Visible = True .Enabled = True End With End Sub ''' <summary> ''' This function is fired when the header checkbox is changed it's checking. It's used to select everything in the Search grid. ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub OnSearchCheckChanged(ByVal sender As Object, ByVal e As EventArgs) If mSearchHeaderCheckbox IsNot Nothing Then For Each dr As GridViewRow In mSearchGridView.Rows Dim checkbox As CheckBox = dr.FindControl("gridCheckBox") If checkbox IsNot Nothing Then checkbox.Checked = mSearchHeaderCheckbox.Checked End If Next End If End Sub ''' <summary> ''' This function is fired when the header checkbox is changed it's checking. It's used to select everything in the Access grid. ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub OnSearchAccessCheckChanged(ByVal sender As Object, ByVal e As EventArgs) mAccessHeaderCheckbox = mAccessGridView.HeaderRow.FindControl("gridCheckBox") If mAccessHeaderCheckbox IsNot Nothing Then For Each dr As GridViewRow In mAccessGridView.Rows Dim checkbox As CheckBox = dr.FindControl("gridCheckBox") checkbox.Checked = mAccessHeaderCheckbox.Checked Next End If End Sub Protected Sub removeButton_Click(ByVal sender As Object, ByVal e As EventArgs) 'when the remove button is clicked, remove the selected IDS from the AccessGrid and add re-search. End Sub Protected Sub addButton_Click(ByVal sender As Object, ByVal e As EventArgs) 'add the selected ids to the access grid, but remove them from the search grid. Aslo change the 'where clause to remove them from the search (wehere ID IN (For each item in gridview.rows.finditem("columnname") or something like that) Dim changed As Boolean = False Dim whereClause As New StringBuilder For Each row As GridViewRow In mSearchGridView.Rows Dim check As CheckBox = row.FindControl("searchCheck") If check IsNot Nothing AndAlso check.Checked Then 'remove gridview row and add it to other box 'convert gridview row to regular data row and add it to access datatable AccessDT.Rows.Add(row) whereClause.Append("") changed = True End If Next If changed Then 'rebind both grids. Add restricted IDs to search! End If End Sub #End Region End Class Public Class GridViewCheckboxTemplate Implements ITemplate Implements INamingContainer Private ThisId As String = "" Dim mType As ListItemType Dim mColumnName As String Public Sub GridCiewCheckboxTemplate() End Sub Public Sub New(ByVal type As ListItemType, ByVal colName As String) mType = Type mColumnName = colName End Sub Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn Dim check As New CheckBox check.ID = "gridCheckBox" If mType = ListItemType.Item Then AddHandler check.DataBinding, AddressOf BindData End If container.Controls.Add(check) End Sub Public Sub BindData(ByVal sender As Object, ByVal e As EventArgs) Dim check As CheckBox = CType(sender, CheckBox) Dim container As GridViewRow = CType(check.NamingContainer, GridViewRow) Dim dataValue As Object = DataBinder.Eval(container.DataItem, mColumnName) If dataValue IsNot Nothing Then check.Checked = varToBool(dataValue) End If End Sub End Class
Thanks again for looking to this. I really appreciate your help!
Thursday, April 10, 2008 2:44 PM -
User-16411453 posted
What's the control suppose to look like when the user uses it. I worked on it for about 30 minutes, but after rendering, I had no idea how it should look. Didn't have time for the update panel. No data either
If the control looks useful to me, I don't mind fixing it for you. See what you can do with it.
Imports System Imports System.Collections Imports System.Collections.Specialized Imports System.Collections.Generic Imports System.ComponentModel Imports System.Text Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Drawing.Design Imports AjaxControlToolkit Imports System.IO Imports System.Web.UI.Design Imports System.Net.Mail Imports System.XML Imports System.Data Imports System.Data.SqlClient <ToolboxData("<{0}:Search runat=server></{0}:Search>")> _ Public Class Search Inherits WebControl Implements IPostBackDataHandler 'Private mBusinessTier As BusinessTier Private mUpdatePanel As UpdatePanel Private mSearchDropDown As DropDownList Private mWordSearchTextBox As TextBox Private mSearchButton As Button Private mSearchGridView As UI.WebControls.GridView Private mAddAccessButton As Button Private mEntireAccessCheckBox As CheckBox Private mAccessGridView As UI.WebControls.GridView Private mRemoveAccessButton As Button Private mSearchHeaderCheckbox As CheckBox Private mAccessHeaderCheckbox As CheckBox Private mAccessData As DataTable Public Event CheckChanged As EventHandler Public Event ButtonClick As EventHandler Private mCssClassDropList As String = Nothing Private mCssClassFindText As String = Nothing Private mCssClassBody As String = Nothing Private mCssClassButton As String Private mCssClassTextBox As String Private mCSSClassGrid As String = Nothing Private mSkinGridID As String = Nothing Private mIsDirectory As Boolean = False Private mTitle As String = Nothing Private mPathToArray As String = Nothing Private misCode As Boolean = False Private chkBox As String = Nothing Private dq As String = Chr(34) Private sq As String = Chr(39) #Region "Methods" Private Sub InitializeVariables() 'mBusinessTier = New BusinessTier(varToStr(Page.Session("ConnStr"))) End Sub Protected Overrides Sub OnInit(ByVal e As System.EventArgs) ' REQUIRED to hook PostBack MyBase.OnInit(e) Page.RegisterRequiresPostBack(Me) End Sub Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) If Not Me.Context Is Nothing Then RenderContents(writer) Else 'RenderGridViewSearch(writer) End If End Sub Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData End Function Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent OnSearchCheckChanged(mSearchHeaderCheckbox, EventArgs.Empty) End Sub Protected Overridable Sub OnCheckChanged(ByVal e As EventArgs) RaiseEvent CheckChanged(Me, e) End Sub Protected Overrides Sub CreateChildControls() MyBase.CreateChildControls() Controls.Clear() InitializeVariables() If [Enabled] = True Then Dim Comment As LiteralControl Comment = New LiteralControl Comment.Text = "<!-- GridView Search Control V1.0 for ASP.NET -->" & vbCrLf Controls.Add(Comment) Dim mUpdatePanel As UpdatePanel mUpdatePanel = New UpdatePanel mUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional mUpdatePanel.EnableViewState = True mUpdatePanel.ChildrenAsTriggers = True Controls.Add(mUpdatePanel) Dim table As Table table = New Table table.Width = [Width] table.Height = [Height] table.BorderColor = [BorderColor] table.BorderStyle = [BorderStyle] table.BorderWidth = [BorderWidth] table.CellPadding = 0 table.CellSpacing = 0 Controls.Add(table) mUpdatePanel.ContentTemplateContainer.Controls.Add(table) Dim trSearch As TableRow trSearch = New TableRow table.Controls.Add(trSearch) Dim tdSearchLeft As TableCell tdSearchLeft = New TableCell tdSearchLeft.ColumnSpan = 1 tdSearchLeft.Style.Add(HtmlTextWriterStyle.TextAlign, "left") tdSearchLeft.Style.Add(HtmlTextWriterStyle.Height, "46px") tdSearchLeft.VerticalAlign = VerticalAlign.Middle trSearch.Controls.Add(tdSearchLeft) Dim wordSearchFor As Label wordSearchFor = New Label wordSearchFor.Text = "Search For:" tdSearchLeft.Controls.Add(wordSearchFor) mSearchDropDown = New DropDownList mSearchDropDown.ID = "searchTypeDropDown" mSearchDropDown.Attributes.Add("style", "float:left; margin-left:8px;") AddHandler mSearchDropDown.SelectedIndexChanged, AddressOf searchDropDown_SelectedIndexChanged mSearchDropDown.Items.Add(New ListItem("Org. Name", "OrgName")) mSearchDropDown.Items.Add(New ListItem("Org. Code", "OrgCode")) mSearchDropDown.Items.Add(New ListItem("Owner Name", "OwnerName")) mSearchDropDown.Items.Add(New ListItem("Extension", "Extension")) If Convert.ToBoolean(Page.Session("isAdmin")) Then mSearchDropDown.Items.Add(New ListItem("Auth. Code", "AuthCode")) End If tdSearchLeft.Controls.Add(mSearchDropDown) Dim tdSearchRight As TableCell tdSearchRight = New TableCell tdSearchRight.ColumnSpan = 1 tdSearchRight.Style.Add(HtmlTextWriterStyle.TextAlign, "center") tdSearchRight.Style.Add(HtmlTextWriterStyle.Height, "46px") tdSearchRight.VerticalAlign = VerticalAlign.Middle trSearch.Controls.Add(tdSearchRight) Dim wordSearchLabel As Label wordSearchLabel = New Label wordSearchLabel.Text = "Word Search:" wordSearchLabel.Style.Add(HtmlTextWriterStyle.TextAlign, "left") tdSearchRight.Controls.Add(wordSearchLabel) ' Search textbox mWordSearchTextBox = New TextBox mWordSearchTextBox.ID = "wordSearchTextBox" mWordSearchTextBox.Style.Add(HtmlTextWriterStyle.TextAlign, "left") tdSearchRight.Controls.Add(mWordSearchTextBox) mSearchButton = New Button mSearchButton.ID = "searchButton" mSearchButton.Text = "Search" mWordSearchTextBox.Style.Add(HtmlTextWriterStyle.TextAlign, "left") AddHandler mSearchButton.Click, AddressOf searchButton_Click tdSearchRight.Controls.Add(mSearchButton) Dim trResult As TableRow trResult = New TableRow table.Controls.Add(trResult) Dim tdResult As TableCell tdResult = New TableCell tdResult.ColumnSpan = 1 tdResult.Style.Add(HtmlTextWriterStyle.TextAlign, "center") tdResult.Style.Add(HtmlTextWriterStyle.Height, "26px") tdResult.VerticalAlign = VerticalAlign.Middle trResult.Controls.Add(tdResult) Dim searchResultLabel As New Label searchResultLabel.Text = "Search Result:" searchResultLabel.Style.Add(HtmlTextWriterStyle.TextAlign, "left") tdResult.Controls.Add(searchResultLabel) Dim GridPanel As Panel GridPanel = New Panel GridPanel.Height = 200 GridPanel.Width = 400 GridPanel.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#ccddff") tdResult.Controls.Add(GridPanel) mSearchGridView = New UI.WebControls.GridView mSearchGridView.AutoGenerateColumns = False mSearchGridView.AllowPaging = False mSearchGridView.EnableTheming = True mSearchGridView.EnableViewState = True mSearchGridView.SkinID = mSkinGridID GridPanel.Controls.Add(mSearchGridView) Dim trControl As TableRow trControl = New TableRow table.Controls.Add(trControl) Dim tdControl As TableCell tdControl = New TableCell tdControl.ColumnSpan = 1 tdControl.Style.Add(HtmlTextWriterStyle.TextAlign, "center") tdControl.Style.Add(HtmlTextWriterStyle.Height, "26px") tdControl.VerticalAlign = VerticalAlign.Middle trControl.Controls.Add(tdControl) mAddAccessButton = New Button mAddAccessButton.Attributes.Add("style", "float:right; margin-right: 15px;") mAddAccessButton.ID = "AddAccessButton" mAddAccessButton.Text = "Add" AddHandler mAddAccessButton.Click, AddressOf addButton_Click tdControl.Controls.Add(mAddAccessButton) mRemoveAccessButton = New Button mRemoveAccessButton.Attributes.Add("style", "float:right; margin-right: 15px;") mRemoveAccessButton.ID = "RemoveAccessButton" mRemoveAccessButton.Text = "Remove" AddHandler mRemoveAccessButton.Click, AddressOf removeButton_Click tdControl.Controls.Add(mRemoveAccessButton) End If End Sub ' Required for LoadPostData() event to fire. Private Sub Search_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init Page.RegisterRequiresPostBack(Me) End Sub Protected Sub searchDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 'no need to do anything here. This is just here for future expansion should it be needed. End Sub Protected Sub searchButton_Click(ByVal sender As Object, ByVal e As EventArgs) 'when the search button is clicked, populate the search grid. InitializeVariables() Dim doNotSearchIDs As New ArrayList Dim mAccessGridViewDT As New DataTable 'placeholder until we finalize this. If mAccessGridView IsNot Nothing AndAlso mAccessGridView.Rows.Count > 0 Then For x As Integer = 0 To mAccessGridView.Rows.Count 'GateUserTypeDT.Rows Dim accessRow As Control = mAccessGridView.Controls(0).Controls(x) Dim currentCheckBox As CheckBox = accessRow.FindControl("accessCheckbox") If currentCheckBox.Checked Then doNotSearchIDs.Add("ID") 'name with correct column name, find a way to extract data out of this row. End If Next End If Dim whereClause As String = Page.Session("WhereClause") Dim userID As String = Page.Session("userid") Dim searchData As New DataTable("SearchData") Dim orgNames As String() = Page.Session("OrgNames").Split("|") searchData = Nothing 'mBusinessTier.GetOrgSearch(mWordSearchTextBox.Text, mSearchDropDown.SelectedValue.ToString, doNotSearchIDs, userID, whereClause) searchData.Columns.Add(New DataColumn("Level")) searchData.Columns.Add(New DataColumn("IsChecked")) If searchData IsNot Nothing AndAlso searchData.Rows.Count > 0 Then For Each dr As DataRow In searchData.Rows dr("Level") = 1 'orgNames(Convert.ToInt32(dr("Lvl"))) dr("IsChecked") = False Next searchData.AcceptChanges() End If BindSearchGrid(searchData) mUpdatePanel.Update() End Sub Private Sub BindSearchGrid(ByVal searchData As DataTable) Dim orgPersonName As New System.Web.UI.WebControls.BoundField Dim level As New System.Web.UI.WebControls.BoundField Dim parent As New System.Web.UI.WebControls.BoundField Dim checkField As New TemplateField checkField.HeaderTemplate = New GridViewCheckboxTemplate(ListItemType.Header, "isChecked") checkField.ItemTemplate = New GridViewCheckboxTemplate(ListItemType.Item, "isChecked") orgPersonName.DataField = "Name" orgPersonName.HeaderText = "Org. / Person Name" level.DataField = "Level" level.HeaderText = "Level" parent.DataField = "DisplayName" parent.HeaderText = "Parent" With mSearchGridView 'add columns for checkbox, Person Name (name column), Level (level column), 'Parent (Parent Column but redo how it looks) mSearchGridView.Columns.Clear() .Columns.Add(checkField) '.Columns.Add(personName) .Columns.Add(level) .Columns.Add(parent) .DataSource = searchData .DataBind() mSearchHeaderCheckbox = mSearchGridView.HeaderRow.FindControl("gridCheckBox") If mSearchHeaderCheckbox Is Nothing Then mSearchHeaderCheckbox = New CheckBox End If mSearchHeaderCheckbox.AutoPostBack = True AddHandler mSearchHeaderCheckbox.CheckedChanged, AddressOf OnSearchCheckChanged .DataBind() .Visible = True .Enabled = True .EditIndex = -1 End With End Sub Private Sub BindAccessGrid(ByVal accessData As DataTable) Dim personName As New System.Web.UI.WebControls.BoundField Dim level As New System.Web.UI.WebControls.BoundField Dim parent As New System.Web.UI.WebControls.BoundField Dim checkField As New TemplateField checkField.ItemTemplate = New GridViewCheckboxTemplate(ListItemType.Item, "isChecked") personName.DataField = "Name" personName.HeaderText = "Person Name" level.DataField = "Level" level.HeaderText = "Level" parent.DataField = "DisplayName" parent.HeaderText = "Parent" With mAccessGridView 'add columns for checkbox, Org. / Person Name (name column), Level (level column), If Not .Columns.Count > 0 Then .Columns.Add(checkField) 'once the check field is added to the grid, find the grid, go to the header and add an event. .Columns.Add(personName) .Columns.Add(level) .Columns.Add(parent) End If .DataSource = accessData .DataBind() mAccessHeaderCheckbox = mAccessGridView.HeaderRow.FindControl("gridCheckBox") Page.Session("SearchCheckBoxSelected") = False AddHandler mAccessHeaderCheckbox.CheckedChanged, AddressOf OnSearchAccessCheckChanged .Visible = True .Enabled = True End With End Sub ''' <summary> ''' This function is fired when the header checkbox is changed it's checking. It's used to select everything in the Search grid. ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub OnSearchCheckChanged(ByVal sender As Object, ByVal e As EventArgs) If mSearchHeaderCheckbox IsNot Nothing Then For Each dr As GridViewRow In mSearchGridView.Rows Dim checkbox As CheckBox = dr.FindControl("gridCheckBox") If checkbox IsNot Nothing Then checkbox.Checked = mSearchHeaderCheckbox.Checked End If Next End If End Sub ''' <summary> ''' This function is fired when the header checkbox is changed it's checking. It's used to select everything in the Access grid. ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub OnSearchAccessCheckChanged(ByVal sender As Object, ByVal e As EventArgs) mAccessHeaderCheckbox = mAccessGridView.HeaderRow.FindControl("gridCheckBox") If mAccessHeaderCheckbox IsNot Nothing Then For Each dr As GridViewRow In mAccessGridView.Rows Dim checkbox As CheckBox = dr.FindControl("gridCheckBox") checkbox.Checked = mAccessHeaderCheckbox.Checked Next End If End Sub Protected Sub removeButton_Click(ByVal sender As Object, ByVal e As EventArgs) 'when the remove button is clicked, remove the selected IDS from the AccessGrid and add re-search. End Sub Protected Sub addButton_Click(ByVal sender As Object, ByVal e As EventArgs) 'add the selected ids to the access grid, but remove them from the search grid. Aslo change the 'where clause to remove them from the search (wehere ID IN (For each item in gridview.rows.finditem("columnname") or something like that) Dim changed As Boolean = False Dim whereClause As New StringBuilder For Each row As GridViewRow In mSearchGridView.Rows Dim check As CheckBox = row.FindControl("searchCheck") If check IsNot Nothing AndAlso check.Checked Then 'remove gridview row and add it to other box 'convert gridview row to regular data row and add it to access datatable AccessDT.Rows.Add(row) whereClause.Append("") changed = True End If Next If changed Then 'rebind both grids. Add restricted IDs to search! End If End Sub #End Region #Region "Properties" <Bindable(True), Category("Layout"), DefaultValue("cmd")> _ Public Property CssClassButton() As String Get Return mCssClassButton End Get Set(ByVal value As String) mCssClassButton = value End Set End Property <Bindable(True), Category("Layout"), DefaultValue("")> _ Public Property CssClassTextBox() As String Get Return mCssClassTextBox End Get Set(ByVal value As String) mCssClassTextBox = value End Set End Property <Bindable(True), Category("Layout"), DefaultValue("grid")> _ Public Property CSSClassGrid() As String Get Return mCSSClassGrid End Get Set(ByVal value As String) mCSSClassGrid = value End Set End Property Public Property GridSkinID() As String Get Return mSkinGridID End Get Set(ByVal value As String) mSkinGridID = value End Set End Property Public Property isDirectory() As Boolean Get Return mIsDirectory End Get Set(ByVal Value As Boolean) mIsDirectory = Value End Set End Property Public Property accessDT() As DataTable Get Return mAccessData End Get Set(ByVal value As DataTable) mAccessData = value End Set End Property Public Sub New() mIsDirectory = False mTitle = "" mPathToArray = "" chkBox = "" misCode = False End Sub Public Property Title() As String Get Return mTitle End Get Set(ByVal value As String) mTitle = value End Set End Property #End Region End Class Public Class GridViewCheckboxTemplate Implements ITemplate Implements INamingContainer Private ThisId As String = "" Dim mType As ListItemType Dim mColumnName As String Public Sub GridCiewCheckboxTemplate() End Sub Public Sub New(ByVal type As ListItemType, ByVal colName As String) mType = type mColumnName = colName End Sub Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn Dim check As New CheckBox check.ID = "gridCheckBox" If mType = ListItemType.Item Then AddHandler check.DataBinding, AddressOf BindData End If container.Controls.Add(check) End Sub Public Sub BindData(ByVal sender As Object, ByVal e As EventArgs) Dim check As CheckBox = CType(sender, CheckBox) Dim container As GridViewRow = CType(check.NamingContainer, GridViewRow) Dim dataValue As Object = DataBinder.Eval(container.DataItem, mColumnName) If dataValue IsNot Nothing Then check.Checked = Convert.ToBoolean(dataValue) End If End Sub End Class
Thursday, April 10, 2008 6:54 PM -
User563335125 posted
This is how I want it to look, except I want the grid under the search textbox line. What happens is that when the person selects the items in the first grid and then hits add, it will remove them from the first grid and then add them to the second grid, granting the user permissions.
I need the check/uncheck all to work so that the person can give the user all the permissions at once.
Does this help?
Friday, April 11, 2008 12:10 PM -
User-16411453 posted
What about the add remove buttons, under the grid?. The last code I posted is pretty close to it.
Friday, April 11, 2008 12:50 PM -
User563335125 posted
I hadn't completed wiring them up yet, but the add button will take the checked boxes from the first grid and throw them into the second grid.
The Remove button will be under the second grid (the one that stands for the ones that the person already has access to), and when clicked will remove the clicked values from the bottom grid and put them back into the top grid IF they match the search parameters.
I'm going to now work on getting the code you posted working with my project, I'll let you know how it goes when it's all done. Thanks again for all the work!
*Update* I have implemented your code, and the checkchanged event never fires when the grid header checkbox was checked.
The work looks great, but still, the event for that darn header checkbox just isn't working. Any ideas?
Friday, April 11, 2008 1:23 PM -
User888441741 posted
uofirob:
your code looks very interesting to me.
can you post all your working code?
thanks
Friday, April 11, 2008 4:11 PM -
User563335125 posted
This is stuff for work, so unfortunately, I can't. It's also several thousand lines long! It doesn't matter if this is in the tab control like I posted or if it's in a blank page. I just need the control to be able to to take the input of the accessTable, and populate the lower "access granted" grid (it isn't working yet because I can't get the check event to fire off the top grid). When the search button is clicked, it'll go to the database, grab the available grantable accesses (ones that haven't already been granted), and allow the user to select as many as they want, hit add and move those accesses into the lower grid. I also need to be able to access the access table so I can take the values out of the control and save them to the database later.
Right now the major problems I have is that when any event fires (other than the search button click), the values in the search grid stay, but all the checkboxes vanish.
The only thing the above code is missing in order to make it to work is the datatable pulled back from my business tier in order to populate the top grid. To make that, create a data table that contains columns: "Name" "Level" "DisplayName" "Parent" and "Checked" and populate the datatable with values. The first columns are all strings, and the last column is boolean.
I suspect because the databind is kinda wonky, the grid isn't saving the checkboxes, and when an event fires, it makes all of the checkboxes = nothing, which causes them do disappear. Does anyone know how to combat this AND make the checkbox's checkchanged event fire? I think the event is not firing because the checkbox in the header no longer exists on postback.
Can anyone get that checkchanged event to fire and not have all the checkboxes go poof?
Friday, April 11, 2008 4:24 PM -
User563335125 posted
Has anyone found a solution to this?
Monday, April 14, 2008 10:36 AM -
User563335125 posted
Has anyone gotten any further? A coworker has generated a solution that uses javascript, I'll try to integrate it, but I'd still like a clean VB.Net+Asp.net+Ajax solution that doesn't involve including a .js file.
Tuesday, April 15, 2008 9:57 AM