locked
auto-generate next value on insert page RRS feed

  • Question

  • User-636272623 posted

    Hello,

    I have a table in my dynamic data site where i'd like to pre-populate a string field on the insert page.The tricky part is that the value I'd like to pre-populate the field with is calculated from the last record that was inserted into that table. So I need to know how I can query the table and retreive a value in the Page_Init sub routine of the Insert page template. Help would be much appreciated.

    John

    Saturday, December 1, 2012 1:21 AM

Answers

  • User-330204900 posted

    here is  some sample code:

    protected void Page_Init(object sender, EventArgs e)
    {
        table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
        var defaultValues = table.GetColumnValuesFromRoute(Context);
        defaultValues.Add("Column1", 23);
        defaultValues.Add("Column2", "A Default Value");
        FormView1.SetMetaTable(table, defaultValues);
        DetailsDataSource.EntityTypeFilter = table.EntityType.Name;
    }

    I have added a temporary variable "defaultValues" to get the collection from the route then I add a couple of values and then pass the collection to the FormView using the SetMetaTable extension method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2012 5:48 AM
  • User-330204900 posted

    you would need to use Linq or you favoirite method and you would need to create a custom page for each entity type you wanted to do this with.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2012 4:51 PM

All replies

  • User-330204900 posted

    This woudl not be too hard you could this in the data source's inserted event and pass the value as parameter the fiedl should then get the value auto matically but you will need to recall the insert page with the parameter int he query string. This works in the same way the if you go to the list page and then select some filters and then click insert those filter value will be passed int he query string then set as a defaul value.

    Saturday, December 1, 2012 6:50 PM
  • User-636272623 posted

    But what about on the initial insert, i want the insert form to be prepopulated when the user first opens up the page. Would I do that in the page_intit() and query the datatable somehow?  I guess my other question is, how do access my datatables in the same manner i would do with say a sqldatareader for example?

    Monday, December 3, 2012 5:25 PM
  • User-330204900 posted

    The same applies if you populate the Default values collection before you call the Insert page those values will be passed to the form or you could populate the collection in ttheINSrt pages Page_Init before it's passed to the FormView.

    Tuesday, December 4, 2012 5:34 AM
  • User-330204900 posted

    here is  some sample code:

    protected void Page_Init(object sender, EventArgs e)
    {
        table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
        var defaultValues = table.GetColumnValuesFromRoute(Context);
        defaultValues.Add("Column1", 23);
        defaultValues.Add("Column2", "A Default Value");
        FormView1.SetMetaTable(table, defaultValues);
        DetailsDataSource.EntityTypeFilter = table.EntityType.Name;
    }

    I have added a temporary variable "defaultValues" to get the collection from the route then I add a couple of values and then pass the collection to the FormView using the SetMetaTable extension method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2012 5:48 AM
  • User-636272623 posted

    I understand what you are saying, but the default values I want to set are derived from existing values in the database. How do I query the database to retreive these values?

    Tuesday, December 4, 2012 12:12 PM
  • User-330204900 posted

    you would need to use Linq or you favoirite method and you would need to create a custom page for each entity type you wanted to do this with.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2012 4:51 PM