当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net 用户控件读取以及赋值

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

英文字库,实训总结范文,dbl限高篮球赛

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;
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网