手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >asp.net 用户控件读取以及赋值
asp.net 用户控件读取以及赋值
摘要:XML内容如下:复制代码代码如下:1CN2EN用户控件的关键代码:SystemVersion.ascx复制代码代码如下:后台文件:复制代码代...

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" %>

<>

<div>

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

}

}

}

【asp.net 用户控件读取以及赋值】相关文章:

asp.net HTML文件上传标签

asp.net自定义控件代码学习笔记

asp.net动态载入用户控件的方法

asp.net中控制反转怎么理解?

ASP.NET用户控件技术

asp.net 操作cookie的实例

介绍asp.net 操作INI文件的读写

asp.net DataGrid控件中弹出详细信息窗口

asp.net网站安全从小做起与防范小结

DataList 中动态绑定服务器子控件的代码

精品推荐
分类导航