Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
SQL
I have on grid which has four columns.second and fourth columns are text boxes.I have one update
button out side the grid.when click on the update button my grid value will store in database.Now

if any of the two text boxes are empty after ckicking update i want to that fields to be highlighted.

Please suggest.
Posted

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="JQuery.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="update" OnClick="Button1_Click" />
        <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txtbox" Text='<%# Eval("Value") %>' runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>







C#
namespace POC
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) {

            if (Page.IsPostBack)
            { }
            else
            {
                List<Entity> lst = new List<Entity>();
                lst.Add(new Entity() { Name = "karthik", Value = "25" });
                lst.Add(new Entity() { Name = "india", Value = "" });
                lst.Add(new Entity() { Name = "tamil nadu", Value = "55" });
                lst.Add(new Entity() { Name = "karnataka", Value = "" });
                lst.Add(new Entity() { Name = "Microsoft", Value = "66" });
                GridView1.DataSource = lst;
                GridView1.DataBind();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows )
            {
                var textbox = row.FindControl("txtbox") as TextBox;
                bool isEmpty = textbox.Text == "";
                if(isEmpty)
                    textbox.BackColor = System.Drawing.Color.Yellow;

            }
        }

    }

    public class Entity { public string Value { get; set; } public string Name { get; set; } }
}
 
Share this answer
 
Comments
spanner21 22-Nov-13 9:42am    
thanks karthik..it helped me
Karthik_Mahalingam 22-Nov-13 10:40am    
always welcome.. happy coding :)
XML
<html>


<head>
    <style>
        .wd25 {
            width: 25%;
            text-align: center;
        }
        .trH {
            background-color: maroon;
            color: white;
        }
        .mT {
            width: 600px;
            border: solid 1px lightgray;
            font-family: verdana;
            font-size:  9pt;
        }
        tr {
            background-color: lightgray;
        }
        input {
            width: 98%;
            border: solid 1px dimgray;
        }
        .inputM {
            width: 98%;
            border: solid 1px dimgray;
            background-color: lightpink;
        }
    </style>
</head>

<body>
    <table id="tbl" class="mT" cellpadding="4" cellspacing="1">
        <tr class="trH">
            <td class="wd25">
                Name
            </td>
            <td class="wd25">
                Phone
            </td>
            <td class="wd25">
                Address
            </td>
            <td class="wd25">
                DOB
            </td>
        </tr>
    </table>

    <br />
    <input type="button" value="Save" onclick="CheckDetails();" style="width: 100px" />
</body>

<script type="text/javascript" language="javascript">

    document.body.onload = function ( ) {
        for(var c = 1; c <= 10; c++) {
            var tr = document.createElement("tr");
            var tdName = document.createElement("td");
            var tdPhone = document.createElement("td");
            var tdAddress = document.createElement("td");
            var tdDob = document.createElement("td");

            var txtPhone = document.createElement("input");
            var txtDob = document.createElement("input");

            txtPhone.type = "text";
            txtDob.type = "text";

            tdPhone.appendChild(txtPhone);
            tdDob.appendChild(txtDob);

            tdName.innerText = "Name ";// + c;
            tdAddress.innerText = "Address ";// + c;

            tr.appendChild(tdName);
            tr.appendChild(tdPhone);
            tr.appendChild(tdAddress);
            tr.appendChild(tdDob);

            tbl.childNodes[0].appendChild(tr);
        }
    };

    function CheckDetails() {
        for(var c = 1; c < tbl.rows.length; c++) {
            if(tbl.rows[c].cells[1].children[0].value == "") {
                tbl.rows[c].cells[1].children[0].className = "inputM";
            }
            else {
                tbl.rows[c].cells[1].children[0].className = "";
            }

            if(tbl.rows[c].cells[3].children[0].value == "") {
                tbl.rows[c].cells[3].children[0].className = "inputM";
            }
            else {
                tbl.rows[c].cells[3].children[0].className = "";
            }
        }
    }
</script>

</html>
 
Share this answer
 
like this?

C#
var i = dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["Column1"].Value = "123";
dataGridView1.Rows[i].Cells["Column2"].Value = "qwe";
dataGridView1.Rows[i].Cells["Column3"].Value = "yxc";
dataGridView1.Rows[i].Cells["Column4"].Value = "";

i = dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["Column1"].Value = "890";
dataGridView1.Rows[i].Cells["Column2"].Value = "";
dataGridView1.Rows[i].Cells["Column3"].Value = "klj";
dataGridView1.Rows[i].Cells["Column4"].Value = "iop";

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    row.Cells["Column2"].Style.BackColor = string.IsNullOrEmpty(row.Cells["Column2"].Value.ToString()) ? Color.Red : Color.White;
    row.Cells["Column4"].Style.BackColor = string.IsNullOrEmpty(row.Cells["Column4"].Value.ToString()) ? Color.Red : Color.White;
}
 
Share this answer
 

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