Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2502
  • Last Modified:

VB.NET - Edit button not firing on gridview

I have a page with a detailsView that allows the user to add a project.  Once the project is added, there are several gridViews that contain information related to the ProjectID that is created in the detailsView.  The one I'm having trouble with is the resources gridView.  The user can go into the page, create a project, and then should be able to create resource records based on the ProjectID.  I've got the Add and Delete functionality working fine but when I click the 'Edit' button on the gridView nothing happens (the edit templates don't appear).  I did this on another page and it works fine but for some reason it won't gel here.  I'm pretty sure it has something to do with databinding but I'm stumped.  Here's my code:

DetailsView:

                    <asp:DetailsView
                        ID="dvProjectDetails"
                        DataSourceID="dsProjectDetails"
                        DataKeyNames="ProjectID"
                        Width="100%"
                        AutoGenerateRows="False"
                        runat="server">
               
                        <Fields>
                            <asp:TemplateField HeaderText="Project ID" HeaderStyle-Width="110px" HeaderStyle-CssClass="bold_8" ItemStyle-HorizontalAlign="Left">
                                <ItemTemplate>
                                    <asp:Label ID="lblProjectID" Text='<%# Eval("ProjectID") %>' runat="server" />&nbsp;
                                </ItemTemplate>
 
                            </asp:TemplateField>
                       
                            <asp:TemplateField HeaderText="Project Name" HeaderStyle-Width="110px" HeaderStyle-CssClass="bold_8" ItemStyle-HorizontalAlign="Left">
                                <ItemTemplate>
                                    <asp:Label ID="lblProjectName" Text='<%# Eval("ProjectName") %>' runat="server" />&nbsp;
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtProjectName" Text='<%# Bind("ProjectName") %>' Width="250px" runat="server" />
                                </EditItemTemplate>
                            </asp:TemplateField>
</asp:DetailsView>

Its datasource:

<asp:SqlDataSource
    ID="dsProjectDetails"
    ConnectionString="<%$ ConnectionStrings:BOS %>"
    SelectCommand="up_ProjectDetailsSelect"
    SelectCommandType="storedprocedure"
    UpdateCommand="up_ProjectUpdate"
    UpdateCommandType="StoredProcedure"
    runat="server">
    <SelectParameters>
        <asp:Parameter Name="ProjectID" />
    </SelectParameters>
   
    <InsertParameters>
        <asp:Parameter Name="ProjectID" Type="Int32" />
   </InsertParameters>

The addResource code:

    Protected Sub btnAddResource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddResource.Click
        Dim sFullName, sLname, sFname, sNBID As String
        Dim iFullNameLen, iCommaPos, iDashPos, iResourceID As Integer

        sFullName = txtResourceSearch.Text
        iFullNameLen = Len(sFullName)

        iCommaPos = InStr(sFullName, ",")

        If iCommaPos > 0 Then
            iDashPos = InStr(sFullName, "-")

            If iDashPos > 0 Then
                sLname = Trim(Left(sFullName, (iCommaPos - 1)))
                sFname = Trim(Mid(sFullName, (iCommaPos + 1), (iDashPos - 1) - iCommaPos))
                sNBID = Trim(Right(sFullName, iFullNameLen - iDashPos))

                Dim oConn As New SqlConnection(DataProvider.GetConnectionString)
                Dim oCmd As New SqlCommand
                oConn.Open()

                With oCmd
                    .Connection = oConn
                    .CommandType = CommandType.StoredProcedure

                    .CommandText = ("up_ResourceIDSelect")
                    .Parameters.Add("@NBID", SqlDbType.VarChar).Value = sNBID
                    iResourceID = .ExecuteScalar()

                    .Parameters.Clear()

                    .CommandText = ("up_ProjectResourceAssignmentInsert")
                    .Parameters.Add("@ResourceID", SqlDbType.SmallInt).Value = iResourceID
                    .Parameters.Add("@ProjectID", SqlDbType.SmallInt).Value = dvProjectDetails.SelectedValue
                    .Parameters.Add("@ProjectRoleID", SqlDbType.VarChar).Value = ddlRoleAdd.SelectedValue
                    .ExecuteNonQuery()
                End With

                oConn.Close()
                dsResources.SelectParameters(0).DefaultValue = dvProjectDetails.SelectedValue
                gvResources.DataBind()
                txtResourceSearch.Text = ""
            End If
        End If
    End Sub

Thanks in advance,

N
0
stretch73
Asked:
stretch73
  • 5
  • 4
1 Solution
 
Bob LearnedCommented:
I don't see where an Edit button is defined, so are you using automatically generated Edit buttons for each row in the GridView?

Bob
0
 
stretch73Author Commented:
Morning Bob,

Duh, here's the Resources gridview:

                        <asp:GridView
                            ID="gvResources"
                            DataSourceID="dsResources"
                            AutoGenerateColumns="False"
                            AllowPaging="true"
                            AllowSorting="true"
                            PageSize="10"
                            Width="100%"
                            runat="server">
                            <Columns>

                                <asp:TemplateField ItemStyle-HorizontalAlign="center">
                                    <ItemTemplate>
                                        <asp:Button
                                            ID="btnResourceEdit"
                                            Text="Edit"
                                            CommandName="Edit"
                                            EnableTheming="false"
                                            CssClass="btn_8"
                                            runat="server" />&nbsp;
                                        <asp:Button
                                            ID="btnResourceDelete"
                                            Text="Delete"
                                            CommandName="Delete"
                                            EnableTheming="false"
                                            CssClass="btn_8"
                                            OnClientClick="return confirm('Are you sure you want to delete this record? This action cannot be undone.');"
                                            runat="server" />
                                    </ItemTemplate>

                                    <EditItemTemplate>
                                        <asp:Button
                                            ID="btnResourceUpdate"
                                            Text="Update"
                                            EnableTheming="false"
                                            CssClass="btn_8"
                                            CommandName="Update"
                                            runat="server" />&nbsp;
                                        <asp:Button
                                            ID="btnResourceCancel"
                                            Text="Cancel"
                                            EnableTheming="false"
                                            CssClass="btn_8"
                                            CommandName="Cancel"
                                            runat="server" />
                                    </EditItemTemplate>
                                </asp:TemplateField>
                               
                                <asp:TemplateField HeaderText="Role" SortExpression="ProjectRoleID" HeaderStyle-CssClass="bold_8">
                                    <ItemTemplate>
                                        <asp:Label ID="ProjectRoleName" Text='<%# Eval("ProjectRoleName") %>' runat="server" />
                                    </ItemTemplate>
                                   
                                    <EditItemTemplate>
                                        <ucRole:ddlRole id="ddlRoleEdit" SelectedValue='<%# Bind("ProjectRoleID") %>' EditControl="true" runat="server" />
                                    </EditItemTemplate>
                                </asp:TemplateField>
                               
                                <asp:TemplateField HeaderText="NBID" SortExpression="StandardID" HeaderStyle-CssClass="bold_8">
                                    <ItemTemplate>
                                        <asp:Label ID="StandardID" Text='<%# Eval("StandardID") %>' runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                               
                                <asp:TemplateField HeaderText="Last Name" SortExpression="NameLast" HeaderStyle-CssClass="bold_8">
                                    <ItemTemplate>
                                        <asp:Label ID="NameLast" Text='<%# Eval("NameLast") %>' runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                               
                                <asp:TemplateField HeaderText="First Name" SortExpression="ProjectRoleID" HeaderStyle-CssClass="bold_8">
                                    <ItemTemplate>
                                        <asp:Label ID="NameFirst" Text='<%# Eval("NameFirst") %>' runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>

and the code I use to add a resource:

    Protected Sub btnAddResource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddResource.Click
        Dim sFullName, sLname, sFname, sNBID As String
        Dim iFullNameLen, iCommaPos, iDashPos, iResourceID As Integer

        sFullName = txtResourceSearch.Text
        iFullNameLen = Len(sFullName)

        iCommaPos = InStr(sFullName, ",")

        If iCommaPos > 0 Then
            iDashPos = InStr(sFullName, "-")

            If iDashPos > 0 Then
                sLname = Trim(Left(sFullName, (iCommaPos - 1)))
                sFname = Trim(Mid(sFullName, (iCommaPos + 1), (iDashPos - 1) - iCommaPos))
                sNBID = Trim(Right(sFullName, iFullNameLen - iDashPos))

                Dim oConn As New SqlConnection(DataProvider.GetConnectionString)
                Dim oCmd As New SqlCommand
                oConn.Open()

                With oCmd
                    .Connection = oConn
                    .CommandType = CommandType.StoredProcedure

                    .CommandText = ("up_ResourceIDSelect")
                    .Parameters.Add("@NBID", SqlDbType.VarChar).Value = sNBID
                    iResourceID = .ExecuteScalar()

                    '.Parameters.Clear()

                    '.CommandText = ("up_ProjectResourceAssignmentInsert")
                    '.Parameters.Add("@ResourceID", SqlDbType.SmallInt).Value = iResourceID
                    '.Parameters.Add("@ProjectID", SqlDbType.SmallInt).Value = dvProjectDetails.SelectedValue
                    '.Parameters.Add("@ProjectRoleID", SqlDbType.VarChar).Value = ddlRoleAdd.SelectedValue
                    '.ExecuteNonQuery()
                End With

                With dsResources
                    .InsertParameters(0).DefaultValue = iResourceID
                    .InsertParameters(1).DefaultValue = dvProjectDetails.SelectedValue
                    .InsertParameters(2).DefaultValue = ddlRoleAdd.SelectedValue
                    .Insert()
                End With

                oConn.Close()
                dsResources.SelectParameters(0).DefaultValue = dvProjectDetails.SelectedValue
                gvResources.DataBind()
                txtResourceSearch.Text = ""
            End If
        End If
    End Sub
0
 
Bob LearnedCommented:
Are you handling the command for btnResourceEdit to set the EditItemIndex for the GridView?

Bob
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
stretch73Author Commented:
No I'm not but only because this is almost the exact same code from another page (and don't ask why it's being duplicated).  The only difference is that on the other page the main gridview, for the projects, is already populated and the details gridviews are bound when a record is selected.
0
 
stretch73Author Commented:
And nothing, really nothing, happens when I hit the edit button.  It doesn't fall into the RowCommand or RowEditing routines which is even weirder.
0
 
Bob LearnedCommented:
Add an event handler for the button:

            <asp:Button ID="btnResourceEdit" runat="server" Text="Edit" CommandName="Edit" EnableTheming="false"
                CssClass="btn_8" OnCommand="OnResourceEditCommand" />

...

    Protected Sub OnResourceEditCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
        If e.CommandName = "Edit" Then
            Me.gvResources.EditIndex = CType(CType(sender, Button).Parent.Parent, GridViewRow).RowIndex
        End If
    End Sub

Bob
0
 
stretch73Author Commented:
Doesn't hit it, I actually tried something similar yesterday.  I just noticed that btnResourceEdit doesn't show up in the controls drop down list on the code behind page.
0
 
Bob LearnedCommented:
It won't show up, because it is not on the page, it is embedded within the GridView.  If I were you, I would let the GridView auto-generate the edit button.

    AutoGenerateEditButton="True"

Bob
0
 
stretch73Author Commented:
Yup, that makes it so that it actually picks up the action.  I would love to know what's keeping it from firing the other way.  Kind of worries me that it works in one place and not another.  Thanks as usual Bob, hope life in South Park is going well.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now