Marcel Marcel - 26 days ago 4x
ASP.NET (C#) Question

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

I am using the new Model Binding feature for WebForms, with .NET Framework Version 4.5.1. What I hope to achieve is, to exclude some of the two-way bindings, based on some condtions.

I very much like the (hopefully now famous) blog post series, by Scott Guthrie. I implement an editing page using the approach number two from Web Forms Model Binding Part 3: Updating and Validation (ASP.NET 4.5 Series)

Here's what I have: (simplified, in ElementEdit.aspx):

<asp:FormView runat="server" ID="FormViewElement" RenderOuterTable="false" DefaultMode="Edit" DataKeyNames="ElementId"
<asp:Panel runat="server" DefaultButton="ButtonSpeichern">
/*some databound controls*/
<asp:Panel runat="server" Visible="<%# !Item.CurrentElementData.SomeCondition() %>">
/*more databound controls*/
/*the submit button ("ButtonSpeichern")*/

As you see, there is a condition for the visibility on the wrapped inner panel with "more databound controls". These should bind only, when the conditioni is true, and they are visible. Otherwise they should not bind and not change the values.

The update works like in Scott's post (simplified, in xxPage.cs), which is a generic base class of Type Element:

protected virtual bool UpdateEntity(int id) {
T existing = UseCase.GetItem(id); //gets the original element

TryUpdateModel(existing); //SHOULD NOT update the invisible databound controls, but does

if (ModelState.IsValid) {
return true;
return false;

Here, after the call to
, the invisible controls have updated the model, which is what I wanted to avoid.

How to dynamically omit data binding for some elements, based on a condition, even though setting them invisible does not help?

I now have create a workaround, which solves the problem for me today: I simply have created two .aspx pages with their respective code behind. Depending on which fields the user should successfully edit, I call the appropriate page in the first place.

This, however, does not solve the underlying problem, which is conditional databinding.


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:

    // Update all controls.


    // 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.
        // 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;
            // Open connection and bind data to GUI.

            list.DataSource = cmd.ExecuteReader();
            list.DataTextField = "Name";
            list.DataValueField = "ID";

        catch (Exception ex)
            throw ex;

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


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){

    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.