?
Solved

VB.NET - Edit button not firing on gridview

Posted on 2007-12-04
9
Medium Priority
?
2,498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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
 

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

771 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