简体   繁体   English

在ASP.NET动态数据中的单元格上插入自定义值

[英]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. 我有一个动态数据linq到sql网站,我需要在插入或更新页面中的特定单元格上分配值。 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. 但作为一个metatable它是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). 要更改元数据,必须向模型类属性添加一些属性(如果使用LinqToSql,可以在DataContext生成的类中找到它们)。

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: 但不幸的是,动态数据字段模板默认不会使用它,因此您必须编辑模板以使用TextEdit模板的Page_Load中的DefaultValue属性Exemple:

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);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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