c# - 如何绑定值以形成控件
问题描述
我正在尝试将值绑定到 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 />
我从一些教程中得到了这个,但由于某种原因,下拉列表仍然是空白的,无法弄清楚为什么。
解决方案
尝试改变
Page.IsPostBack
to !Page.IsPostBack
!Page.IsPostBack 本质上是页面第一次加载
推荐阅读
- linux - 如何从第二行使用 awk '{print $1*Number}' 或告诉他忽略 NaN 值?
- python - PYTHON - Itertools.combinations 导致内存错误
- ios - 当 Swift 闭包暴露给 Objective-C 时如何自动生成参数名称?
- python - 两个函数相等布尔值在python中无法正常工作
- c# - 当用户在 C# 中取消操作时,如何停止从 Oracle db 查询数据?
- r - 在 R 中输入 NA BINARY SEX 数据
- terraform - Terraform:引用输出变量时不支持的属性
- powershell - 如何在工作流中访问 InLineScript 中的 ForEach 变量?
- excel - 当范围是动态的时返回多个匹配项
- c++ - 在 OSX mac 中使用 QT6 和 Opencv 架构 x86_64 的未定义符号