站长网 Asp教程 asp.net 用户控件读取以及赋值

asp.net 用户控件读取以及赋值

?xml version=”1.0″ encoding=”utf-8″ ? SystemVersion Item Version_ID1/Version_ID Version_NameCN/Version_Name /Item Item Version_ID2/Version_ID Version_NameEN/Version_Name /Item /SystemVersion 用户控件的关键代码: SystemVersion.ascx 复制代


<?xml version=”1.0″ encoding=”utf-8″ ?>
<SystemVersion>
<Item>
<Version_ID>1</Version_ID>
<Version_Name>CN</Version_Name>
</Item>
<Item>
<Version_ID>2</Version_ID>
<Version_Name>EN</Version_Name>
</Item>
</SystemVersion>


用户控件的关键代码:
SystemVersion.ascx

复制代码 代码如下:


<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”SystemVersion.ascx.cs” Inherits=”UserControls_SystemVersion” %>
<!– Value是传入的值 –>
<div>
<asp:DropDownList runat=”server”>
</asp:DropDownList>
</div>


后台文件:

复制代码 代码如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Xml.Linq;

public partial class UserControls_SystemVersion : System.Web.UI.UserControl
{
private const string CON_FilePath = “~/App_Data/sysVersion.xml”;

//// <summary>
/// 下拉框赋值
/// </summary>
public string Value
{
set { ViewState[“Value”] = value; }
get { return ViewState[“Value”] == null ? null : ViewState[“Value”].ToString().Trim(); }
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DdlBind();
}
}

public void DdlBind()
{
XElement xDoc = XElement.Load(Server.MapPath(CON_FilePath));

// Create the query
var lVersion = from c in xDoc.Descendants(“Item”)
where c.Element(“Version_ID”).Value == “1” //目前只显示CN
select new
{
Version_Name = c.Element(“Version_Name”).Value,
Version_ID = c.Element(“Version_ID”).Value
};

ddlVersion.DataSource = lVersion.ToList();
ddlVersion.DataTextField = “Version_Name”;
ddlVersion.DataValueField = “Version_Name”;
ddlVersion.DataBind();
if (Value != null)
{
ddlVersion.SelectedValue=Value;
}
}
}

本文来自网络,不代表站长网立场,转载请注明出处:https://www.tzzz.com.cn/html/jc/asp/2021/0722/14103.html

作者: dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部