简体   繁体   中英

Insert Custom value on Cell in ASP.NET Dynamic Data

I have a Dynamic Data linq to sql Website where I need to assign values on an specific cell in the insert or update pages. I have tried in the pageload of the Edit template

table.Columns[1].DefaultValue = User.Identity.Name;

but as is a metatable it is readonly.

Help...

To change the metadata, you have to add some attributes to the model class properties (you can find them in the DataContext generated classes if you use LinqToSql).

class User
{
  [DefaultValue("The default name")]
  string Name {get;set;}
}

But unfortunaly it will not be used by default by the dynamic data field templates, so you'll have to edit the templates to use the DefaultValue property, Exemple in the Page_Load of the TextEdit template:

if (!IsPostBack)
{
  if (Mode == DataBoundControlMode.Insert && Column.DefaultValue != null)
  {
      TextBox1.Text = Column.DefaultValue.ToString();
  }
}

I know this is an old post, but this could help others in solving their problem.

You can use this:

public partial class BasicModelDataContext : DataContext
{
        partial void InsertEmployee(Employee instance)
        {
            instance.MyValue = "NEW VALUE";
            Employee.Insert(instance);
        }

        partial void UpdateEmployee(Employee instance)
        {
             instance.MyValue = "NEW Update VALUE";
             Employee.Update(instance);
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM