Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I need some help with the GridView, you can find the html code below.
On edit mode, when ddlType changes I need lblAvg updated with the proper value (from ddlType) while keeping the row on edit mode.
Similarly, I have to do the same on new addition (insert).

Any kind of help will be highly appreciated.

ASP.NET
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" ShowFooter="true" Font-Size="Small"
           AllowPaging="True" PageSize="20">
             <Columns>
                 <asp:TemplateField ItemStyle-Width = "20px"  HeaderText = "ID" Visible="false">
                     <ItemTemplate>
                         <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
                     </ItemTemplate>
                     <EditItemTemplate>
                           <asp:Label ID="txtID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
                     </EditItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField ItemStyle-Width = "300px"  HeaderText = "Type">
                     <ItemTemplate>
                         <asp:Label ID="lblType" runat="server" Width="250px" Text='<%# Eval("TypeDescription")%>'></asp:Label>
                     </ItemTemplate>
                     <EditItemTemplate>
                         <asp:DropDownList ID="ddlType" Width="250px" runat="server"></asp:DropDownList>                  
                     </EditItemTemplate>
                     <FooterTemplate>
                         <asp:DropDownList ID="addDdlType" Width="250px" runat="server"></asp:DropDownList> 
                     </FooterTemplate>
                 </asp:TemplateField>  
                 <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Average">
                     <ItemTemplate>
                         <asp:Label ID="lblAvg" runat="server" Text='<%# Eval("Avg")%>'></asp:Label>
                     </ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Percentage">
                     <ItemTemplate>
                         <asp:Label ID="lblPerc"   runat="server" Text='<%# Eval("Perc")%>'></asp:Label>
                     </ItemTemplate>
                     <EditItemTemplate>
                         <asp:TextBox ID="txtPerc" runat="server" Width="100px" Text='<%# Eval("Perc")%>'></asp:TextBox>
                     </EditItemTemplate>
                     <FooterTemplate>
                         <asp:TextBox ID="addtxtPerc" Width="100px" runat="server"></asp:TextBox>
                     </FooterTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField ItemStyle-Width="150" >
                    <ItemTemplate>
                        <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit"/>                      
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update"/>
                        <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel"/>
                    </EditItemTemplate>
                      <FooterTemplate>
                        <asp:Button ID="btnInsert" runat="server" Text="Insert" Width="100" CommandName="Insert" />
                    </FooterTemplate>
                </asp:TemplateField>
                 <asp:TemplateField>
                     <ItemTemplate>
                       <span onclick="return confirm('Delete?')">
                          <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~\Images\delete.png" OnClick="Delete" CommandArgument='<%#Eval("ID")%>' />
                       </span>
                     </ItemTemplate>
                 </asp:TemplateField>
             </Columns>
         </asp:GridView>


What I have tried:

I added the following on ddlType OnSelectedIndexChanged

C#
protected void cropTypeFillTextbox(object sender, EventArgs e)
       {
            DropDownList ddl = sender as DropDownList;

            foreach (GridViewRow row in gv.Rows)
            {
                gv.SetEditRow(row.RowIndex);


                Control ctrl = row.FindControl("ddlType") as DropDownList;
                if (ctrl != null)
                {
                    DropDownList ddl1 = (DropDownList)ctrl;
                    Label txt2 = row.FindControl("lblAvg") as Label;

                    txt2.Text = ddl1.SelectedItem.ToString();
                }
                gv.UpdateRow(row.RowIndex, false);
            }
         }
Posted
Comments
[no name] 28-Feb-19 12:23pm    
Your applying a desktop mentality to web pages. The model doesn't fit. Stick to filling in the blanks on the round-trip.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900