您好我是C#的新手,我正在尝试实施与此网站相关的示例: http://www.devmanuals.com/tutorials/ms/aspdotnet/dropdownlist.html
我要做的就是创建网络部分&将其部署到sharepoint: 代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master"
AutoEventWireup="true" CodeFile="DropDownList.aspx.cs" Inherits="DropDownList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style3
{
color: #800000;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2 style="color:Green">DropDownList in ASP.NET 4 , C#</h2>
<strong><span class="style3">Enter first number:</span>
</strong>
<br />
<asp:TextBox ID="txt1" runat="server" Text="12"/>
<br />
<br />
<span class="style3">
<strong>Enter second number:
</strong>
</span>
逻辑代码:
protected void drp1_SelectedIndexChanged(object sender, EventArgs e)
{
double firstno = Convert.ToDouble(txt1.Text);
double secondno = Convert.ToDouble(txt2.Text);
if(drp1.SelectedIndex == 1)
{
double add = firstno + secondno;
label1.Text = "Addition is :" + add;
}
我一直收到错误: txt1无法识别, 请指教,我是C#的新手
答案 0 :(得分:0)
您需要在代码隐藏文件中声明文本框,如下所示:
protected TextBox txt1;
否则在事件处理程序中无法访问它(drp1_SelectedIndexChanged)。
但是我没有在HTML中看到对drp1_SelectedIndexChanged的任何引用,这是正确的吗?