Solved

VB.NET - Edit button not firing on gridview

Posted on 2007-12-04
9
2,470 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

16 Experts available now in Live!

Get 1:1 Help Now