Solved

VB.NET - Edit button not firing on gridview

Posted on 2007-12-04
9
2,475 Views
Last Modified: 2013-11-26
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
Comment
Question by:stretch73
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20402886
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
 

Author Comment

by:stretch73
ID: 20402914
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20402925
Are you handling the command for btnResourceEdit to set the EditItemIndex for the GridView?

Bob
0
 

Author Comment

by:stretch73
ID: 20402970
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:stretch73
ID: 20402989
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20403169
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
 

Author Comment

by:stretch73
ID: 20403220
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 20403243
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
 

Author Comment

by:stretch73
ID: 20403283
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now