Solved

VB.NET - Edit button not firing on gridview

Posted on 2007-12-04
9
2,490 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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