Change background color of dynamically created GridView row using C# in ASP.Net

Ikramshams
 
on Aug 02, 2021 11:03 PM
406 Views

I am creating Dynamically TextBoxes inside GridView. I want to change Background color of dynamically created TextBoxes on condition from code behind. How I will do it? I have taken the reference of below url for creating these TextBoxes.

Retain TextBox focus in dynamically created GridView in ASP.Net

<div  class="row" >
    <asp:GridView ID="grdVoucherCoupon" runat="server" AutoGenerateColumns="false" HorizontalAlign="Right">
        <Columns>
            <asp:TemplateField HeaderText="رقم الكوبون" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:TextBox ID="txtVoucherNo" runat="server" Height="25px" MaxLength="10" OnTextChanged="TextBox_Changed" onkeypress="return isNumber(event)" AutoPostBack="true" Width="100%"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="مبلغ الكوبون">
                <ItemTemplate>
                    <asp:TextBox ID="txtAmount" ReadOnly="true" Enabled="false" Height="25px"  runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Expery Date" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:TextBox ID="txtExpiryDate" runat="server" Height="25px" Enabled="false"  Width="100%"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="الحالة" ItemStyle-Width="600">
                <ItemTemplate>
                    <asp:TextBox ID="txtMessage" ReadOnly="true" Enabled="false" runat="server" Width="100%" Height="25px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 </div>

 

protected void TextBox_Changed(object sender, EventArgs e)
{
    GridViewRow row = (GridViewRow)((TextBox)sender).NamingContainer;
    TextBox rowTextVoucherNo = row.FindControl("txtVoucherNo") as TextBox;      
    TextBox rowTextMsg = row.FindControl("txtMessage") as TextBox;
 
     DataTable dtResult=new DataTable();
     param1 = new SqlParameter("@ParameterName", rowTextVoucherNo.Text.Trim());
             
     dtResult = dataMgr.GetDataTableBySP("StoredProcedureName", new SqlParameter[] { param1 });
     if (dtResult.Rows.Count > 0)
     {                 
         rowTextMsg.BackColor = Color.Red;                 
     }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 03, 2021 05:54 AM

Use below code.

protected void TextBox_Changed(object sender, EventArgs e)
{
    GridViewRow row = (GridViewRow)((TextBox)sender).NamingContainer;
    TextBox rowTextVoucherNo = row.FindControl("txtVoucherNo") as TextBox;      
    TextBox rowTextMsg = row.FindControl("txtMessage") as TextBox;
    DataTable dtResult=new DataTable();
    param1 = new SqlParameter("@ParameterName", rowTextVoucherNo.Text.Trim());
             
    dtResult = dataMgr.GetDataTableBySP("StoredProcedureName", new SqlParameter[] { param1 });
    if (dtResult.Rows.Count > 0)
    {
        row.BackColor = Color.Red;                    
    }
}