Is there an Attribute that hides a property from being bound to a gridview in asp.net? - asp.net

I have an object that I set as the datasource for a gridview - this works fine, I get a nice table on the page with a column for each public property.
But - I always want to hide one of the columns (but still need it available as a public property.
I'm using a clunky hide-column-on-row-created fix for now, but am looking for a better solution, like an attribute applied to the property to hide it from databinding.
Apparently this exists in winforms:
[Browsable(false)] // this stops Type from showing up in databound controls
public string Type { get; set; }
public string Description { get; set; }
Can anyone suggest a similar solution for ASP.NET?
Update:
I marked Rex M's answer as correct, because it answers the question, but if anyone else is interested in how to do this:
What eventually worked for me was to mark the property corresponding to the column I wanted to hide as internal instead of public.

Looking at the reflected code for GridView.CreateAutoGeneratedColumns(PagedDataSource dataSource), it appears there are not any checks for attributes when it is scraping the datasource for properties. So, apparently not.

Related

Programming Strategy for ASP.NET MVC 5 - Updating a Child or Related Records on Same Page

I am struggling a bit with strategies that allow for updating child records related to a parent record in MVC in the same view. Take for instance model classes described below.
public class Person {
public Person() {}
int personId{get;set;}
string personName{get;set}
public virtual ICollection<Phone> Phones { get; set; }
}
public class Phone {
public Phone() {}
public int PhoneId {get;set;}
public string PhoneType {get;set;}
public string Number {get;set;}
public string Ext {get;set;}
}
Providing a single page view allows for Creating,Updating,Deleting a person easily.
Adding actions that jump to views designed specifically for the phone records and then return the user to the person record is also straightforward.
What is the best strategy for displaying the related phone records for the person in the same view as the person information and allowing for the CRUD operations to be used against only the phone entities in the same view?
If the answer is that AJAX is the best approach then that is perfectly acceptable. I am looking for feedback from the community that describes what individuals have used in this type scenario in their projects that have this type of challenge.
There was one approach listed here:
http://www.codeproject.com/Articles/786085/ASP-NET-MVC-List-Editor-with-Bootstrap-Modals
That had a decent if somewhat cumbersome approach.
I have to believe that this is a much simpler problem and that I am quite literally missing the obvious.
I've done in-line editing by displaying the current phone information and having hidden inputs. When the user clicks the edit button, I toggle visibility to hide the display and show the inputs.
I've also done similar to the article. Have a modal popup to edit phone numbers, after saving the changes and closing the modal refresh the partial with the phone number list.
In either scenario I'm just submitting an ajax request and return a partial that I use to replace the div that contained the original information.
The modal dialog seems to be little more "modern".

ScaffoldColumn(false) not working because it is a FK

I am using ASP.NET Dynamic Data. The page dynamically creates a List, Edit, Details pages to display the records stored in the table in the DB.
I have the Sub Contractors in the table but I want its visibility set to false on screen.
I've tried
[HideColumnIn(PageTemplate.List)]
[Display(Name = "SubContractor Id", Order = 70)]
public object SubContractorId { get; set; }
ALSO:
[ReadOnlyColumnIn(PageTemplate.List)]
But no joy. Iv made these changes in the database.cs file.
Anyone know how can I make it disappear or remove the hyperlink from it?
EDIT:
[ScaffoldColumn(false)] usually works but because SubContractorId is a FK it seems to not take effect.
Consider the possibility of using Custom pages (List and other) for your Dynamic Data site which allows you hide specific fields.
For more information: How to: Customize the Layout of an Individual Table By Using a Custom Page Template

ASP.Net Custom Control - Override "Text" Property

This would seem to be very simple, but it's not working for me. I desparately need to know how to override the "Text" property of a server control I created, which inherits from "Label". When the control is dropped into an ASP Web form, I want the text property to already be set to a certain value. I tried:
[Browsable(true), Bindable(true), Category("Behavior"), Localizable(true)]
[DefaultValue("00:00:00")]
public override string Text{get; set;}
But it doesnt work; the "Text" property shows up blank - and when I try to edit it, I can change it to anything except the value specified in the "DefaultValue" attrib. This property is supposed to be overridable.
I also need to be able to set the "ID" property so it's set to a specific value when dropped on the form. Is this possible?
Any suggestions would be greatly appreciated!
For this you can use the ToolBoxDataAttribute:
Specifies the default tag generated for a custom control when it is
dragged from a toolbox in a tool such as Microsoft Visual Studio.
By default, the visual designer of a tool such as Visual Studio,
creates an empty tag. This is a tag representing a control in its
default state, when the control is dropped from the toolbox of a
visual designer onto the design surface. To specify initial default
values, a control can make use of this attribute. You can use this
attribute to customize the initial HTML content that is placed in the
designer when the control is dragged from the toolbox onto the form.
It would look something like this for your control:
[ToolBoxData("<{0}:TimeLabel ID='TL1' Text='00:00:00' runat='server' />")]
public class TimeLabel : System.Web.UI.WebControls.Label
{
[DefaultValue("00:00:00")]
public override string Text
{
get { return ViewState["Text"] != null ? (string)ViewState["Text"] : "00:00:00"; }
set { ViewState["Text"] = value; }
}
}
The DefaultValue attribute is intended to display a default value in Properties Tab.
So when you launch website without specify a value for your Text property .net uses this one.
IMHO you have not chance to do that. Sorry!

Custom ListView to show EmptyDataTemplate and InsertItemTemplate at the same time

It is known that the ListView control can't display both an EmptyDataTemplate and a InsertItemTemplate at the same time.
For my design style I need to be able to show both. I want to be able to show that no data exist and at the same time show a form to add new data.
I've already implemented various solutions, such as putting a PlaceHolder in my LayoutTemplate and then manually showing or hiding this PlaceHolder in the code-behind, depending on if there is data or not.
However, I would like a control that has this built-in capability in order to keep my code-behind light.
I believe there are only two ways to achieve what I want:
First way (preferred) is to write that custom control myself. I was thinking of deriving from ListView and overriding the function responsible for disabling the EmptyDataTemplate, but I have no experience with custom controls. And I'm not even sure it will work in the end.
Second way is to use a custom control found or purchased somewhere. I have not been able to find such control that has the same base capabilities as the ListView.
Has anybody any idea how to solve #1 and maybe #2?
Thank you.
Here is what I ended up doing:
public class MyListView : ListView
{
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
int itemCount = base.CreateChildControls(dataSource, dataBinding);
if (this.InsertItemPosition != InsertItemPosition.None && itemCount == 0)
{
CreateEmptyDataItem();
}
return itemCount;
}
}
Works great!
I would go for your option 1: Create a custom control
Because you haven't specified a programming language I made one in VB.NET:
Public Class CustomListView
Inherits ListView
Public Sub CheckEmptyData() Handles Me.PreRender
If Me.Items.Count = 0 Then
Dim label As New Label
label.Text = "No data found <br/>"
Me.Controls.AddAt(0, label)
End If
End Sub
End Class
Just tested it and works perfectly, it can just replace an existing ListView.
As you can see it checks if there is any data and if not it inserts a label with the text "No data found". I haven't found an easy way to use the EmptyDataTemplate for this, that would be a better option but this might already work for you.
Another option is to hide the InsertItem (InsertItemPosition.None) if there is no data, and add a Button "Insert" to the EmptyDataTemplate that enables the InsertItemTemplate and therefore hides the EmptyDataTemplate.
I don't understand much of your requirement without a screen shot of what you are actually trying to achieve. Anyway, you may be able to achieve this interface with a combination of ListView+FormView or ListView+ a User Control. If you can provide any more info I may help further.

Binding a radgrid to a tree like data structure

I have a structure that looks following
Class TreeNode {
public TreeNode Parent { get; }
public IEnumerable<TreeNode> Children { get; }
public . . . .
}
I want to bind this to a asp.net telerik radgrid with detail tables for each level of the children. I know that radgrid supports declarative binding to a self referencing hierarchy using a datasource control but that assumes you have a flat dataset (e.g. from a database) and can look at a parent key field of some sort. My question has anyone bound a radgrid to a data structure like this before and is there a way to do it declaratively or mostly declaratively?
I can't answer your question directly since I've no experience with the telerik grid, but I can offer a work around.
Add a method to your class that flattens its contents into a self-referencing table like structure such as the one you mentioned. This way you can still work with the class and it's more advanced structure like you want to, but can still output the contents in a consumable form that matches the expectations of the UI components.
This is also a good candidate for an extension method too.
Another possible way I can think of is to build the hierarchy dynamically by filtering the date for the child tables based on their parent inside the DetailTableDataBind grid handler. This demo is a good reference:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx
but I must agree that Stephen's suggestion might be more handy.
Dick

Resources