如何使用XSLT c#创建下拉列表
本文关键字:创建 下拉列表 XSLT 何使用 | 更新日期: 2023-09-27 17:51:15
我正在研究使用xslt文件显示重复项目的项目。如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:param name="BasePath" select="/" />
<xsl:param name="OffersPerPage" select="4" />
<xsl:template match="/" xml:space="default">
<div class="offers_section">
<script>
$(document).ready(function() {
$(".offers_page").hide();
$("#OfferPage1").show();
});
function OffersPageChange(CurrentPage, NewPage)
{
$("#OfferPage" + CurrentPage).hide();
$("#OfferPage" + NewPage).show();
$("$offers_section").focus();
return false;
}
</script>
<xsl:choose>
<xsl:when test="count(//Offers/Offer) > 0">
<xsl:for-each select="//Offers/Offer[(position()) mod $OffersPerPage = 1 or position = 1]">
<xsl:variable name="page" select="position()" />
<xsl:variable name="lastpage" select="last()" />
<div class="offers_page"><xsl:attribute name="id">OfferPage<xsl:value-of select="$page" /></xsl:attribute>
<div class="offers_paging">
<xsl:if test="$page > 1">
<a href="#"><xsl:attribute name="onclick">return OffersPageChange(<xsl:value-of select="$page" />, <xsl:value-of select="$page - 1" />)</xsl:attribute>< Previous</a>
</xsl:if>
<xsl:if test="$page < $lastpage">
<a href="#"><xsl:attribute name="onclick">return OffersPageChange(<xsl:value-of select="$page" />, <xsl:value-of select="$page + 1" />)</xsl:attribute>Next ></a>
</xsl:if>
<br /><br />
<xsl:if test="$lastpage != 1">
<xsl:for-each select="//Offers/Offer">
<xsl:if test="(position() - 1) mod $OffersPerPage = 0">
<a href="#"><xsl:attribute name="class"><xsl:choose><xsl:when test="(position() - 1) div $OffersPerPage + 1 = $page">offers_page_number_active</xsl:when><xsl:otherwise>offers_page_number</xsl:otherwise></xsl:choose></xsl:attribute><xsl:attribute name="onclick">return OffersPageChange(<xsl:value-of select="$page" />, <xsl:value-of select="(position() - 1) div $OffersPerPage + 1" />)</xsl:attribute><xsl:value-of select="(position() - 1) div $OffersPerPage + 1" /></a>
</xsl:if>
</xsl:for-each>
<br /><br />
</xsl:if>
</div>
<xsl:for-each select=". | following-sibling::Offer[(position()) < $OffersPerPage]" >
<div class="offers_offer">
<h2><xsl:value-of select="Partner" /></h2>
<xsl:if test="ImageSource != ''">
<div class="offers_image_container">
<div class="offers_image">
<img width="100%"><xsl:attribute name="src"><xsl:value-of select="$BasePath" />portals/<xsl:value-of select="ImageSource" /></xsl:attribute></img>
</div>
<div class="offers_image_overlay">
<img width="100%" height="33" alt=""><xsl:attribute name="src"><xsl:value-of select="$BasePath" />portals/_default/skins/images/offer%20top%20swoop.png</xsl:attribute></img>
<img width="100%" height="126" style="position:absolute;bottom:0;left:0;right:0;" alt=""><xsl:attribute name="src"><xsl:value-of select="$BasePath" />portals/_default/skins/images/offer%20bottom%20swoop.png</xsl:attribute></img>
</div>
</div>
</xsl:if>
<xsl:if test="Description != ''">
<div class="offers_description">
<xsl:value-of select="Description" /><br />
</div>
</xsl:if>
<xsl:if test="LegalInformation != '' and LegalOnOfferPage = 'True'">
<div class="offers_legal">
<xsl:value-of select="LegalInformation" /><br />
</div>
</xsl:if>
<xsl:if test="Facebook != '' or Twitter != ''">
<div class="offers_social_media">
Follow us on:<br />
<div class="social_media_links">
<xsl:if test="Facebook != ''">
<a class="facebook_logo" style="float:none;" target="_blank" title="Facebook"><xsl:attribute name="href"><xsl:value-of select="Facebook" /></xsl:attribute></a>
</xsl:if>
<xsl:if test="Twitter != ''">
<a class="twitter_logo" style="float:none;" target="_blank" title="Twitter"><xsl:attribute name="href">https://www.twitter.com/<xsl:value-of select="Twitter" /></xsl:attribute></a>
</xsl:if>
</div>
</div>
</xsl:if>
<xsl:if test="UrlText != ''">
<div class="offers_url">
<a target="_blank"><xsl:attribute name="href"><xsl:value-of select="Url" /></xsl:attribute><xsl:value-of select="UrlText" /></a>
</div>
</xsl:if>
<xsl:if test="Email != ''">
<div class="offers_email">
Email: <a><xsl:attribute name="href">mailto:<xsl:value-of select="Email" /></xsl:attribute><xsl:value-of select="Email" /></a>
</div>
</xsl:if>
<xsl:if test="Phone != ''">
<div class="offers_phone">
Tel no: <strong><xsl:value-of select="Phone" /></strong>
</div>
</xsl:if>
</div>
</xsl:for-each>
<xsl:if test="$lastpage > 1">
<div class="offers_paging">
<xsl:if test="$lastpage != 1">
<xsl:for-each select="//Offers/Offer">
<xsl:if test="(position() - 1) mod $OffersPerPage = 0">
<a href="#"><xsl:attribute name="class"><xsl:choose><xsl:when test="(position() - 1) div $OffersPerPage + 1 = $page">offers_page_number_active</xsl:when><xsl:otherwise>offers_page_number</xsl:otherwise></xsl:choose></xsl:attribute><xsl:attribute name="onclick">return OffersPageChange(<xsl:value-of select="$page" />, <xsl:value-of select="(position() - 1) div $OffersPerPage + 1" />)</xsl:attribute><xsl:value-of select="(position() - 1) div $OffersPerPage + 1" /></a>
</xsl:if>
</xsl:for-each>
<br /><br />
</xsl:if>
<xsl:if test="$page > 1">
<a href="#"><xsl:attribute name="onclick">return OffersPageChange(<xsl:value-of select="$page" />, <xsl:value-of select="$page - 1" />)</xsl:attribute>< Previous</a>
</xsl:if>
<xsl:if test="$page < $lastpage">
<a href="#"><xsl:attribute name="onclick">return OffersPageChange(<xsl:value-of select="$page" />, <xsl:value-of select="$page + 1" />)</xsl:attribute>Next ></a>
</xsl:if>
</div>
</xsl:if>
</div>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<div class="offers_not_available">
Sorry, no offers are currently available in this category
</div>
</xsl:otherwise>
</xsl:choose>
</div>
背后的代码是:
void Load_Category(int CategoryId)
{
string skinPath = PortalSettings.ActiveTab.SkinPath;
XDocument XDocOffers = new XDocument(new XElement("Offers"));
SqlDataProvider provider = new SqlDataProvider();
SqlConnection SQLConnection = new SqlConnection(provider.ConnectionString);
SQLConnection.Open();
SqlConnection SQLConnectionAnswers = new SqlConnection(provider.ConnectionString);
SQLConnectionAnswers.Open();
SqlCommand sqlComm = new SqlCommand("CIDOTOffersGetOffersByCategory", SQLConnection);
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.Parameters.Add(new SqlParameter("@CategoryId", SqlDbType.Int)).Value = CategoryId;
SqlDataReader OffersReader = sqlComm.ExecuteReader();
string OfferId;
string Partner;
string Description;
string ImageSource;
string Url;
string UrlText;
string Email;
string Phone;
string LegalInformation;
string Facebook;
string Twitter;
string LegalOnOfferPage;
string onclick;
string Emailonclick;
while (OffersReader.Read())
{
OfferId = OffersReader["OfferId"].ToString();
Partner = OffersReader["Partner"].ToString();
Description = OffersReader["Description"].ToString();
ImageSource = OffersReader["ImageSource"].ToString();
Url = OffersReader["Url"].ToString();
UrlText = OffersReader["UrlText"].ToString();
Email = OffersReader["Email"].ToString();
Phone = OffersReader["Phone"].ToString();
LegalInformation = OffersReader["LegalInformation"].ToString();
Facebook = OffersReader["Facebook"].ToString();
Twitter = OffersReader["Twitter"].ToString();
LegalOnOfferPage = OffersReader["LegalOnOfferPage"].ToString();
onclick = "_gaq.push(['_trackEvent', 'Book Now', 'click', '" + Partner + "']);";
Emailonclick = "_gaq.push(['_trackEvent', 'Email', 'click', '" + Email + "']);";
XElement NextOffer =
new XElement("Offer",
new XElement("OfferId", OfferId),
new XElement("Partner", Partner),
new XElement("Description", Description),
new XElement("ImageSource", ImageSource),
new XElement("Url", Url),
new XElement("UrlText", UrlText),
new XElement("Email", Email),
new XElement("Phone", Phone),
new XElement("LegalInformation", LegalInformation),
new XElement("Facebook", Facebook),
new XElement("Twitter", Twitter),
new XElement("LegalOnOfferPage", LegalOnOfferPage),
new XElement("onclick", onclick),
new XElement("Emailonclick", Emailonclick)
);
XDocOffers.Element("Offers").Add(NextOffer);
}
OffersReader.Close();
SQLConnection.Close();
string XSLTFile = Server.MapPath(skinPath + "xslt/offers.xslt");
if (File.Exists(XSLTFile))
{
StringWriter XSLTOutput = new StringWriter();
XmlWriter XMLOutput = XmlWriter.Create(XSLTOutput);
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XSLTFile);
XsltArgumentList ArgumentsQA = new XsltArgumentList();
if (Settings.ContainsKey("OffersPerPage"))
{
ArgumentsQA.AddParam("OffersPerPage", "", Settings["OffersPerPage"].ToString());
}
else
{
ArgumentsQA.AddParam("OffersPerPage", "", "4");
}
ArgumentsQA.AddParam("BasePath", "", "/");
// Execute the transform and output the results to a writer.
xslt.Transform(XDocOffers.CreateReader(), ArgumentsQA, XMLOutput);
//Output.Controls.Add(new LiteralControl(XSLTOutput.ToString().Replace("&", "&")));
Output.Controls.Add(new LiteralControl(HttpUtility.HtmlDecode(XSLTOutput.ToString())));
}
else
{
Output.Controls.Add(new LiteralControl("Cannot find XSLT file"));
}
}
我以前从未使用过xslt,现在我想向页面添加下拉列表,并且需要从页面正文中的重复项中创建对所选值的搜索。
我的问题是:
如何在xslt中创建下拉列表。我试过下面的链接,但它不适合我。使用c#
在xslt中生成动态下拉列表如何创建一个事件,将搜索从重复的项目。有什么方法可以让我调用下拉菜单的selectedindex更改事件?
请帮。
您可以创建一个DropDownList
<asp:DropDownList xmlns:asp="System.Web.UI.WebControls" ID="TheDdl" OnSelectedIndexChanged="Selection_Change" runat="server">
<asp:ListItem Value="1" Text="Text 1" />
<asp:ListItem Value="2" Text="Text 2" />
<asp:ListItem Value="3" Text="Text 3" />
</asp:DropDownList>
Code behind
看起来像
void Selection_Change(Object sender, EventArgs e)
{
// do something ...
}
Xslt只是将一些源XML转换为一些格式化的文本,通常是HTML或XML。你的xslt正在创建html代码,一些
。我看到xslt代码中绑定了一些链接。
生成的html代码存储在字符串XSLTOutput.ToString()中,该字符串稍后作为文字控件插入ASP页面。背后的代码是通过从数据库中检索一些数据来创建源XML文档XDocOffers。
您可以编辑已提供的xslt代码,并允许它插入html控件
如果你想使用<控件,我猜你试图使用XDocOffers作为数据源。如果没有,你必须自己编写代码从数据库中查询。
您可以运行代码并查看XDocOffers XML文档及其结构。然后你可以明确询问哪个成员你想要用作下拉键,哪个作为下拉值。具体来讲下拉控件
相关文章:
- 使用枚举和实体框架基架从模型创建下拉列表
- j可编辑的自定义编辑器,从服务器获取数据以创建下拉列表
- 循环创建下拉列表的最佳方法
- 创建下拉列表用于使用列表中的字符串<字符串>
- MVC与Razor创建下拉列表
- 如何动态创建下拉列表并在视图中枚举它们?asp.net MVC 5
- 在visual studio 2013中的MVC 5中创建下拉列表
- 创建下拉列表
- 使用linq获取2列并创建下拉列表
- 在formview中动态创建下拉列表
- 使用ClosedXML创建下拉列表
- 为用户角色创建下拉列表
- 如何在c#中使用Openxml在Excel中创建下拉列表
- 在视图中创建下拉列表并将值传递给控制器- MVC
- 从分组ViewModel的细节中创建下拉列表
- 我如何创建下拉列表框作为搜索数据的文本框
- 如何创建下拉列表在ASP禁用.. NET Razor MVC 5
- HTML从数据库linqMVC创建下拉列表
- 如何从控制器内轻松创建下拉列表
- 如何在button_click事件中创建下拉列表对象的实例