首页 > 解决方案 > 如何绑定值以形成控件

问题描述

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

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

XML:注册.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 />

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

标签: c#asp.netxmlwebforms

解决方案


尝试改变

Page.IsPostBack

to !Page.IsPostBack

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


推荐阅读