Hide some fields from LIST,ADD,VIEW and EDIT pages from a comma separated string

This public forum is for user-to-user discussions of ASP.NET Maker. Note that this is not support forum.
Post Reply
Andros
User
Posts: 111

Hide some fields from LIST,ADD,VIEW and EDIT pages from a comma separated string

Post by Andros »

Hello, in my solution for a given table I have a HiddenFields string field which contains the comma separated list of fields that I want to hide from LIST, VIEW, ADD and EDIT pages.
For example for customers table (CustomerID,FirstName,LastName,CustomerLevelID,NickName,...) if I have the HiddenFields = "CustomerLevelID,NickName" I have to set CustomerLevelID.Visible and NickName.Visible to false.
I want to iterate the comma separated list and extract the field names: from the string field name (e.g. myFieldName="CustomerLevelID") how can I set the Visible property to false? something like CurrentTable.Field[myFieldName].Visible=false.
Thanx.


MichaelG
User
Posts: 1095

Post by MichaelG »

To get a field by name and set Visible to false, use:

var field = FieldByName(name);
field?.Visible = false;

Andros
User
Posts: 111

Post by Andros »

Perfect it works, thank you.

Now I have this code in Page_DataRendering event for EVERY TABLE I need.

I tried to put the code in a GLOBAL FUNCTION, but it gives me the error that FieldByName is not available there.

How can I run this code from every table without coding all the specific events? Can I put some code in a TABLE GENERIC Page_DataRendering call?

// Page Data Rendering event
public void Page_DataRendering(ref string header) {
    // Example:
    //header = "your header";

//this is my full code
string jsonStr = Profile.GetValue("myJsonConfig");
JObject jsonObj = JObject.Parse(jsonStr);
var items = jsonObj["tables"][CurrentTable.Name]["fieldstohide"].ToString().Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
foreach (string item in items) {
    var myfield = FieldByName(item);
    myfield.Visible = false;
}      

//I would like to simply call the global function SETFIELDSVISIBLE()

}

MichaelG
User
Posts: 1095

Post by MichaelG »

I tried to put the code in a GLOBAL FUNCTION, but it gives me the error that FieldByName is not available there.

You should pass the table as the parameter in the Global function, or just use CurrentTable (e.g. CurrentTable.FieldByName(name))


Post Reply