C# FormView EditTemplate 如何在底层更新 ObjectDataSource UpdateParameters 中的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/764570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:14:59  来源:igfitidea点击:

How does FormView EditTemplate update values in ObjectDataSource UpdateParameters under the hood?

c#asp.netobjectdatasourceformview

提问by Leo Nix

I have a FormView bound to an ObjectDataSource.

我有一个绑定到 ObjectDataSource 的 FormView。

* ObjectDataSource definition (omitted portion of it for simplicity)*

* ObjectDataSource 定义(为简单起见省略了部分)*

<asp:ObjectDataSource 
    ID="odsHousehold" 
    runat="server"
    TypeName="BLL.Households"
    ConflictDetection="OverwriteChanges"
    UpdateMethod="UpdateHousehold" 
    >
    <UpdateParameters>
        <asp:Parameter Name="sName" Type="String" Direction="Input" />
        <asp:Parameter Name="sAddress" Type="String" Direction="Input" DefaultValue="" />
        <asp:Parameter Name="sCity" Type="String" Direction="Input" DefaultValue="" />
        <asp:Parameter Name="sState" Type="String" Direction="Input" DefaultValue="" />
        <asp:Parameter Name="sZip" Type="String" Direction="Input" DefaultValue="" />
    </UpdateParameters>
</asp:ObjectDataSource>

* FormView definition (omitted portion of it for simplicity) *

* FormView 定义(为简单起见省略了部分)*

   <asp:FormView 
    ID="fvHousehold"
    runat="server"
    DataKeyNames="HouseholdID"
    DataSourceID="odsHousehold"
    HorizontalAlign = "Left"
 >
<EditItemTemplate>
<asp:TextBox ID="txtHouseHoldName" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("HouseholdName") %>'></asp:TextBox>
<asp:TextBox ID="txtAddress" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("Address") %>'></asp:TextBox>
<asp:TextBox ID="txtCity" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("City") %>'></asp:TextBox>
<asp:TextBox ID="txtState" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("State") %>'></asp:TextBox>
<asp:TextBox ID="txtZip" runat="server" MaxLength="50" Width="100%" Text='<%# Bind("Zip") %>'></asp:TextBox>
 <asp:Button ID="btnUpdateHousehold" runat="server" Text="Update" CommandName="Update" />
</EditItemTemplate>
</asp:FormView>

I'd like to know: how does the FormView know which UpdateParameter to populate with which EditTemplate TextBox when the Update button is clicked?
For instance, I haven't instructed "txtAddress" in the FormView to populate the UpdateParameter "sAddress" but InputParameters["sAddress"] contains the Text value of txtAddress. How does it know to do that?

Could any guru enlighten me?

我想知道:当单击更新按钮时,FormView 如何知道用哪个 EditTemplate TextBox 填充哪个 UpdateParameter?
例如,我没有指示 FormView 中的“txtAddress”填充 UpdateParameter“sAddress”,但 InputParameters["sAddress"] 包含 txtAddress 的文本值。它怎么知道这样做?

有哪位大师能开导我吗?

Thank you so much,

非常感谢,

Cullen

卡伦

回答by antscode

Perhaps it is simply the order in which the TextBox controls are added to the EditItemTemplate? i.e. the order of the controls must match the order of the UpdateParameters...

也许这只是将 TextBox 控件添加到 EditItemTemplate 的顺序?即控件的顺序必须与 UpdateParameters 的顺序匹配...

Try swapping the position of txtHouseHoldName and txtAddress, does the address get passed into the sName parameter of your update method?

尝试交换 txtHouseHoldName 和 txtAddress 的位置,地址是否会传递到更新方法的 sName 参数中?

回答by antscode

I have a post on my blog with a detailed discussion of how Bind() works at http://www.aarongoldenthal.com/post/2009/03/15/ASPNET-Databinding-Bind()-Method-Dissected.aspx .

我在我的博客上有一篇文章详细讨论了 Bind() 如何工作,网址http://www.aaronoldenthal.com/post/2009/03/15/ASPNET-Databinding-Bind()-Method-Dissected.aspx

回答by antscode

"how does the FormView know which UpdateParameter to populate with which EditTemplate TextBox when the Update button is clicked?"

“当单击更新按钮时,FormView 如何知道用哪个 EditTemplate TextBox 填充哪个 UpdateParameter?”

I believe the simple answer is: it knows because of the Bind statements you put in the TextBox controls. E.g. txtAddress has "Bind("Address")" so when the update is called, it has a connection between txtAddress and parameter "Address"

我相信简单的答案是:它知道是因为您在 TextBox 控件中放置的 Bind 语句。例如,txtAddress 有“Bind("Address")”,所以当更新被调用时,它在 txtAddress 和参数“Address”之间有一个连接