How to alert message if gridview textbox cell is empty.

Last Reply 16 hours ago By dharmendr

Posted 6 days ago

Hi all,

I have the below condition to full fill my requirement.

below one is my gridview

                            <asp:GridView ID="Gv1Data" runat="server" Width="1150px" GridLines="Vertical" AutoGenerateColumns="false" ShowHeader="false">
                                <Columns>
                                    <asp:TemplateField HeaderText="Column1">
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="Section_Column1" Text='<%# Eval("Section_Column1") %>' CssClass="TxtBox" ToolTip='<%# Eval("Section_Column1") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Column2">
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="Section_Column2" Text='<%# Eval("Section_Column2") %>' CssClass="TxtBox" ToolTip='<%# Eval("Section_Column2") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Column3">
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="Section_Column3" Text='<%# Eval("Section_Column3") %>' CssClass="TxtBox" ToolTip='<%# Eval("Section_Column3") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Column4">
                                        <ItemTemplate>
                                            <asp:TextBox runat="server" ID="Section_Column4" Text='<%# Eval("Section_Column4") %>' CssClass="TxtBox" ToolTip='<%# Eval("Section_Column4") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
								</Columns>
                            </asp:GridView>

 

i am binding database with gridview at page load, after next i am click the save button i want to validate below conditions.

if i entered gridview details are below like ,

Column1 Column2 Column3 Column4
       
       
       

in above grid i am entered first row details like below and save button click then no need to alert fill row details gridview.

Column1 Column2 Column3 Column4
Column      
       

in above condition the user entered first row first cell then we don't need alert message enter cell details.

Column1

Column2

Column3

Column4

F1Cell1

F1Cell2

 

F1Cell4

F2Cell2

F2Cell2

F2Cell3

F2Cell4

 

 

 

 

 

 

 

 

in above condition also no need to alert message , why the 1st and 2nd row , first cell is filled.

Column1

Column2

Column3

Column4

 

F1Cell2

F1Cell3

F1Cell4

 

F2Cell2

F2Cell3

F2Cell4

 

 

 

 

 

 

 

 

In above condition we require alert message why the 1st and 2nd row first cells are not entered. so, we required alert message.

Column1

Column2

Column3

Column4

 

 

 

 

F2Cell1

F2Cell2

F2Cell3

F2Cell4

 

 

 

 

 

 

 

 

In this case the first row first cell or entire first row all cells are not enter. so, we want to give alert message for enter cell fields.

Column1

Column2

Column3

Column4

F1Cell1

F1Cell2

F1Cell3

F1Cell4

 

 

 

 

F3Cell1

F3Cell2

F3Cell3

F3Cell4

 

 

 

 

In this case the 2nd row first cell or entire 2nd row all cells are not enter. so, we want to give alert message for enter cell fields.

How to validate above conditions. please help me.

Thanks in advance

You are viewing reply posted by: pandeyism 4 days ago.
Posted 4 days ago

Hi nagaraju60,

Please refer below sample.

HTML

<asp:GridView ID="Gv1Data" runat="server" Width="1150px" GridLines="Vertical" AutoGenerateColumns="false"
    ShowHeader="false">
    <Columns>
        <asp:TemplateField HeaderText="Column1">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Section_Column1" Text='<%# Eval("Section_Column1") %>'
                    CssClass="TxtBox" ToolTip='<%# Eval("Section_Column1") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Column2">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Section_Column2" Text='<%# Eval("Section_Column2") %>'
                    CssClass="TxtBox" ToolTip='<%# Eval("Section_Column2") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Column3">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Section_Column3" Text='<%# Eval("Section_Column3") %>'
                    CssClass="TxtBox" ToolTip='<%# Eval("Section_Column3") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Column4">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="Section_Column4" Text='<%# Eval("Section_Column4") %>'
                    CssClass="TxtBox" ToolTip='<%# Eval("Section_Column4") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnValidate" runat="server" Text="Submit" OnClick="btnValidate_Click" />

Namespaces

C#

