I have been asked to add a new class (vb) to our dll of custom webcontrols on the college website. We have no access to the actual website code due to the CMS (Contensis)
I'm having some difficulty with a delete button. If I set oGridview.AutoGenerateDeleteButton=true then everything is fine, but I want to be able to use a small image instead. Now if I had access to the asp I would ofc simply add a template field with an
image button, but I can't work out how to replicate this in my vb class.
My current gridview setup:
Public oGridview As New GridView
Dim tf As New TemplateField
tf.ItemTemplate = New GridViewCourseTemplate("CourseCode", "String", DataControlRowType.DataRow)
oGridview.Columns.Add(tf)
oGridview.RowStyle.Wrap = True
oGridview.DataSource = dsDisplay.Tables(0)
oGridview.DataBind()
oGridview.Visible = True
I'm not that familiar with creating custom itemtemplates & I inherted this one from when the .dll was created some years back
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text.RegularExpressions
Public Class GridViewCourseTemplate
Implements System.Web.UI.ITemplate
Private templateType As DataControlRowType
Private columnName As String
Private dataType1 As String
Sub New(ByRef colname As String, ByRef DataType As String, ByRef type As DataControlRowType)
templateType = type
columnName = colname
dataType1 = DataType
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements System.Web.UI.ITemplate.InstantiateIn
Dim HC As DataControlFieldCell
HC = Nothing
Select Case (templateType)
Case DataControlRowType.DataRow
Dim l As New ExtendedLiteralControl
AddHandler l.DataBinding, New EventHandler(AddressOf Item_DataBinding)
container.Controls.Add(l)
End Select
End Sub
Shared Sub Item_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim l As ExtendedLiteralControl
l = CType(sender, LiteralControl)
Dim row As GridViewRow
row = CType(l.NamingContainer, GridViewRow)
Dim RawValue As String
RawValue = DataBinder.Eval(row.DataItem, "CourseCode").ToString + " : " + DataBinder.Eval(row.DataItem, "CourseTitle"
l.Text = RawValue
End Sub
End Class
I did try creating an itemtemplate class that adds an imagebutton instead of raw text, but the delete functionality is missing
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text.RegularExpressions
Public Class GridViewDeleteButtonTemplate
Implements System.Web.UI.ITemplate
Sub New()
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim deleteButton As New ImageButton
deleteButton.ImageUrl = "/Images/CourseDetailsButtons/btnDelete.jpg"
deleteButton.CommandName = "delete"
container.Controls.Add(deleteButton)
End Sub
End Class
I'm missing something obvious - what is it please?
My google-fu has found me this improved class for the button event handler, but the event doesn't happen:
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class GridViewDeleteButtonTemplate
Inherits System.Web.UI.Control
Implements System.Web.UI.ITemplate
Private btnDel As ImageButton
Private m_CommandName As String
Public Property CommandName() As String
Get
Return m_CommandName
End Get
Set(ByVal value As String)
m_CommandName = value
End Set
End Property
Public Sub New()
btnDel = New ImageButton
btnDel.ID = "delButton"
btnDel.ImageUrl = "Images/CourseDetailsButtons/btnDelete.jpg"
btnDel.ToolTip = "Delete from basket"
CommandName = "Delete"
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
AddHandler btnDel.Click, New ImageClickEventHandler(AddressOf btn_Click)
container.Controls.Add(btnDel)
End Sub
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim commandEventArgs As New CommandEventArgs(Me.CommandName, btnDel)
RaiseBubbleEvent(btnDel, commandEventArgs)
End Sub
End Class
I mean that the page appears to register that there should be an event, since the mouse turns to a hand when hovering over the image, but that on clicking nothing happens. The event is not firing. Any OnClientClick javascript that i set up works just fine
& test bits I put into the btn_Click event handler do not write to screen, so the event is not firing.
Additionally I have found that
- the imagebutton is only displayed on the last row of the datagrid.
- the autogenerated delete button doesn't fire either
Please try to remove the codes in the constructor——
Public Sub New()
btnDel = New ImageButton
btnDel.ID = "delButton"
btnDel.ImageUrl = "Images/CourseDetailsButtons/btnDelete.jpg"
btnDel.ToolTip = "Delete from basket"
AddHandler btnDel.Click, New ImageClickEventHandler(AddressOf btn_Click)
CommandName = "Delete"
End Sub
The issue is that the custom button event is not being recognised by the gridview despite being bubbled up. Whilst i agree the code was in the wrong place (thankyou) the event handler is still not working as intended. At this point I have the following in
the basket.vb which is where my gridview is populated
Private Sub oGridview_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
errorLabel.Text = "event fired"
End Sub
and the event handler never fires from my custom button although at least the autogeneratebuttons do
frugglewump
0 Points
7 Posts
Programmatically creating delete button for gridview in a dll
Jul 27, 2012 11:53 AM|LINK
Hi,
I have been asked to add a new class (vb) to our dll of custom webcontrols on the college website. We have no access to the actual website code due to the CMS (Contensis)
I'm having some difficulty with a delete button. If I set oGridview.AutoGenerateDeleteButton=true then everything is fine, but I want to be able to use a small image instead. Now if I had access to the asp I would ofc simply add a template field with an image button, but I can't work out how to replicate this in my vb class.
My current gridview setup:
Public oGridview As New GridView Dim tf As New TemplateField tf.ItemTemplate = New GridViewCourseTemplate("CourseCode", "String", DataControlRowType.DataRow) oGridview.Columns.Add(tf) oGridview.RowStyle.Wrap = True oGridview.DataSource = dsDisplay.Tables(0) oGridview.DataBind() oGridview.Visible = TrueI'm not that familiar with creating custom itemtemplates & I inherted this one from when the .dll was created some years back
Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Text.RegularExpressions Public Class GridViewCourseTemplate Implements System.Web.UI.ITemplate Private templateType As DataControlRowType Private columnName As String Private dataType1 As String Sub New(ByRef colname As String, ByRef DataType As String, ByRef type As DataControlRowType) templateType = type columnName = colname dataType1 = DataType End Sub Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _ Implements System.Web.UI.ITemplate.InstantiateIn Dim HC As DataControlFieldCell HC = Nothing Select Case (templateType) Case DataControlRowType.DataRow Dim l As New ExtendedLiteralControl AddHandler l.DataBinding, New EventHandler(AddressOf Item_DataBinding) container.Controls.Add(l) End Select End Sub Shared Sub Item_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Dim l As ExtendedLiteralControl l = CType(sender, LiteralControl) Dim row As GridViewRow row = CType(l.NamingContainer, GridViewRow) Dim RawValue As String RawValue = DataBinder.Eval(row.DataItem, "CourseCode").ToString + " : " + DataBinder.Eval(row.DataItem, "CourseTitle" l.Text = RawValue End Sub End ClassI did try creating an itemtemplate class that adds an imagebutton instead of raw text, but the delete functionality is missing
Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Text.RegularExpressions Public Class GridViewDeleteButtonTemplate Implements System.Web.UI.ITemplate Sub New() End Sub Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn Dim deleteButton As New ImageButton deleteButton.ImageUrl = "/Images/CourseDetailsButtons/btnDelete.jpg" deleteButton.CommandName = "delete" container.Controls.Add(deleteButton) End Sub End ClassI'm missing something obvious - what is it please?
Keyur Shah
Participant
1002 Points
278 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 27, 2012 12:03 PM|LINK
Hello there, here is my created grid which contains image as delete, edit, cancel so go throug this. May be it'll help you.
This is .aspx code <asp:GridView ID="grid_contact" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="cid" OnRowEditing="EditRecord" OnRowUpdating="UpdateRecord" OnRowDeleting="DeleteRecord" OnRowCancelingEdit="CancelRecord" EmptyDataText="No Record Found..!!" CellPadding="4" Font-Names="Verdana" ForeColor="#333333" GridLines="None"> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:ImageButton ID="btEditarGrid" ImageUrl="~/Edit-1.png" CommandName="Edit" Width="25" runat="server" ToolTip="Edit" AlternateText="Edit"/> <asp:ImageButton ID="Imagedelete" ImageUrl="~/cancel.png" CommandName="Delete" Width="25" runat="server" ToolTip="Delete" AlternateText="Delete" OnClientClick="return confirm('Are you sure you want to delete this user?');"/> </ItemTemplate> <EditItemTemplate> <asp:ImageButton ID="btGuardarGrid" ImageUrl="~/updating.png" CommandName="Update" width="25" runat="server" ToolTip="Update" AlternateText="Update"/> <asp:ImageButton ID="Imagecancel" ImageUrl="~/delete.png" CommandName="Cancel" width="25" runat="server" ToolTip="Cancel" AlternateText="Cancel"/> </EditItemTemplate> </asp:TemplateField> <%--<asp:TemplateField> <ItemTemplate> <asp:ImageButton id="edit" runat="server" ImageUrl="D:\Keyur\3_tire\edit.jpg" Width="25" Height="25" CommandName="Edit"/> </ItemTemplate> </asp:TemplateField>--%> <asp:BoundField DataField="cid" HeaderText="Contact ID" ReadOnly="True" /> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <%# Eval("name") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtname" runat="Server" Text='<%# Eval("name") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <ItemTemplate> <%# Eval("address") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtadd" runat="Server" Text='<%# Eval("address") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <ItemTemplate> <%# Eval("city") %> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlcity" runat="server"> <asp:ListItem>Ahmedabad</asp:ListItem> <asp:ListItem>Anand</asp:ListItem> <asp:ListItem>Borsad</asp:ListItem> <asp:ListItem>Bhavnagar</asp:ListItem> <asp:ListItem>Surat</asp:ListItem> <asp:ListItem>Surendranagar</asp:ListItem> <asp:ListItem>Bharuch</asp:ListItem> <asp:ListItem>Mumbai</asp:ListItem> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Telephone"> <ItemTemplate> <%# Eval("tel") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txttel" runat="Server" Text='<%# Eval("tel") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Mobile"> <ItemTemplate> <%# Eval("mob") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtmob" runat="Server" Text='<%# Eval("mob") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>This is .cs code protected void DeleteRecord(object sender, GridViewDeleteEventArgs e) { int cid = Int32.Parse(grid_contact.DataKeys[e.RowIndex].Value.ToString()); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "DeleteRecord"; cmd.Parameters.Add("@cid",cid); cmd.Connection = con; cmd.ExecuteNonQuery(); lblmsg.Text = "Record has been Deleted Successfully..!!"; cmd.Dispose(); con.Close(); grid_contact.EditIndex = -1; fillgrid(); }frugglewump
0 Points
7 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 27, 2012 12:11 PM|LINK
No, but thankyou. Putting an imagebutton in to replace the delete button would have been no trouble at all if I was allowed to use .aspx
I can't use .aspx code anywhere because of the restrictions of our CMS. Everything has to be done in the VB webcontrols in the .dll
frugglewump
0 Points
7 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 27, 2012 01:05 PM|LINK
My google-fu has found me this improved class for the button event handler, but the event doesn't happen:
Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Public Class GridViewDeleteButtonTemplate Inherits System.Web.UI.Control Implements System.Web.UI.ITemplate Private btnDel As ImageButton Private m_CommandName As String Public Property CommandName() As String Get Return m_CommandName End Get Set(ByVal value As String) m_CommandName = value End Set End Property Public Sub New() btnDel = New ImageButton btnDel.ID = "delButton" btnDel.ImageUrl = "Images/CourseDetailsButtons/btnDelete.jpg" btnDel.ToolTip = "Delete from basket" CommandName = "Delete" End Sub Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn AddHandler btnDel.Click, New ImageClickEventHandler(AddressOf btn_Click) container.Controls.Add(btnDel) End Sub Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim commandEventArgs As New CommandEventArgs(Me.CommandName, btnDel) RaiseBubbleEvent(btnDel, commandEventArgs) End Sub End ClassKeyur Shah
Participant
1002 Points
278 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 27, 2012 01:25 PM|LINK
Means? Page is not getting postBack?
Try
btnObj.PostBack = True;
frugglewump
0 Points
7 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 27, 2012 02:03 PM|LINK
I mean that the page appears to register that there should be an event, since the mouse turns to a hand when hovering over the image, but that on clicking nothing happens. The event is not firing. Any OnClientClick javascript that i set up works just fine & test bits I put into the btn_Click event handler do not write to screen, so the event is not firing.
Additionally I have found that
- the imagebutton is only displayed on the last row of the datagrid.
- the autogenerated delete button doesn't fire either
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 29, 2012 02:58 AM|LINK
Hi,
Please try to remove the codes in the constructor——
Public Sub New() btnDel = New ImageButton btnDel.ID = "delButton" btnDel.ImageUrl = "Images/CourseDetailsButtons/btnDelete.jpg" btnDel.ToolTip = "Delete from basket" AddHandler btnDel.Click, New ImageClickEventHandler(AddressOf btn_Click) CommandName = "Delete" End Subfrugglewump
0 Points
7 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 31, 2012 07:54 AM|LINK
Ok well I moved them from the constructor to the instantiateIn sub.
Didn't help in the slightest
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 31, 2012 08:14 AM|LINK
?
frugglewump
0 Points
7 Posts
Re: Programmatically creating delete button for gridview in a dll
Jul 31, 2012 08:47 AM|LINK
The issue is that the custom button event is not being recognised by the gridview despite being bubbled up. Whilst i agree the code was in the wrong place (thankyou) the event handler is still not working as intended. At this point I have the following in the basket.vb which is where my gridview is populated
Private Sub oGridview_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) errorLabel.Text = "event fired" End Suband the event handler never fires from my custom button although at least the autogeneratebuttons do