[英]How to Apply Dropdownlist Control Script to an .aspx Page?
我目前正在开始深入研究ASP.Net。 到目前为止,我一直在相应地使用CSS用HTML样式创建大多数页面,最近,我在页面上应用了Site.Master页面来整理代码。 我正在创建一个页面,该页面需要一个下拉列表控件,该控件最终将进入数据库并显示相关信息,但是现在我将其保持简单,并且当您在页面上使用“您已选择项目1”进行更新时,在下拉列表中选择一个项目。 问题是,我似乎无法弄清楚如何将脚本应用于页面。
到目前为止,这就是我所拥有的:
<%@ Page Title="Bowtie Performance Parts - Inventory" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="parts.aspx.cs" Inherits="WebApplication1._Default"%>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<!--Dropdown Menu Setup-->
<head>
</head>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h1 class="defaultTitle">Parts Catalog</h1>
<p id="defaultLarge">Please Choose from the list below to get started!</p>
<center>
<form id="PartsForm" runat="server">
<asp:DropDownList id="dropDown2" runat="server">
<asp:ListItem>Please Choose</asp:ListItem>
<asp:ListItem disabled="disabled">--Motors--</asp:ListItem>
<asp:ListItem>4 Cylinder</asp:ListItem>
<asp:ListItem>6 Cylinder</asp:ListItem>
<asp:ListItem>8 Cylinder</asp:ListItem>
<asp:ListItem disabled="disabled">--Intakes--</asp:ListItem>
<asp:ListItem disabled="disabled">--Forced Induction--</asp:ListItem>
<asp:ListItem>Superchargers</asp:ListItem>
<asp:ListItem>Turbos</asp:ListItem>
</asp:DropDownList></form>
</center>
</asp:Content>
该页面已设置样式并按预期显示,下拉菜单显示并正常工作,但是现在我试图找出将脚本放置在何处,以便当我在列表中选择一个项目时显示“所选项目#”。
<script runat="server">
Sub submit(sender As Object, e As EventArgs)
mess.Text="You selected " & dropDown2.SelectedItem.Text
End Sub
</script>
将此脚本放在代码中的任何位置都会导致错误,并且页面无法正确编译。 我是使用C#和ASP.net的新手,但是我不确定我是否了解如何执行此操作的逻辑。 我看了很多这样的例子:
<script runat="server">
Sub submit(sender As Object, e As EventArgs)
mess.Text="You selected " & drop1.SelectedItem.Text
End Sub
</script>
<!DOCTYPE html>
<html>
<body>
<form runat="server">
<asp:DropDownList id="drop1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:DropDownList>
<asp:Button Text="Submit" OnClick="submit" runat="server"/>
<p><asp:label id="mess" runat="server"/></p>
</form>
</body>
</html>
但是以这种方式应用脚本会使我无法引用Site.Master页面,该页面包含Banner,NavBar和指向样式整个网站的.css文件的链接(以确保所有网页的一致性)。
如何才能做到这一点?
预先感谢,泰勒·迪恩
在您的代码后面使用drop1_SelectedIndexChanged事件
Protected Sub drop1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drop1.SelectedIndexChanged
mess.text = drop1.SelectedValue
End Sub
这在VB中,但是您可以将其转换为C#。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.