Push Button Event in datagrid

J

janetb

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. 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>
 
K

Kelly Leahy

OK... I thought I replied to this, but I guess it didn't make it.

First, I'd replace e.item.cells(0).text with e.CommandArgument.

Second, I'd ask, does it appear that the event is not occurring, or that the
pageID argument on the query string is not being populated correctlly. If
the former, I'd check to make sure you aren't doing a DataBind() when the
page is posted back in your Page_Load. If it's the latter, I'd try changing
to using the CommandArgument as I recommended above and see if that fixes the
problem. If not, I'm not sure what to look at next. I would note that in
the code you pasted, the url= line seems to be commented out. Also, I don't
see any code to actually do the redirect.

Cheers,
Kelly
 
J

janetb

It appears that the event is not happening. I commented out everything and
just had a label.text="deleting...." and nothing happened.

I know the redirect stuff isn't working yet - it's commented out. I'm just
taking it one step at a time.
 
K

Kelly Leahy

Janet,

did you check that you aren't calling DataBind every time the page is
loaded? Also, have you tried setting a breakpoint on the event handler and
seeing if it gets hit in the IDE when debugging the web app?

Can you post your "Page_Load" code for the page?

Cheers,
Kelly
 
J

janetb

Kelly - here's the whole thing - and thanks so much for hanging in there.
The first two buttonColumns work, except the delete isn't reflected in the
datagrid, but is in fact gone from the sql db. (How could I fix that?) But,
I don't understand why the commands aren't firing on the imagebuttons. Do I
have to change the sub arguements or something?

Public strConn As String = "blah, blah, blah;"
Public cn As New SqlClient.SqlConnection(strConn)
Public cmd as sqlclient.sqlCommand
Public ds as New Dataset
Public da as New SQLDataAdapter
Public varID as integer
Sub dgCourseListChg(Sender As Object, E As DataGridCommandEventArgs)
lblMsg.text = "<b>Record Updated</b><br>"
End sub
Sub dgCourseListDel(Sender As Object, E As DataGridCommandEventArgs)
Dim DeleteCmd As String = "DELETE from ceClasses.dbo.tblEcourse where
pageID_pk = @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()
lblMsg.text = "<b>Record Deleted</b>"
Catch Exc As SQLException
lblMsg.text = "ERROR: Could not delete record"
lblMsg.Style("color") = "red"
End Try
cmd.Connection.Close()
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
else
BindGrid()
pageAdd.visible=True
End If
end sub
</script>
<html><body>
<form id="Form1" method="post" runat="server">
<p>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)
<P><A class=menugray href="adminEcourseAdd.aspx?eventID=<%= varID %>">Add a
Page</A></P>
<asp:DataGrid id="dgCourseList" DataKeyField="pageID_pk"
OnDeleteCommand="dgCourseListDel" onEditCommand="dgcourseListChg"
autogenerateColumns="False" runat="server">
<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>
 
J

janetb

Kelly,
added:
( ds.Tables("pages").Rows(CInt(e.Item.ItemIndex)).Delete
dgCourseList.EditItemIndex = -1
dgCourseList.DataSource=ds.Tables("pages").DefaultView
dgCourseList.DataBind()
to Sub dgCourseListDel
and the datagrid is reflecting correctly now with the buttonColumn delete.....

just wish I could use those imageButtons.....
 
K

Kelly Leahy

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

This code really bothers me. First of all, the BindGrid should only be in
the "not ispostback" branch of the IF. Second, I don't see how this code
compiles, since there's no THEN in the IF line.

Basically the pattern should be this:

1) DataBind should only be called in Page_Load when IsPostBack is not true.
2) DataBind should be called anytime a change is made to the source data,
but only after all important events have been handled. If you need to ensure
that more than one event must be handled on a postback, you'll need to set a
flag in your event handlers and call DataBind in OnPreRender of the page. I
could be wrong on this, maybe it's ok to do it in the event handlers, I've
never needed to handle more than one event in a postback, so I've never run
into a problem.
3) Event handlers that change the data in the DB, should do so, then they
should reload and rebind the data to the controls.

Cheers,
Kelly
 
J

janetb

Kelly,

I need to bind the data AFTER the eventID.text has had an entry, not the
first time the page loads; and, all events are firing correctly and as I wish
them to in the two buttonColumns. So, I guess I'm really wondering why the
imageButtons don't work as the documentation indicates?

Janet
 
K

Kelly Leahy

Sorry,

I don't know, but my opinion is that the buttonColumns aren't working as the
documentation indicates! Everything I've ever read says that you can't bind
the grid on postback, except in response to an event (not Page_Load). Beyond
that, I don't think I can help you - sorry.

Kelly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top