asp.net, total sum, and validation

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27868397.html
I have above issue and already resolved, and now I have three major issues.

this is table with 4 text boxes in a row and I have 20 rows there. For validation purpose, if anything in a row is filled, e.g. qty. the user must complete the rest of that rows such as description, size, and equipment value. I prefer to use asp.net control validation if possible.
If not, javascript is acceptable.

my second issue is: I need to add total textbox at the bottom to show the total of equipment value, and total qty. so when the user types something in qty. the total textbox will be shown for the total number, same as equipment value.

my last issue is: i don't want to add something ' string inflatableTmp = "ctl00$ContentPlaceHolder1$UiFormIndoorFacility$WizardIndoorFacility$UiInventoryList2$" - how to fix it - calling the control without using that..

Thanks
solution1368Asked:
Who is Participating?
 
Obadiah ChristopherCommented:
If you don't like this.... I'm not very much sure...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Informaniac
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            DataTable dt = new DataTable();
            DataRow dr;
            for (int i = 0; i < 3; i++)
            {
                dr = dt.NewRow();
                dt.Rows.Add(dr);
            }
            gvTextBox.DataSource = dt;
            gvTextBox.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvRow in gvTextBox.Rows)
            {
                HiddenField hdnValue = gvRow.FindControl("hdnVal") as HiddenField;
                if (hdnValue.Value.ToUpper().Equals("TRUE"))
                {
                    TextBox txt1 = gvRow.FindControl("txt1") as TextBox;
                    TextBox txt2 = gvRow.FindControl("txt2") as TextBox;
                    TextBox txt3 = gvRow.FindControl("txt3") as TextBox;
                    TextBox txt4 = gvRow.FindControl("txt4") as TextBox;
                }
            }
        }
    }
}

