Edit & delete asp:commandfield stop working after gridview conversion to asp:templatefield

162 views Asked by At

O' Great and Powerful Wizards of Visual Studio,

(asp.net C#)

I have 2 gridviews of employee skills. To simplify the question, I have only the employee badge_ID (123 = John Doe) and the skill_ID (34 = knowledge of CNC lathe usage). My database table looks like this:

table: empl_skills
ref_no (key) badge_ID   Skill_ID   prof_value     Need_Improve   skill_update   
------       --------   --------   ------------   ------------   ------------
  1            123          35        Expert           No         10/08/20
  2            123          36        Proficient       No         10/08/20
  3            123          37        Expert           No         10/08/20
  4            319          12        Expert           No         10/08/20
  5            319          60        Basic            Yes        10/08/20
  6            225          62        Proficient       No         10/08/20
  7            225          71        Proficient       No         10/08/20
The first ref_no column is the primary key that is (1,1) identity

The setup is quite simple. The first Gridview1 only displays the badge_ID and SELECT so you can pick the employee. Once selected, Gridview1 hides and the second Gridview2 displays only that employee's skills with the EDIT and DELETE commandfields turned on. This was all working great and I built the code in the designer, all working, in less than 5 minutes.

Now I want to add an INSERT function. I was going to do that in a FooterTemplate with an INSERT linkbutton in the primary key column (ref_no) and textboxes in the other columns for data entry. So to do that I first need to convert the default asp:boundfields to asp:templatefields. As soon as I did that conversion of the fields, my EDIT and DELETE functions in the commandfield stopped working and I can't figure out why.

It really is quite maddening because in ALL of these other webforms below - more than 30 times, I have converted the default asp:boundfields to asp:templatefields and I've never had this problem.

Other Successful WebForms

Hopefully, someone can tell me what's going on, please?

Here's Gridview1 to select an employee (I removed the styling stuff)

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" 
   BorderStyle="Solid" BorderWidth="1px" 
   CellPadding="3" CssClass="centerMyHeader" DataKeyNames="badge_ID" 
   DataSourceID="SqlDataSource1" OnPreRender="GridView1_PreRender" 
   ForeColor="Black" GridLines="Vertical">
   <AlternatingRowStyle BackColor="#EEEEEE" />
   <Columns>
      <asp:CommandField ShowSelectButton="True" />
   </Columns>
</asp:GridView>

Here's Gridview2 with the boundfields converted to templatefields:

<asp:GridView ID="GridView2" runat="server" BackColor="White" BorderColor="#999999" 
   BorderStyle="Solid" BorderWidth="1px" 
   CellPadding="3" CssClass="centerMyHeader" DataSourceID="SqlDataSource2" 
   ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="False">
   <AlternatingRowStyle BackColor="#EEEEEE" />
   <Columns>
      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
      <asp:TemplateField HeaderText="ref_no" InsertVisible="False" SortExpression="ref_no">
         <EditItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ref_no") %>'></asp:Label>
         </EditItemTemplate>
         <ItemTemplate>
            <asp:Label ID="Label6" runat="server" Text='<%# Bind("ref_no") %>'></asp:Label>
         </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="badge_ID" SortExpression="badge_ID">
          <EditItemTemplate>
             <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("badge_ID") %>'> 
             </asp:TextBox>
          </EditItemTemplate>
          <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Text='<%# Bind("badge_ID") %>'></asp:Label>
          </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="skill_ID" SortExpression="skill_ID">
       <EditItemTemplate>
         <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("skill_ID") %>'></asp:TextBox>
       </EditItemTemplate>
       <ItemTemplate>
          <asp:Label ID="Label2" runat="server" Text='<%# Bind("skill_ID") %>'></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="prof_value" SortExpression="prof_value">
       <EditItemTemplate>
          <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("prof_value") %>'>
          </asp:TextBox>
       </EditItemTemplate>
       <ItemTemplate>
          <asp:Label ID="Label3" runat="server" Text='<%# Bind("prof_value") %>'></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="need_improve" SortExpression="need_improve">
          <EditItemTemplate>
          <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("need_improve") %>'>
          </asp:TextBox>
          </EditItemTemplate>
          <ItemTemplate>
             <asp:Label ID="Label4" runat="server" Text='<%# Bind("need_improve") %>'>
             </asp:Label>
          </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="skill_update" SortExpression="skill_update">
          <EditItemTemplate>
             <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("skill_update") %>'>
             </asp:TextBox>
           </EditItemTemplate>
           <ItemTemplate>
              <asp:Label ID="Label5" runat="server" Text='<%# Bind("skill_update") %>'>
              </asp:Label>
           </ItemTemplate>
           </asp:TemplateField>
   </Columns>
</asp:GridView>

The CodeFile just hides whichever gridviews are supposed to show or not show:

    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        if (GridView1.SelectedRow != null)
        {
            //when one employee is selected
            GridView1.Visible = false;
            Label1.Visible = false;
            GridView2.Visible = true;
            Label2.Visible = true;
            btnReload2.Visible = true;
        }
        else
        {
            //when the selection list is up (no one selected yet)
            GridView2.Visible = false;
            Label2.Visible = false;
            GridView1.Visible = true;
            Label1.Visible = true;
            btnReload2.Visible = false;
        }
    }

