Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 878
Francisco Rivera Avatar asked Sep 13 '26 15:09

Francisco Rivera


2 Answers

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();
  }
}
like image 62
Guillaume86 Avatar answered Sep 16 '26 03:09

Guillaume86


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);
        }
}
like image 36
Roman Avatar answered Sep 16 '26 04:09

Roman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!