Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have asp.net web form and when Im trying to assing onclick event to a button I get following error:

Compiler Error Message: CS1061: 'ASP.webform1_aspx' does not contain a definition for 'Button1_Click' and no extension method 'Button1_Click' accepting a first argument of type 'ASP.webform1_aspx' could be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 19: Line 20: Line 21: Line 22: Line 23:

ASP code is following:

     <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server" method="post">
    <div class="page">
    <p>Persons</p>
    <asp:Literal  id="drpdwn"  runat="server" /> 
    <p>TransactionType</p>
    <select id="sel" runat="server"> 
        <option>bilance </option>
        <option>liekākais parādnieks un aizdevējs</option>
        <option>vidējais aizņēmuma lielums</option>
    </select>

    <asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
        <table id="HTMLTable" width="300px;" runat="server">
            <tr>
                <td>
                    <b></b>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
<script type="text/javascript" >
    var yourSelect = document.getElementById("sel");
    var asdasd = yourSelect.options[yourSelect.selectedIndex].value;
</script>
</html>

And Codebehind is following:

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //int selectedIndex = (sel as HtmlSelect).SelectedIndex;
            sel.Value.ToString();
            SqlConnection cnn = new SqlConnection();
            SqlDataReader rdr = null;
            SqlCommand command = new SqlCommand();
            cnn.ConnectionString = @"Server=localhost;Database=TildeTest;uid=AdminUser;pwd=AdminPwd;app=WebApplication1;";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            string CommandText = "select * from persons";
            command = new SqlCommand(CommandText);
            command.CommandType = CommandType.Text;
            //command.Connection = cnn;
            try
            {
                cnn.Open();
                command.Connection = cnn;
                rdr = command.ExecuteReader();
                if (rdr.HasRows)
                {
                    drpdwn.Text = "<select>";
                    drpdwn.Text += "<option> </option>";
                    while (rdr.Read())
                    {
                        drpdwn.Text += "<option>" + rdr.GetString(1) + "</option>";
                    }
                    drpdwn.Text += "</select>";
                }
                else
                {
                    Console.WriteLine("No rows found.");
                }
                rdr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
            }

        }
        void Button1_Click(Object sender,
                                  EventArgs e)
        {
            // do something
        }
    }
}
share|improve this question
2  
Possible duplicate of stackoverflow.com/questions/6924061/… –  NightOwl888 Mar 23 at 9:54
    
Solution doesnt work for me. –  Воробьев Андрей Mar 23 at 9:58

2 Answers 2

up vote 0 down vote accepted

Assuming you are using visual studio, double click the button from design view, then write the code behind inside the generated function. That should work.

share|improve this answer
    
This solution worked! Visual Studio generated the same code but somehow it worked! Thank you! –  Воробьев Андрей Mar 23 at 10:14

you can change void Button1_Click to protected void Button1_Click and try.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.