0

嗨,我是 C# 新手,我正在尝试实现与该网站相关的示例:http: //www.devmanuals.com/tutorials/ms/aspdotnet/dropdownlist.html

我要做的是创建 Web 部件并将其部署到共享点:代码:

<%@ 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# 新手

4

1 回答 1

0

您需要在代码隐藏文件中声明文本框,如下所示:

protected TextBox txt1;

否则,您的事件处理程序 (drp1_SelectedIndexChanged) 将无法访问它。

但是我在您的 HTML 中没有看到任何对 drp1_SelectedIndexChanged 的​​引用,对吗?

于 2013-10-18T15:04:54.393 回答