PDA

View Full Version : ImageButton in DataGrid


janetb
05-02-2006, 04:03 PM
Learning as I go and having trouble with a datagrid. Using images, and need to delete a selected record if the image is pushed or send to another page if it's edit. No errors, but don't think things are getting passed or the sub is even being called. I commented all the code in the sub and just set a label.text to "deleting" and nothing happens. Trying to plagarize from a help article on the web and haven't got something just right. Any help, please?

Private Sub dgCourseListItemCommand(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
if e.CommandName="Edit" then
'do a redirect with e.item.cells(0).text as url parameter
'url=http://www.servername.com/adminEcoursePageEdit.aspx?pageID= &
e.item.cells(0).text
End if

if e.CommandName="Delete" then
Dim cmdp as new sqlClient.SqlCommand("sp_eCoursePageDel",cn)
cmdp.CommandType = CommandType.StoredProcedure
Dim p_pg as SQLParameter = new SQLParameter("@pageID", SqlDbType.Int, 4)
p_pg.Value = e.item.cells(0).text
cmdp.Parameters.Add(p_pg)
cmdp.ExecuteNonQuery()
lblMsg.text=e.commandName & ", " & e.item.cells(0).text
End if
End Sub

<asp:DataGrid id="dgCourseList" OnItemCommand="dgCourseListItemCommand"
runat="server" autogenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="pageID_pk" Visible="False"></asp:BoundColumn>
<asp:boundcolumn HeaderText="Navigation Title" DataField="navTitle"
Visible="True"></asp:boundcolumn>
<ASP:TemplateColumn HeaderText="Action">
<ItemTemplate>
<ASP:ImageButton id="btnDel" Runat="server" AlternateText="Delete"
ImageUrl="../images/boxX.gif" CommandArgument='<%#
DataBinder.Eval(Container,"DataItem.pageID_pk")%>' CommandName="Delete" />
<ASP:ImageButton id="btnEdit" Runat="server" AlternateText="Edit"
ImageUrl="../images/boxCheck.gif" CommandArgument='<%#
DataBinder.Eval(Container,"DataItem.pageID_pk")%>' CommandName="Edit" />
</ItemTemplate>
</ASP:TemplateColumn>
</Columns>
</asp:DataGrid>

handshakeit
05-03-2006, 12:31 PM
Try this
set DataKeyNames property of datagrid
DataKeyNames= pageID_pk
and use OnRowDeleting event instead of OnItemCommand
and there
p_pg.Value =(int) GridView1.DataKeys[e.RowIndex].Value;

hop it wil work

handshakeit
05-03-2006, 12:33 PM
for additional help go through the following link
http://www.codeproject.com/aspnet/GridViewConfirmDelete.asp?df=100&forumid=254626&exp=0&select=1451805#xx1451805xx

janetb
05-04-2006, 11:36 PM
I got this code to work using buttoncolumns, but would prefer to use the imageButtons (they don't seem to fire at all in the onClick). Any ideas?

<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Data" %>
<%@Page Language="VB" debug="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<script language="vb" runat="server">
Public strConn As String = "connection stuff ;"
Public cn As New SqlClient.SqlConnection(strConn)
Public cmd as sqlclient.sqlCommand
Public cmd2 as sqlclient.sqlCommand
Public ds as New Dataset
Public da as New SQLDataAdapter
Public varID as integer

Sub dgCourseListDel(Sender As Object, E As DataGridCommandEventArgs)
Dim DeleteCmd As String = "DELETE from tblEcourse where pageID_pk = @Id " & _
" DELETE from tblEcourseDownloads where pageID_fk = @Id"
cmd = New sqlClient.SqlCommand(DeleteCmd, cn)
cmd.Parameters.Add(New SqlParameter("@ID", SqlDbType.NVarChar, 11))
cmd.Parameters("@Id").Value = dgCourseList.DataKeys(CInt(E.Item.ItemIndex))
cmd.Connection.Open()
Try
cmd.ExecuteNonQuery()
Catch Exc As SQLException
lblMsg.text = "ERROR: Could not delete record"
lblMsg.Style("color") = "red"
End Try
cmd.Connection.Close()
ds.Tables("pages").Rows(CInt(e.Item.ItemIndex)).Delete
dgCourseList.EditItemIndex = -1
dgCourseList.DataSource=ds.Tables("pages").DefaultView
dgCourseList.DataBind()
End Sub
Sub BindGrid()
varID=cint(eventID.text)
da = New SqlDataAdapter("sp_eCoursePage", cn)
da.SelectCommand.CommandType = CommandType.StoredProcedure
Dim p_ID as New SqlParameter("@ID",varID)
da.SelectCommand.Parameters.Add(p_ID)
da.Fill(DS, "pages")
dgCourseList.DataSource=DS.Tables("pages").DefaultView
If ds.Tables("pages").Rows.Count = 0 Then
dgCourseList.visible=False
else
dgCourseList.visible=True
end if
dgCourseList.DataBind()
End Sub

Sub Page_load(sender as Object, e as EventArgs)
If Not (IsPostBack)
pageAdd.visible=False
pageEdit.visible=False
else
BindGrid()
pageAdd.visible=True
pageEdit.visible=False
End If
end sub

</script>

<form id="Form1" method="post" runat="server">
<p>Casce Event ID:
<asp:textbox id="eventID" runat="server" columns="5"></asp:textbox><br />
<asp:label id="lblMsg" runat="server" /><br />
<span class="small">(enter the id and press enter)
<asp:placeholder id="pageAdd" runat="server">
<P><A class=menugray href="adminEcourseAdd.aspx?eventID=<%= varID %>">Add a Page</A></P>
</asp:placeholder>
<p><asp:DataGrid id="dgCourseList" DataKeyField="pageID_pk" OnDeleteCommand="dgCourseListDel" onEditCommand="dgCourseListChg"
autogenerateColumns="False" runat="server" BorderColor="Black" Font-Names="Arial" Font-Size="9" CellPadding="5">
<SelectedItemStyle BackColor="Gold"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="pageID_pk" Visible="False"></asp:BoundColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete" />
<asp:ButtonColumn Text="Edit" CommandName="Edit" />
<asp:boundcolumn HeaderText="Page Order" DataField="pgOrder" Visible="True"></asp:boundcolumn>
<asp:boundcolumn HeaderText="Navigation Title" DataField="navTitle" Visible="True"></asp:boundcolumn>
<ASP:TemplateColumn HeaderText="Action"><ItemTemplate>
<ASP:ImageButton id="btnDel" Runat="server" AlternateText="Delete" ImageUrl="../images/boxX.gif" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.pageID_pk")%>' CommandName="Delete" />
<ASP:ImageButton id="btnEdit" Runat="server" AlternateText="Edit" ImageUrl="../images/boxCheck.gif" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.pageID_pk")%>' CommandName="Edit" />
</ItemTemplate></ASP:TemplateColumn>
</Columns>
</asp:DataGrid>

handshakeit
05-05-2006, 09:39 AM
I think there is no need of any event of imagebutton
Hav not u go thrugh the link I gav in previous reply?

handshakeit
05-05-2006, 09:50 AM
What about the code u supplied
u used image button in that are it is not working?????????