Removing Columns From Gridview in List.aspx Page

Last post 05-07-2008 10:57 PM by westmich. 35 replies.

Sort Posts:

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 8:48 AM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    For what it's worth, my Intellisense doesn't work either and I'm not getting the error you are getting. One difference is I'm using C# and you're using VB.Net.

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 1:02 PM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 1:11 PM
    • Newton-le-Willows, UK
    • Posts 834

    Hi tlanier I've just tested your code with a detail page and a list page it works fine :-D

     

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
    Oh, and don't forget to mark as answer posts that answer your question Big Smile
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 1:33 PM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    Steve, 

    If I set the DetailsView RowsGenerator in an Edit page, I get the error concerning viewstate. The List and Details pages work fine.

    I'm also still searching for a way to mark a field as read only.

    Tommy 

     

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 2:40 PM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 1:11 PM
    • Newton-le-Willows, UK
    • Posts 834

    tlanier:
    If I set the DetailsView RowsGenerator in an Edit page,

    Sorry Tommy I've now put it in all the Page Templates "Details, Edit, Insert, List & ListDetails" no problems at all. you might try a fresh dynamicData website connected to Northwind and see if you still get the problem if so it might be your install.

     However in ListDetails I get a javascript error when I click Edit ALL other pages work fine, its only when I change the routing from the default of this drop to test the ListDetails.aspx template that i get a problem. With the Routing below I have no problems.

    routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
    {
        Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
        Model = model
    });
    
     
    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
    Oh, and don't forget to mark as answer posts that answer your question Big Smile
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 3:00 PM
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 974
    • AspNetTeam

    The intellisense error should not break the runtime.  If you want to get rid of it, see my mail above.  Copying from there: "The intellisense is broken because the simplistic install batch file doesn't copy the updated System.Web to \windows\Microsoft.NET\Framework\v2.0.50727.  Potentially, you could copy it manually, though you should be able to run fine without it."

    Tommy, making the field readonly is not as easy as it should be, but here is way: create a derived DynamicReadonlyField (e.g. in App_Code), e.g.

        public class DynamicReadonlyField : DynamicField {
            public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) {
                if (cellType == DataControlCellType.DataCell) {
                    var control = new DynamicControl() { DataField = DataField };
    
                    // Copy various properties into the control
                    control.UIHint = UIHint;
                    control.HtmlEncode = HtmlEncode;
                    control.NullDisplayText = NullDisplayText;
    
                    cell.Controls.Add(control);
                }
                else{
                    base.InitializeCell(cell, cellType, rowState, rowIndex);
                }
            }
        }
    

    Then create that instead of DynamicFields for columns that you want read only.  Please let me know if this works for you.

    Also, can you give more info about how you get that viewstate error?

    thanks,
    David

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 3:26 PM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    David,

    The viewstate error occurs if you click either "Cancel" or "Update" when editing a record.

    Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml.Linq;
    using System.Web.DynamicData;
    
    public partial class Edit : System.Web.UI.Page {
        protected MetaTable table;
    
        protected void Page_Init(object sender, EventArgs e) {
            DynamicDataManager1.RegisterControl(DetailsView1);
        }
    
        protected void Page_Load(object sender, EventArgs e) {
            table = DetailsDataSource.GetTable();
            Title = table.DisplayName;
            DetailsView1.RowsGenerator = new FieldManager(table);       
        }
    
        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) {
            if (e.CommandName == DataControlCommands.CancelCommandName) {
                Response.Redirect(table.ListActionPath);
            }
        }
    
        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e) {
            if (e.Exception == null) {
                Response.Redirect(table.ListActionPath);
            }
        }
    }
    

     

    using System;
    using System.Data;
    using System.Collections;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Web.DynamicData;
    
    public class FieldManager : IAutoFieldGenerator
    {
    	protected MetaTable _table;
    
    	public FieldManager(MetaTable table)
    	{
    		_table = table;
    	}
    
    	public ICollection GenerateFields(Control control)
    	{
    		List oFields = new List();
    
    		foreach (MetaColumn col in _table.Columns)
    		{
    			if (col.Name == "Memo")
    				continue;
    
    			DynamicField f = new DynamicField()
    			{
    				DataField = col.Name
    			};
    
    			oFields.Add(f);
    		}
    
    		return oFields;
    	}
    
    }
    
     
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 3:45 PM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    David,

    The DynamicReadonlyField worked perfectly!

    Now if I can get past the viewstate error when editing, I can do the following:

    do case

         case Role is Admin

              Generate Fields that Admin should see

         case Role is User

              Gererate Fields that User should see (some of them will be read only)

         and so on... 

    end case

    Thanks for your help!

    Tommy

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 3:50 PM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    Steve,

    Did you actually try to do an update or cancel from an Edit page?

    I've regenerated everything and I still have the problem. The page comes up ok, but when you click the update or cancel button the error is generated. 

    Thanks,

    Tommy

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 4:56 PM
    • Loading...
    • scothu
    • Joined on 12-10-2007, 1:54 AM
    • Redmond, WA
    • Posts 193
    • AspNetTeam

    Move the call to        DetailsView1.RowsGenerator = new FieldManager(table);     to your Page_Init method AFTER the RegisterControl call and it should work in Insert/Update model.

    ...Scott

    Scott Hunter
    PM, ASP.NET Team, Microsoft
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 5:40 PM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 1:11 PM
    • Newton-le-Willows, UK
    • Posts 834

    I can't believe I did'nt spot that as I noted that westmich had it there in an earlier post!!!

    It now works in ALL paged when updating.

    Big Smile

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
    Oh, and don't forget to mark as answer posts that answer your question Big Smile
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 6:36 PM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    Scott,

    That fixed it! Edits now work correctly!

    Sorry I took so long to get back to you. For some reason I'm now having trouble with a page that contains a foreign key which does not show correctly with the dropdown. I've even tried putting UIHint("ForeignKey") attribute in the Metafile class. This causes a run time error saying that the field is not a foreign key. There is a foreign key relationship defined. I did rename the fields in both the primary table and the foreign key table before this problem started happening. I have since deleted the fields, added them back, and recreated the relationship, but the problem still exists.

    I don't think the two things are related; however, I'll let you know what I find.

    Tommy

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 6:42 PM
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 974
    • AspNetTeam

    Glad you got this working Tommy!  Let's follow up on your new issue in a new thread as this one is getting long :)

    David

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 6:44 PM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 1:11 PM
    • Newton-le-Willows, UK
    • Posts 834

    Regarding that Foreign Key issue

    tlanier:

    For some reason I'm now having trouble with a page that contains a foreign key which does not show correctly with the dropdown. I've even tried putting UIHint("ForeignKey") attribute in the Metafile class. This causes a run time error saying that the field is not a foreign key.

     I had a similar thing in this post  Problem with custom pages using ForeignKey.ascx & ForeignKey_Edit... and David said that you use the entity name not the key name:

    i.e. Shippers not ShipVia.

    Asuming your trying to do what i was doing.

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
    Oh, and don't forget to mark as answer posts that answer your question Big Smile
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 6:44 PM
    • Loading...
    • scothu
    • Joined on 12-10-2007, 1:54 AM
    • Redmond, WA
    • Posts 193
    • AspNetTeam

    One thing to always check in these cases is that your Linq to SQL classes match your database. If you create your Linq to SQL clasees and then go and modify the database the Linq to SQL classes will not be automatically updated. Dynamic Data looks at the Linq to SQL classes to figure things out. You might just want to open it up in the designer, delete all the tables, and re drag them in from the database.

    Scott Hunter
    PM, ASP.NET Team, Microsoft
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 6:49 PM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    Yes, I did recreate the Linq to Sql classes (several times...).

    I just got it working. The solution was to reboot my computer. Magically the foreign key is now working correctly.

    Tommy

Pag