一个MVC家伙试图理解asp.net Web表单(数据绑定)

本文关键字:Web net 表单 数据绑定 asp MVC 一个 | 更新日期: 2023-09-27 18:07:13

我被扔在这个使用Asp.net Webforms的项目上。

我正在尝试理解数据绑定是如何工作的。

假设我输入关于用户注册的表单数据。当我提交时,我能以某种方式将输入绑定到模型吗?

我读过关于绑定到数据集,可以是XML。我是否需要将它反序列化为某种实体?

一个MVC家伙试图理解asp.net Web表单(数据绑定)

尽量避免使用数据集。使用强类型对象代替。如果你确实使用数据集,它们已经很好地序列化了。这里有一个粗略的例子来帮助你。这将从文本框中提取值,并将列表视图绑定到集合。我希望这对你有帮助。

步骤1:创建对象&视图模型

 [Serializable]
public class Person
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public Person(String FirstName, String LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
    }
    public Person()
        : this(String.Empty, String.Empty)
    {
    }
}
[Serializable]
public class ViewModelBase
{
}
[Serializable]
public class SomePageViewModel : ViewModelBase
{
    private Person currentperson;
    public Person CurrentPerson
    {
        get
        {
            if (currentperson == null)
            {
                currentperson = new Person();
            }
            return currentperson;
        }
        set
        {
            currentperson = value;
        }
    }
    private List<Person> persons;
    public List<Person> Persons
    {
        get
        {
            if (persons == null)
            {
                persons = new List<Person>();
            }
            return persons;
        }
        set
        {
            persons = value;
        }
    }
    public SomePageViewModel()
    {
    }
    public void RegisterPerson(String FirstName, String LastName)
    {
        Persons.Add(new Person(FirstName,LastName));
    }
    public void GetPersons()
    {
        /*Get Persons from database*/
    }
}

步骤2:在你的代码文件(PageName.aspx.cs)中实例化ViewModel

 /// <summary>
    /// Lazy initialized and persisted in session
    /// </summary>
    public SomePageViewModel ViewModel
    {
        get
        {
            if (Session["SomePageViewModel"] == null)
            {
                Session.Add("SomePageViewModel", new SomePageViewModel());
            }
            return Session["SomePageViewModel"] as SomePageViewModel;
        }
        set
        {
            if (value == null)
            {
                if (Session["SomePageViewModel"] != null)
                {
                    Session.Remove("SomePageViewModel");
                }
            }
            else
            {
                Session["SomePageViewModel"] = value;
            }
        }
    }

步骤3:添加标记到ASPX

   <asp:TextBox runat="server" ID="txtFirstName" ValidationGroup="vgrpRegister"  />
<asp:TextBox runat="server" ID="txtLastName" ValidationGroup="vgrpRegister" />
<asp:Button runat="server" ID="btnRegister" Text="Register"  ValidationGroup="vgrpRegister"/>
<asp:ListView runat="server" ID="lvPersons" ItemPlaceholderID="phPersons" DataSource='<%#ViewModel.Persons%>'>
    <LayoutTemplate>
        <table id="tblPersons" border="1">
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                </tr>
            </thead>
            <tbody>
                <asp:PlaceHolder runat="server" ID="phPersons"></asp:PlaceHolder>
            </tbody>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td><%#Eval("FirstName") %></td>            
            <td><%#Eval("LastName") %></td>
        </tr>
    </ItemTemplate>
</asp:ListView>

步骤4:CodeBehind Events

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnRegister.Click += new EventHandler(btnRegister_Click);
    }
    void btnRegister_Click(object sender, EventArgs e)
    {
        this.ViewModel.RegisterPerson(txtFirstName.Text, txtLastName.Text);
        this.lvPersons.DataBind();
    }