asp.net 用戶控件讀取以及賦值
更新時(shí)間:2009年07月18日 11:30:39 作者:
最近項(xiàng)目中看到同事寫的一個(gè)用戶控件,是一個(gè)下拉,值是從XML中讀取的,而且這部分還用到了LINQ讀取XML的知識(shí),最近才看了一點(diǎn)點(diǎn)LINQ的知識(shí),當(dāng)記錄一下吧。
XML內(nèi)容如下:
<?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>
用戶控件的關(guān)鍵代碼:
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>
后臺(tái)文件:
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;
}
}
}
復(fù)制代碼 代碼如下:
<?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>
用戶控件的關(guān)鍵代碼:
SystemVersion.ascx
復(fù)制代碼 代碼如下:
<%@ 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>
后臺(tái)文件:
復(fù)制代碼 代碼如下:
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;
}
}
}
您可能感興趣的文章:
相關(guān)文章
asp.net中Post表單保存頁(yè)面狀態(tài)并輸出源碼的實(shí)現(xiàn)方法
先執(zhí)行腳本,復(fù)制源碼到隱藏域里,再輸出源碼,注意代碼紅色設(shè)置2012-08-08
.NET?Core跨平臺(tái)資源監(jiān)控工具CZGL.SystemInfo用法
這篇文章介紹了.NET?Core跨平臺(tái)資源監(jiān)控工具CZGL.SystemInfo的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
asp.net 獲取系統(tǒng)中參數(shù)的實(shí)現(xiàn)代碼
asp.net 獲取系統(tǒng)中參數(shù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12
.NET Core結(jié)合Nacos實(shí)現(xiàn)配置加解密的方法
當(dāng)我們把應(yīng)用的配置都放到配置中心后,很多人會(huì)想到這樣一個(gè)問(wèn)題,配置里面有敏感的信息要怎么處理呢?本文就詳細(xì)的介紹了.NET Core Nacos配置加解密,感興趣的可以了解一下2021-06-06
Asp.net SignalR創(chuàng)建實(shí)時(shí)聊天應(yīng)用程序
這篇文章主要介紹了Asp.net SignalR創(chuàng)建實(shí)時(shí)聊天應(yīng)用程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