using System.Data;
using System.Web.UI;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("Section_Column1", typeof(string)), 
                            new DataColumn("Section_Column2", typeof(string)), 
                            new DataColumn("Section_Column3", typeof(string)),
                            new DataColumn("Section_Column4", typeof(string)) });
        dt.Rows.Add("", "", "", "");
        dt.Rows.Add("1", "", "", "");
        dt.Rows.Add("", "", "", "");
        Gv1Data.DataSource = dt;
        Gv1Data.DataBind();
    }
}
protected void btnValidate_Click(object sender, EventArgs e)
{
    bool isAllRowEmpty = true;
    foreach (GridViewRow row1 in Gv1Data.Rows)
    {
        TextBox txt11 = (TextBox)row1.FindControl("Section_Column1");
        TextBox txt21 = (TextBox)row1.FindControl("Section_Column2");
        TextBox txt31 = (TextBox)row1.FindControl("Section_Column3");
        TextBox txt41 = (TextBox)row1.FindControl("Section_Column4");
        if (txt11.Text != "" || txt21.Text != "" || txt31.Text != "" || txt41.Text != "")
        {
            isAllRowEmpty = false;
        }
    }
    if (isAllRowEmpty)
    {
        return;
    }

    bool isPreviousRowEmpty = true;
    foreach (GridViewRow row in Gv1Data.Rows)
    {
        bool is1stCellEmpty = false;
        bool isAllCellEmpty = false;
        TextBox txt1 = (TextBox)row.FindControl("Section_Column1");
        TextBox txt2 = (TextBox)row.FindControl("Section_Column2");
        TextBox txt3 = (TextBox)row.FindControl("Section_Column3");
        TextBox txt4 = (TextBox)row.FindControl("Section_Column4");
        
        if (txt1.Text == "")
        {
            is1stCellEmpty = true;
        }
        else
        {
            isPreviousRowEmpty = false;
        }

        if (is1stCellEmpty)
        {
            if (txt2.Text == "" && txt3.Text == "" && txt4.Text == "")
            {
                isAllCellEmpty = true;
            }
            if (isAllCellEmpty)
            {
                isPreviousRowEmpty = true;
            }
        }
        if (isPreviousRowEmpty)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please fill all cell records')", true);
            break;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("Section_Column1", GetType(String)), New DataColumn("Section_Column2", GetType(String)), New DataColumn("Section_Column3", GetType(String)), New DataColumn("Section_Column4", GetType(String))})
        dt.Rows.Add("", "", "", "")
        dt.Rows.Add("1", "", "", "")
        dt.Rows.Add("", "", "", "")
        Gv1Data.DataSource = dt
        Gv1Data.DataBind()
    End If
End Sub

Protected Sub btnValidate_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim isAllRowEmpty As Boolean = True
    For Each row1 As GridViewRow In Gv1Data.Rows
        Dim txt11 As TextBox = CType(row1.FindControl("Section_Column1"), TextBox)
        Dim txt21 As TextBox = CType(row1.FindControl("Section_Column2"), TextBox)
        Dim txt31 As TextBox = CType(row1.FindControl("Section_Column3"), TextBox)
        Dim txt41 As TextBox = CType(row1.FindControl("Section_Column4"), TextBox)
        If txt11.Text <> "" OrElse txt21.Text <> "" OrElse txt31.Text <> "" OrElse txt41.Text <> "" Then
            isAllRowEmpty = False
        End If
    Next
    If isAllRowEmpty Then
        Return
    End If
    Dim isPreviousRowEmpty As Boolean = True
    For Each row As GridViewRow In Gv1Data.Rows
        Dim is1stCellEmpty As Boolean = False
        Dim isAllCellEmpty As Boolean = False
        Dim txt1 As TextBox = CType(row.FindControl("Section_Column1"), TextBox)
        Dim txt2 As TextBox = CType(row.FindControl("Section_Column2"), TextBox)
        Dim txt3 As TextBox = CType(row.FindControl("Section_Column3"), TextBox)
        Dim txt4 As TextBox = CType(row.FindControl("Section_Column4"), TextBox)
        If txt1.Text = "" Then
            is1stCellEmpty = True
        Else
            isPreviousRowEmpty = False
        End If
        If is1stCellEmpty Then
            If txt2.Text = "" AndAlso txt3.Text = "" AndAlso txt4.Text = "" Then
                isAllCellEmpty = True
            End If
            If isAllCellEmpty Then
                isPreviousRowEmpty = True
            End If
        End If
        If isPreviousRowEmpty Then
            ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('Please fill all cell records')", True)
            Exit For
        End If
    Next
End Sub