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";
}
}
}
}
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";
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";
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.