Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

asp.net, total sum, and validation

Posted on 2012-09-18
8
Medium Priority
?
463 Views
Last Modified: 2012-09-25
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
0
Comment
Question by:solution1368
  • 4
  • 3
8 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38411355
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
 

Author Comment

by:solution1368
ID: 38411887
thank you for your suggestion. but i have 20 rows need to be shown for input
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38412217
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:solution1368
ID: 38415794
i have the source codes on http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27868397.html

Please download it. Thanks
0
 
LVL 10

Expert Comment

by:gavsmith
ID: 38416960
Are you by any chance using jQuery?
0
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 38423569
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
 

Author Comment

by:solution1368
ID: 38433941
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
 

Author Closing Comment

by:solution1368
ID: 38433943
Great Helps.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

577 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