如何突出显示网格内的文本框 [英] how to get highlighted for textbox inside grid

查看:111
本文介绍了如何突出显示网格内的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

<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>






















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; } }
}


<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>


like this?



like this?

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;
}


这篇关于如何突出显示网格内的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