Skip to main content

Field Coloring and Necessity Control by Condition in DetailTable Object

The following code can be used to change object properties based on a condition in a DetailTable object.

In the example, there are 3 TextBoxes named Metin1, Metin2, Metin3 in the DetailTable. If the value entered in the TextBox of Metin1 is greater than the value entered in the TextBox of Metin2, the font color of the TextBox of Metin3 will be made red and Metin3 will not be allowed to be blank.

using System.Web.UI.WebControls;

public void DetayTablo1_RowInserting(object sender, DetailsGridInsertRowEventArgs args)
{
if(DetailTable1.CurrentRowCount > 0)
{
for(int i=0; i<DetailTable1.CurrentRowCount;i++) { TextBox tx1 = (TextBox)DetailTable1.GetRowObject(i,"Text1"); TextBox tx2 = (TextBox)DetailTable1.GetRowObject(i,"Text2"); TextBox tx3 = (TextBox)DetailTable1.GetRowObject(i,"Text3"); if(Convert.ToInt32(tx1.Text)>Convert.ToInt32(tx2. Text))
{
tx3. ForeColor=System.Drawing.Color.Red;
} } }
}

In the OnSaveData event, we are navigating between the rows of the DetailTable. For the lines where the Metin3 field is red and the Metin3 field is left blank, we print the "Fill" value in the TextBox named control (default value is 0) that we put on the form.

public void OnSaveData()
{
control. text = ""; for(int i=0; i<DetailTable1.CurrentRowCount; i++) {

if(string. IsNullOrEmpty(((TextBox)DetailTable1.GetRowObject(i,"Text3")). Text))
{
if(((TextBox)DetailTable1.GetRowObject(i,"Text3")). ForeColor == System.Drawing.Color.Red)
{
control. Text = "Fill in";
}
}
}
}

Then, on the validation side, the value in the TextBox named control is checked and if this value is "Fill", a warning is given.

public override void OnValidateDocument(string view, bool canEdit, Hashtable parameters, ValidationSummary summary)
{
validation code goes here
summary. AddMessage("message");
if(control == "Fill")
summary. AddMessage("The field in DetailTable cannot be left blank!");
}