如何更改DetailsView插入返回的参数?

时间:2013-09-25 10:12:31

标签: c# asp.net detailsview

我有一个DetailView返回插入参数:

<InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Capital" Type="String" />
            <asp:Parameter Name="Currency" Type="String" />
            <asp:Parameter Name="ISOCode" Type="String" />
</InsertParameters>

方法:

public void AddCountry(string Name, string Capital, string Currency, string ISOCode)
        {
           CountyList.Add(new Country(Name, Capital, Currency, ISOCode)));
        }

但是如何强制DetailsView返回自定义类?

public void AddCountry(Country country)
        {
            CountyList.Add(country);
        }

2 个答案:

答案 0 :(得分:1)

您强制使用以下属性返回类: TypeName ObjectDataSource App_Code。然后无需指定任何插入参数。

Country.cs文件夹中创建一个类,声明各个字段可公开访问。 例如:

类文件public class Country { public Country() { } private string name; public string Name { get { return name; } set { name= value; } } private string capital; public string Capital { get { return capital; } set { capital= value; } } private string currency; public string Currency { get { return currency; } set { currency= value; } } private string iSOCode; public string ISOCode { get { return iSOCode; } set { iSOCode= value; } } } ,声明字段

AddCountry()

现在,在您的插入方法:Class_variable_Name.Public_Field_Name中,只需传递上述类的实例,如下所示。使用public void AddCountry(Country country) { // a sample code to access the values from Class instance passed in this method string connString = WebConfigurationManager.ConnectionStrings["MyConnections"].ConnectionString; SqlConnection con = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "Insert into Country(Name, Capital,Currency,ISOCode) values(@Name,@Capital,@Currency,@ISOCode)"; cmd.Connection = con; // Add the insert parameters cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 20)); // specify the values for insert parameters // using the Country class passed as parameter to this method cmd.Parameters["@Name"].Value = country.Name; // country.Public_Field_Name cmd.Parameters.Add(new SqlParameter("@Capital", SqlDbType.NVarChar, 20)); cmd.Parameters["@Capital"].Value = country.Capital; cmd.Parameters.Add(new SqlParameter("@Currency", SqlDbType.NVarChar, 20)); cmd.Parameters["@Currency"].Value = country.Currency; cmd.Parameters.Add(new SqlParameter("@ISOCode", SqlDbType.NVarChar, 20)); cmd.Parameters["@ISOCode"].Value = country.ISOCode; con.Open(); // execute the Insert Command cmd.ExecuteNonQuery(); } 访问字段值,如下所示。

ObjectDataSource

显然,在TypeName控件中,您需要指定属性:DataObjectTypeNameAddCountry()

1。) DataObjectTypeName 表示包含数据访问方法的类的名称,表示包含方法的类文件:ObjectDataSource

2。) TypeName :获取或设置update控件用于insert中的参数的类的名称,{{1 },或delete数据操作,而不是从数据绑定控件传递单个值。在我们的例子中,这是上面创建的Country类。

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
   DataObjectTypeName="Country"
   InsertMethod="AddCountry"
   TypeName="CountryAccessMethods" <!-- assuming you have defined your Insert,
  Update,Delete methods inside a class file named `CountryAccessMethods` -->
</asp:ObjectDataSource> 

答案 1 :(得分:1)

Here就是答案。

  

在业务对象中,创建具有长控制值一对一映射到数据存储值的长参数列表的方法可能会导致代码不易重复使用。更好的做法是将数据封装在自定义类中,然后将类的实例作为参数传递。这样,构成类实例的数据(例如员工记录)可以更改,而无需对数据源对象公开的公共接口进行任何更改。

     

虽然您无法将参数类型设置为自定义类的名称,但您可以将ObjectDataSource控件的DataObjectTypeName属性设置为自定义用户定义类型的名称,例如NorthwindEmployee类,然后传递实例类型的业务对象数据方法。要将用户定义的对象传递给数据源对象,必须满足以下条件:

     
      
  • 用户定义的类型必须具有默认构造函数(不带参数的构造函数)。

  •   
  • 用户定义的类型必须定义公共属性,其名称与从数据绑定控件(如GridView和DetailsView)传递给数据源控件的字典条目的名称相匹配。有关这些词典的详细信息,请参阅使用带数据源控件的参数。

  •   
  • 数据源对象的公共属性必须同时公开get和set访问器。

  •   

所以,在我的情况下,只有当object的公共属性同时暴露get和set访问器时,我才能传递Countries对象。只有getter是公共的,并且setter通过构造函数初始化。这是无法做到的事情:通过对象构造函数传递值。 最后,问题出在其他地方:在ObjectDataSource中不在DetailsView组件中。

相关问题