Web Forms Model Binding: How to omit binding for not visible control? Web Forms Model Binding: How to omit binding for not visible control? asp.net asp.net

Web Forms Model Binding: How to omit binding for not visible control?


This is more an algorithm than a coded solution.

I like to use a separate class say MyData.cs to manage my data updates and pass UI elements through the methods of this class.I'm like stored procedures, but you can create queries within your project.

If there is variation between which controls are visible and not, I'd recommend:

MyBindingMethod(array[] of the controls){    // Loop through array updating data.    // Or loop through array and call a second method to update the data.}

Dynamically you can check the visibility of controls and then add them to the array or not to pass to the binding method.

If the controls that switch visibility are constant you can use two separate methods, to selectively update:

MyBindingMethodAll(){    // Update all controls.}MyBindingMethodVisible(){    // Update controls that remain visible.}

Then call the methods from the MyData.cs from your aspx.cs. The trick is to maintain control of your data binding within your C# and you can determine exactly what is updated, where and when.

I am happy to provide a more detailed working example, if you are able to provide more code.


Edit Update to assist in clarifying solution

By using a separate class to manage data binding, the display element can be passed to a method of this separate class. I've used stored procedures.

class ManageData

public static void SelectAllSomething(DropDownList list)    {        // Clear any previously bound items.        list.Items.Clear();         // SqlConnection.        SqlConnection con = new SqlConnection(conString);        // Create new command and parameterise.        SqlCommand cmd = new SqlCommand();        cmd.CommandType = CommandType.StoredProcedure;        cmd.CommandText = "MyStoredProcedure";        cmd.Connection = con;        try        {            // Open connection and bind data to GUI.            con.Open();            list.DataSource = cmd.ExecuteReader();            list.DataTextField = "Name";            list.DataValueField = "ID";            list.DataBind();        }        catch (Exception ex)        {            throw ex;        }        finally        {            con.Close();            con.Dispose();        }    }

From you aspx.cs call the method from ManageData class.

ManageData.SelectAllCat(MyDropDownList);

Using this same principal.
Without seeing your layout I can only give you a concept example.

  • if you have TextBoxes you want to control.

TextBox1, TextBox2, TextBox3, .../

public static void AddText(List<TextBox> MyTextBoxes){    for(int i=0; i<MyTextBoxes.Count();i++){        MyTextBoxes.[i].Text = // What means you are using.    }}

From the aspx.cs

public List<TextBox> FindVisibleTextBoxes(){    List<TextBox> MyTextBoxes = new List<TextBox>();    if(TextBox1.Visible== true){        MyTextBoxes.Add(TextBox1);    }    return MyTextBoxes;}

Pass textbox list into method from ManageData.

This can better modularised, according to your needs and you can pass more than one List or a List of Objects to pass through a hetergenous mix of controls.

This is just one concept, there are many ways to go about things. I hope you find this useful to develop more ways to solve your dilemma.