Click here to Skip to main content
15,940,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to set required field in footer template,while adding records working fine but while updating already existing record ,that time also validation working ,so i cant update mt data.

What I have tried:

<pre><FooterTemplate>
 <asp:TextBox ID="txtpname" runat="server"></asp:TextBox>
 
    
            <asp:DropDownList ID = "ddlprice1" runat = "server">
            </asp:DropDownList>
         
            <asp:Button ID="btnAdd" CommandName="AddNew" runat="server" Text="Add"></asp:Button><br />
            <asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="txtpname" errormessage="Please enter product name!" />
            <asp:RequiredFieldValidator ID="reqPrice" runat="server" ControlToValidate="ddlprice1" 
                            InitialValue="0" ErrorMessage="Select Price"></asp:RequiredFieldValidator>

            </FooterTemplate>
Posted
Updated 6-Mar-17 2:11am

1 solution

refer this example

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>

        function checkMandaory(obj) {

            var row = obj.parentElement.parentElement;
            var name = $('input[id*="txtName"]', row).val();
            var address = $('input[id*="txtAddress"]', row).val();
            if (row == '' || address == '') {
                alert('Please enter the Mandatory fields!');
                return false;
            }
            return true;
        }
    </script>
</head>

<body>


    <form id="form1" runat="server">

        <asp:GridView ID="GridView1" runat="server" Width="550px" AutoGenerateColumns="false"
            AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" ShowFooter="true">
            <Columns>
                <asp:TemplateField HeaderText=" Name">
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtName" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <%# Eval("Address") %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddress" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField>
                    <ItemTemplate>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="btnAdd" OnClientClick="return checkMandaory(this)" runat="server" Text="Add" OnClick="Button1_Click" CommandName="Footer" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <AlternatingRowStyle BackColor="#C2D69B" />

        </asp:GridView>
    </form>
</body>
</html>



protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               DataTable dt = new DataTable();
               dt.Columns.Add("Name");
               dt.Columns.Add("Address");
               dt.Rows.Add("aa", "aa-1");
               dt.Rows.Add("bb", "bb-1");
               dt.Rows.Add("cc", "cc-1");
               dt.Rows.Add("dd", "dd-1");
               GridView1.DataSource = dt;
               GridView1.DataBind();

           }
       }

       protected void Button1_Click(object sender, EventArgs e)
       {

       }
 
Share this answer
 
Comments
GrpSMK 6-Mar-17 9:10am    
Thank You for Your Ans:-)
Karthik_Mahalingam 6-Mar-17 9:17am    
welcome :)

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