如何将值绑定到窗体控件

时间:2018-11-01 13:53:25

标签: c# asp.net xml webforms

我正在尝试将值从XML文档绑定到ASP.NET表单控件。我的最终目标是根据用户在ID下拉列表中选择的值来预先填充表单控件。从那里,用户将能够编辑值并将更改保存回XML文档。

我的方法是尝试将每个xml id标记值绑定到下拉列表。到目前为止,我已经提出了这个建议。

XML:registrations.xml

 <registrations>
      <Registration>
         <id>1</id>
         <fullName>Keiran Bernal</fullName>
         <emailAddress>k.bernal@gmail.com</emailAddress>
         <registrationType>Conference only</registrationType>
         <attendingSocialEvent>Yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>2</id>
         <fullName>Cordelia Pierce</fullName>
         <emailAddress>c.pierce@outlook.com</emailAddress>
         <registrationType>Conference and Dinner</registrationType>
         <attendingSocialEvent>Yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>3</id>
         <fullName>Zachery Guy</fullName>
         <emailAddress>z.guy@yahoo.com</emailAddress>
         <registrationType>Conference only</registrationType>
         <attendingSocialEvent>Yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>4</id>
         <fullName>Kiana Hawworth</fullName>
         <emailAddress>k.hawworth@bigpond.com</emailAddress>
         <registrationType>-</registrationType>
         <attendingSocialEvent>No</attendingSocialEvent>
      </Registration>
   </registrations>

隐藏代码:edit.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        DataSet dsEdit = new DataSet();
        dsEdit.ReadXml(Server.MapPath("~/registrations.xml"));
        dllIdEdit.DataTextField = "fullName";
        dllIdEdit.DataValueField = "id";
        dllIdEdit.DataSource = dsEdit;
        dllIdEdit.DataBind();
    }
}

ASP.NET Web表单:edit.aspx

 <%--ID--%>
 <asp:Label ID="lblIdEdit" runat="server" Text="ID" CssClass="editLbl"></asp:Label>
 <asp:DropDownList ID="dllIdEdit" runat="server" CssClass="editDdl"></asp:DropDownList><br />

我是从一些教程中得到的,但是由于某种原因,下拉列表仍然空白,无法弄清原因。

1 个答案:

答案 0 :(得分:1)

尝试更改

Page.IsPostBack

to !Page.IsPostBack

!Page.IsPostBack本质上是页面第一次加载