0

Please tell me how to remove and add CssClass to a specific textbox inside gridview ?

This is what i have tried but it doesnot changee css for the textbox

my css in my .aspx page is :

<style type="text/css">
   .erroramount
   {
       border:3px solid red
   }
</style>

in my button click here is my code for gridview looping where depending upon the condition i want to change the border color of the textbox;

 var result = (from f in dtCloned.AsEnumerable()
                      group f by f.Field<string>("AssetDescription") into g
                      select
                      new
                      {
                          AssetDescription = g.Key,
                          TotalAmount = g.Sum(r => r.Field<double?>("Amount"))
                      });

foreach (var aAsset in result.AsEnumerable())
{
  if (aAsset.TotalAmount < 0)
  {
     foreach (GridViewRow arow in GridProjectDetails.Rows)
     {
         string AssetDescription = ((TextBox)arow.FindControl("TextAssetDescription")).Text;
         if (AssetDescription == aAsset.AssetDescription)
         {
             ((TextBox)arow.FindControl("TextAmount")).CssClass = "erroramount";
         }
     }
   }
 }
4
  • maybe your code doesnt reach the loop, you can set a breakpoint there and debug to check if it reachs where you set your cssclass. Commented Dec 15, 2013 at 22:46
  • Are you sending this information back to client? Commented Dec 15, 2013 at 23:25
  • Try what Emre suggests - put a breakpoint on it and see if it gets hit. Also check that you're not re-binding your grid in some later Page event such as PreRender, which might be over-writing your changes. Commented Dec 16, 2013 at 1:12
  • Hi all, sorry for the late response. yes, it's hitting breakpoint and executing without errory but there is no change in UI Commented Dec 16, 2013 at 17:37

2 Answers 2

1

Your statement should work unless the code is not reachable or its not finding the control. It would have thrown exception if control was not found.There is an alternate way to set the class as well:

((TextBox)arow.FindControl("TextAmount")).Attributes["class"] = "erroramount";
Sign up to request clarification or add additional context in comments.

Comments

0

Hi i got my output by removing the existing (default) css class and adding this css class with textbox. it worked.

Here is the complete code

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ValidateAmount();
    }

    private void ValidateAmount()
    {
        System.Data.DataTable dtGridData = new DataTable();
        DataTable dtCloned = new DataTable();

        dtGridData.Columns.Add("ID", typeof(string));
        dtGridData.Columns.Add("DocumentNumber", typeof(string));
        dtGridData.Columns.Add("NameOfOffsettingAccount", typeof(string));
        dtGridData.Columns.Add("Amount", typeof(string));
        dtGridData.Columns.Add("AssetDescription", typeof(string));
        dtGridData.Columns.Add("Quantity", typeof(string));
        dtGridData.Columns.Add("UnitOfMeasure", typeof(string));

        foreach (GridViewRow row in GridProjectDetails.Rows)
        {
            string Id = ((TextBox)row.FindControl("ID")).Text;
            string DocumentNumber = ((Label)row.FindControl("LabelDocumentNumber")).Text;
            string NameOfOffsettingAccount = ((Label)row.FindControl("TextName")).Text;
            string Amount = ((TextBox)row.FindControl("TextAmount")).Text;
            string AssetDescription = ((TextBox)row.FindControl("TextAssetDescription")).Text;
            string Quantity = ((TextBox)row.FindControl("TextQuantity")).Text;
            string UnitOfMeasure = ((DropDownList)row.FindControl("DropDownUnitOfMeasure")).Text;

            DataRow dr = dtGridData.NewRow();
            dr["Id"] = Id;

            dr["DocumentNumber"] = DocumentNumber;
            dr["NameOfOffsettingAccount"] = NameOfOffsettingAccount;
            if (Amount.Contains(','))
            {
                Amount = Amount.Replace(",", "");
            }
            dr["Amount"] = Amount;
            dr["AssetDescription"] = AssetDescription;
            dr["Quantity"] = Quantity;
            dr["UnitOfMeasure"] = UnitOfMeasure;                

            dtGridData.Rows.Add(dr);

            dtCloned = dtGridData.Clone();
            dtCloned.Columns["Amount"].DataType = typeof(double);
            foreach (DataRow arow in dtGridData.Rows)
            {
                dtCloned.ImportRow(arow);
            }
        }

        var result = (from f in dtCloned.AsEnumerable()
                      group f by f.Field<string>("AssetDescription") into g
                      select
                      new
                      {
                          AssetDescription = g.Key,
                          TotalAmount = g.Sum(r => r.Field<double?>("Amount"))
                      });

        foreach (var aAsset in result.AsEnumerable())
        {
            if (aAsset.TotalAmount < 0)
            {
                if (!lblMessage.Text.Contains("<br/> Total Amount cannot be negative for same asset - "
                                              + aAsset.AssetDescription.ToString()))
                {
                    lblMessage.Text = lblMessage.Text + "\n" + "<br/> Total Amount cannot be negative for same asset - " + aAsset.AssetDescription.ToString();
                    lblMessage.Attributes.Add("style", "color:Red;font-weight:bold;");
                    lblMessage.Visible = true;
                }

                foreach (GridViewRow arow in GridProjectDetails.Rows)
                {
                    string AssetDescription = ((TextBox)arow.FindControl("TextAssetDescription")).Text;
                    if (AssetDescription == aAsset.AssetDescription)
                    {
                        ((TextBox)arow.FindControl("TextAmount")).CssClass =
                            ((TextBox)arow.FindControl("TextAmount")).CssClass.Replace("amount", " ");
                        ((TextBox)arow.FindControl("TextAmount")).CssClass = "erroramount";
                    }
                }
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.