0

Hi I am new to C# and I am trying to implement an example with relation to this website: http://www.devmanuals.com/tutorials/ms/aspdotnet/dropdownlist.html

What I have to do is to create web part & deploy it onto sharepoint: Code:

<%@ 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>

Logic code:

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;
}

I keep getting the error: txt1 is not recognized, Please advise, I am new to C#

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user2445971
  • 77
  • 1
  • 11

1 Answers1

0

You need to declare the textbox in the code-behind file, like so:

protected TextBox txt1;

Otherwise it isn't accesible in your event handler (drp1_SelectedIndexChanged).

But I don't see any references to drp1_SelectedIndexChanged in your HTML, is that correct?

Aage
  • 5,932
  • 2
  • 32
  • 57
  • Hi what is the purpose of protected? – user2445971 Oct 18 '13 at 15:41
  • This is a convention in declaring controls in code-behind (Web Forms). If you have an automatically generated designer file besides your mark-up and code-behind, you'll see the same convention in use. Protected signifies that this class (a control in this case) and all classes inheriting from it can access it, but no other. In case you didn't know. – Aage Oct 21 '13 at 14:01