欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-11 12:07:25
xml内容如下:复制代码 代码如下: ...
xml内容如下:
复制代码 代码如下:

<?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 style="white-space:nowrap">
<asp:dropdownlist id="ddlversion" 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;
}
}
}