locked
How to affect elements based on data value RRS feed

  • Question

  • User-1425666379 posted

    Here's a simple one... 

    I have a value in my Entity model that tells me if page elements should be read only. I want to check that value before the page displays and make my Gridview/Formview etc readonly. How do get to that value?

     I want to do something like:

     if (IsClosed)
    {
         myFormView.ChangeMode(FormViewMode.ReadOnly);

    }

    Monday, September 8, 2008 9:08 PM

Answers

  • User-1425666379 posted

    Ok, if I could click Diego Vega as having the right answer I would. I used a variation of his solution that worked.

    If you want to read values and act on them before the page displays, it appears the only way to get at them is via DataItem. If you are working with a DataView, you can get to that with e.row.DataItem. However if you are using a FormView, you must first cast sender as a FormView type to get the DataItem. The rest is all about ICustomTypeDescriptor, which knowledge was delivered by Diego. Something like this should work:

    //Don't forget these!
    using System.Data.Linq;
    using System.ComponentModel;
    
    protected void FormView1_ItemCreated(object sender, EventArgs e)
    {
        FormView fv = (FormView)sender;
        
        //Not my favorite way of stopping this, but the event gets called
        //multiple times and gives an exception since DataItem is null sometimes
        if (fv.DataItem != null)
        {
            ICustomTypeDescriptor descriptor = fv.DataItem as ICustomTypeDescriptor;
            var prop = descriptor.GetProperties().Cast().First();
    
            //Get the current instance of your entity data
    MyEFEntity entity = (MyEFEntity)descriptor.GetPropertyOwner(prop);




    //Now do something based on your data, like make a form ReadOnly... if (entity.MyProperty == "myvalue") { FormView1.ChangeMode(FormViewMode.ReadOnly); } } }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 9, 2008 7:34 PM

All replies

  • User-330204900 posted

    I guess this would need to be a custom page, i would use an IAutoFieldGenrator and set each item to to be a readonly Field

    see my Generate Columns/Rows (using IAutoFieldGenerator) in my A DynamicData Attribute Based Permission Solution using User Roles. series.

    In the above Generate Columns/Rows (using IAutoFieldGenerator) I create a DynamicReadonlyField in the FilteredFieldsManager if a field is readonly, what you could do is check if IsClosed and then set all fields ro readonly.

    Hope this helps [:D]

    Tuesday, September 9, 2008 3:51 AM
  • User-1425666379 posted

    Sadly Steve, I'm seeing how remedial my question is. I apologize:

     I can't figure out how to even see my database values in code. I have the custom page, and I can make the necessary elements readonly. But I want the page to render as readonly based on what I see in my data, particularly the "IsClosed" field. I'm using Entity Framework, and on my page, for the life of me I can't get to those values.

     This is like forgetting "if" syntax, or something

     humbled,

    jeff

    Tuesday, September 9, 2008 10:21 AM
  • User188291263 posted

     Use OnDataBound event,where u can get a data from current Row

    Tuesday, September 9, 2008 11:38 AM
  • User-1425666379 posted

    Ok, well at least I'm on the right track, but that's not quite doing it. I'm using a FormView as a details section for my Grid. When the user is on an item in the master grid whose "IsClosed" column reads true, I want the details section to go ReadOnly.

    I had thought that on FormView1_ItemCreated I could simply check the current IsClosed value and then make the FormView readonly.

    In the object viewer tool in debug, I can drill down into the FormView to the DataItem, and I can see what I need in the WrappedEntity, but I don't know how to get at it programmatically. This link from Diego Vega seemed promising but I keep getting "Unable to cast object of type 'MyEntityType' to type 'System.Data.Linq.EntitySet`1[MyEntityType]'."

    Please, I've been stuck on what I thought was a simple thing for way too long. Any help would be much appreciated.

    Tuesday, September 9, 2008 3:28 PM
  • User-1425666379 posted

    Ok, if I could click Diego Vega as having the right answer I would. I used a variation of his solution that worked.

    If you want to read values and act on them before the page displays, it appears the only way to get at them is via DataItem. If you are working with a DataView, you can get to that with e.row.DataItem. However if you are using a FormView, you must first cast sender as a FormView type to get the DataItem. The rest is all about ICustomTypeDescriptor, which knowledge was delivered by Diego. Something like this should work:

    //Don't forget these!
    using System.Data.Linq;
    using System.ComponentModel;
    
    protected void FormView1_ItemCreated(object sender, EventArgs e)
    {
        FormView fv = (FormView)sender;
        
        //Not my favorite way of stopping this, but the event gets called
        //multiple times and gives an exception since DataItem is null sometimes
        if (fv.DataItem != null)
        {
            ICustomTypeDescriptor descriptor = fv.DataItem as ICustomTypeDescriptor;
            var prop = descriptor.GetProperties().Cast().First();
    
            //Get the current instance of your entity data
    MyEFEntity entity = (MyEFEntity)descriptor.GetPropertyOwner(prop);




    //Now do something based on your data, like make a form ReadOnly... if (entity.MyProperty == "myvalue") { FormView1.ChangeMode(FormViewMode.ReadOnly); } } }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 9, 2008 7:34 PM
  • User-1005219520 posted

    Thanks for posting the solution. I'll let Diego Vega know his blog was useful.

    Tuesday, September 9, 2008 9:05 PM
  • User-1425666379 posted

    And I actually did get Diego's code to work as posted in his blog. I don't know what the problem was before.. :(

    Friday, September 12, 2008 8:55 PM