Open in new window

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Informaniac.WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function $(id)
        {
            return document.getElementById(id);
        }
        function UpdateQty(obj) {
            var gvID = "gvTextBox";
            var txt1 = "txt1";
            var gv = $("gvTextBox");
            var index;
            var footerIndex;
            if (obj.value.trim() != "") {
                footerIndex = gv.rows.length < 10 ? "0" + gv.rows.length : gv.rows.length;
                var footerTextBox = $("gvTextBox_ctl" + footerIndex + "_txt5");
                footerTextBox.value = "0";
                for (var i = 2; i < parseInt(gv.rows.length); i++) {
                    if (i < 10)
                        index = "0" + i;
                    else
                        index = i;

                    var objTxt1 = $(gvID + "_ctl" + index + "_" + txt1);
                    if (objTxt1.value.trim() != "")
                        footerTextBox.value = parseInt(footerTextBox.value) + parseInt(objTxt1.value);
                }
            }
        }
        function UpdateEquipValue(obj) {
            var gvID = "gvTextBox";
            var txt4 = "txt4";
            var gv = $("gvTextBox");
            var index;
            var footerIndex;
            if (obj.value.trim() != "") {
                footerIndex = gv.rows.length < 10 ? "0" + gv.rows.length : gv.rows.length;
                var footerTextBox = $("gvTextBox_ctl" + footerIndex + "_txt6");
                footerTextBox.value = "0";
                for (var i = 2; i < parseInt(gv.rows.length); i++) {
                    if (i < 10)
                        index = "0" + i;
                    else
                        index = i;

                    var objTxt4 = $(gvID + "_ctl" + index + "_" + txt4);
                    if (objTxt4.value.trim() != "")
                        footerTextBox.value = parseInt(footerTextBox.value) + parseInt(objTxt4.value);
                }
            }
        }
        function Validate() {
            var txt1 = "txt1";
            var txt2 = "txt2";
            var txt3 = "txt3";
            var txt4 = "txt4";
            var txt5 = "txt5";
            var txt6 = "txt6";
            var hdnVal = "hdnVal";
            var gvID = "gvTextBox";
            var gv = $("gvTextBox");
            var index;
            var errorFound = false;
            for (var i = 2; i < parseInt(gv.rows.length); i++) {
                if (i < 10)
                    index = "0" + i;
                else
                    index = i;

                var objTxt1 = $(gvID + "_ctl" + index + "_" + txt1);
                var objTxt2 = $(gvID + "_ctl" + index + "_" + txt2);
                var objTxt3 = $(gvID + "_ctl" + index + "_" + txt3);
                var objTxt4 = $(gvID + "_ctl" + index + "_" + txt4);
                var objTxt5 = $(gvID + "_ctl" + index + "_" + txt5);
                var objTxt6 = $(gvID + "_ctl" + index + "_" + txt6);
                var objHdnVal = $(gvID + "_ctl" + index + "_" + hdnVal);

                if (objTxt1.value.trim() != "" || objTxt2.value.trim() != ""
                || objTxt3.value.trim() != "" || objTxt4.value.trim() != "") {
                    objHdnVal.value = "true";
                    if (objTxt1.value.trim() == "") {
                        gv.rows[i - 2].cells[0].style.backgroundColor = "red";
                        errorFound = true;
                    }
                    else if (objTxt1.value.trim() != "") {
                        gv.rows[i - 2].cells[0].style.backgroundColor = "";
                    }
                    if (objTxt2.value.trim() == "") {
                        gv.rows[i - 2].cells[1].style.backgroundColor = "red";
                        errorFound = true;
                    }
                    else if (objTxt2.value.trim() != "") {
                        gv.rows[i - 2].cells[1].style.backgroundColor = "";
                    }
                    if (objTxt3.value.trim() == "") {
                        gv.rows[i - 2].cells[2].style.backgroundColor = "red";
                        errorFound = true;
                    }
                    else if (objTxt3.value.trim() != "") {
                        gv.rows[i - 2].cells[2].style.backgroundColor = "";
                    }
                    if (objTxt4.value.trim() == "") {
                        gv.rows[i - 2].cells[3].style.backgroundColor = "red";
                        errorFound = true;
                    }
                    else if (objTxt4.value.trim() != "") {
                        gv.rows[i - 2].cells[3].style.backgroundColor = "";
                    }
                }
                else {
                    objHdnVal.value = "false";
                }
            }
            if (errorFound == true)
                return false;
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvTextBox" CellPadding="2" RowStyle-HorizontalAlign="Center" runat="server" AutoGenerateColumns="false" 
        ShowFooter="true">
    <Columns>
        <asp:TemplateField HeaderText="Qty">
            <ItemTemplate>
                <asp:HiddenField ID="hdnVal" runat="server" />
                <asp:TextBox ID="txt1" runat="server" Width="50px" onblur="javascript:UpdateQty(this);"></asp:TextBox>
            </ItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="txt5" runat="server" Width="50px" Text="0"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:TextBox ID="txt2" runat="server" Width="50px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Size">
            <ItemTemplate>
                <asp:TextBox ID="txt3" runat="server" Width="50px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Equip Value">
            <ItemTemplate>
                <asp:TextBox ID="txt4" runat="server" Width="50px" onblur="javascript:UpdateEquipValue(this);"></asp:TextBox>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txt6" runat="server" Width="50px" Text="0"></asp:TextBox>
            </FooterTemplate>
            </asp:TemplateField>
    </Columns>
</asp:GridView>

    </div>
    <br />
    <br />
    <div>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
            OnClientClick="javascript:return Validate();" onclick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
Obadiah ChristopherCommented:
Need more markup to understand the situation

"ctl00$ContentPlaceHolder1$UiFormIndoorFacility$WizardIndoorFacility$UiInventoryList2$";

This is the name of which control?

Can u post the code for the wizard control.

And just for clarity sakes, why haven't you considered using the gridview control here?

I guess that would have been an easier option rather than using this approach.

<asp:GridView ID="gvTextBox" runat="server" AutoGenerateColumns="false" ShowHeader="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt1" runat="server" Width="30px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt2" runat="server" Width="30px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt3" runat="server" Width="30px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt4" runat="server" Width="30px" TextMode="MultiLine"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Open in new window

0
 
solution1368Author Commented:
thank you for your suggestion. but i have 20 rows need to be shown for input
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Obadiah ChristopherCommented:
Can you post the html markup from the page? A couple of <tr>s. So that I can try to write the validation javascript. One more question is:

"if anything in a row is filled, e.g. qty. the user must complete the rest of that rows such as description, size, and equipment value"

This should be done on some button click right?

No. of rows should not matter you can do that using gridview also. Like this.

<asp:GridView ID="gvTextBox" runat="server" AutoGenerateColumns="false" ShowHeader="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt1" runat="server" Width="30px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt2" runat="server" Width="30px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt3" runat="server" Width="30px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txt4" runat="server" Width="30px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Open in new window

DataTable dtText;
DataRow dr;
            for (int i = 0; i < 20; i++)
            {
                dr = dtText.NewRow();
                dtText.Rows.Add(dr);
            }
            gvTextBox.DataSource = dtText;
            gvTextBox.DataBind();

Open in new window

0
 
solution1368Author Commented:
i have the source codes on http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27868397.html

Please download it. Thanks
0
 
gavsmithCommented:
Are you by any chance using jQuery?
0
 
solution1368Author Commented:
informaniac: I really appreciate your helps. However, I think I have to stick with my codes because it is already in production website. I can't just take your gridview idea and continue the changes for solution. However, I will still give you full points because of your effort.

I will re-post the question and hopefully get more helps.

Thanks
0
 
solution1368Author Commented:
Great Helps.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.