开发者

Binding and updating object via formview

I want to bind an object to a form view.

<asp:FormView ID="formview" runat="server" DefaultMode="Edit" OnItemUpdating="formview_ItemUpdating">
    <EditItemTemplate>
      <ol>
         <li>
            <label class="leftCo">First</label>
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("First")%>'></asp:TextBox>
         </li>
         <li>
            <label class="leftCo">Second</label>
            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Second")%>'></asp:TextBox>
         </li>
     </ol>
     <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>         
   </EditItemTemplate>
</asp:FormView>

    var objects = new List<OB> { new OB { First = "1111", Second = "2222" } };
    formview.DataSource = objects;
    formview.DataBind();

Now, is it possible to generate a new object on update, without g开发者_Go百科etting and reading each textbox with findcontrol?

When I click on update button I want to create OB object with updated values, and lets say, pass it to some method (within updating event or so).


Try binding to an ObjectDataSource like:

<asp:FormView ID="formview" runat="server" DefaultMode="Edit" OnItemUpdating="formview_ItemUpdating" DataSourceID="dsrcOB">
    <EditItemTemplate>
        <ol>
            <li>
            <label class="leftCo">First</label>
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("First")%>'></asp:TextBox>
            </li>
            <li>
            <label class="leftCo">Second</label>
            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Second")%>'></asp:TextBox>
            </li>
        </ol>
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>         
    </EditItemTemplate>
</asp:FormView>

<asp:ObjectDataSource ID="dsrcOB" runat="server" 
    SelectMethod="GetOB" UpdateMethod="UpdateOB" 
    DataObjectTypeName="TestWeb.OB" TypeName="TestWeb.OBDal">
</asp:ObjectDataSource>

Then, create a class that matches the type referenced by your ObjectDataSource:

public class OBDal
{
    public OB GetOB()
    {
        return new OB() { First = "1111", Second = "2222" };
    }

    public void UpdateOB(OB ob)
    {
        // do something here
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