While it only took me 5 minutes to initially build the gridviews (you can see how many times I've done this), I've spent the last two days trying to make this work with templatefields. Can anyone help, please? Thanks, John

EDIT: Sorry - I didn't say what happens.

Edit linkbutton: On a row when I click Edit, the textboxes appear and I can change the data in any column. Then I click Update. RESULT: the change does NOT take place and no errors are thrown. Essentially, nothing happens

Delete Linkbutton: when clicking Delete, the webform throws the error: Must declare the scalar variable "@ref_no". Not sure what they mean by that or moreover where? Here is SqlDataSource2 with ref_no declared in the UpdateParameters:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TrainingDBConnection %>" 
    DeleteCommand="DELETE FROM [empl_skills] WHERE [ref_no] = @ref_no" 
    InsertCommand="INSERT INTO [empl_skills] ([badge_ID], [skill_ID], [need_improve], [skill_update], [prof_value]) 
         VALUES (@badge_ID, @skill_ID, @need_improve, @skill_update, @prof_value)" 
    SelectCommand="SELECT [ref_no], [badge_ID], [skill_ID], [need_improve], [skill_update], [prof_value] FROM [empl_skills] WHERE ([badge_ID] = @badge_ID)" 
    UpdateCommand="UPDATE [empl_skills] SET [badge_ID] = @badge_ID, [skill_ID] = @skill_ID, [need_improve] = @need_improve, [skill_update] = @skill_update, 
    [prof_value] = @prof_value WHERE [ref_no] = @ref_no">
    <DeleteParameters>
        <asp:Parameter Name="ref_no" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="badge_ID" Type="Int32" />
        <asp:Parameter Name="skill_ID" Type="Int32" />
        <asp:Parameter Name="need_improve" Type="String" />
        <asp:Parameter DbType="Date" Name="skill_update" />
        <asp:Parameter Name="prof_value" Type="String" />
    </InsertParameters>
    <SelectParameters>
        <asp:ControlParameter ControlID="GridView1" Name="badge_ID" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="badge_ID" Type="Int32" />
        <asp:Parameter Name="skill_ID" Type="Int32" />
        <asp:Parameter Name="need_improve" Type="String" />
        <asp:Parameter DbType="Date" Name="skill_update" />
        <asp:Parameter Name="prof_value" Type="String" />
        <asp:Parameter Name="ref_no" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

Thanks.

1

There are 1 answers

0
John Joseph On

nevermind - I guess there's no interest. I converted the second detail Gridview to a DetailsView which already has Edit, Delete, and Insert commandfields. Problem solved - no custom code. Works flawlessly. I guess no one really cares about webforms anymore, too old.