簡體   English   中英

如何刪除css類並將其添加到c#asp.net中gridview內的特定文本框中?

[英]how to remove and add css class to a specific textbox inside gridview in c# asp.net?

請告訴我如何刪除CssClass並將其添加到gridview內的特定文本框?

這是我嘗試過的,但它不會更改文本框的css

我的.aspx頁面中的CSS是:

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

在我的按鈕中,單擊此處是我的gridview循環代碼,其中取決於要更改文本框邊框顏色的條件;

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

除非代碼無法訪問或找不到控件,否則您的語句應該起作用。 如果找不到控件,它會拋出異常。還有另一種設置類的方法:

((TextBox)arow.FindControl("TextAmount")).Attributes["class"] = "erroramount";

嗨,我通過刪除現有的(默認)css類並使用文本框添加此css類來獲得輸出。 有效。

這是完整的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM