asp button click event - asp.net

I am using C#.net for creating an aspx page (Visual Studio 2010).
I have copied most of the template code from online ( HTML ) but when I drop a button of asp and double click it, it doesn't redirect to the code behind page but, instead creates "onclick=btnSubmit_Click" event in the source code.
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Height="30px"
Width="100px" **onclick="btnSubmit_Click"** />
Ideally it should go to the code behind page and allow event handling stuffs, I have no idea why this is happening.
My question is:
1. What do I do in order to get redirected like normal asp pages in Visual Studio
2. If possible, can someone explain what am I doing wrong here?
**I did get a way around it by using javascript
<script type="text/javascript" runat="server">
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
txtName.Text="btn submit clicked"
End Sub

please make sure that your <%Page directive has mentioned CodeBehind file.
i.e
something like this
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage/example.Master" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Example.default" %>
this will be on the top of your aspx page

Please try with the below methods.
Method 1:
Based on what I found here: http://forums.asp.net/t/1229894.aspx/1
Right click on your solution explorer.
Add New Item -> Class File.
Name the file as the name of your aspx eg: Default.aspx.cs
When it asks you the file should be in app_code click "no".
In your aspx in page attribute add
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
Similarly in your class file that you just added remove everything. Your class should look like this:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Method 2: while adding new page please make sure the place code in separate file box is checked

Related

My web control doesn't appear in my web page in a different folder

My web control doesn't appear in my web page in a different folder,
However it works in another web page in the same location.
My source code is like this :
..
<%Register src="~/UI/Human/HumanUserControl.wuc" TagPrefix="uc1"
TagName="HumanUserControl"/>
...
<uc1:HumanUserControl runat="Server" id="HumanUserControl"/>
...
Error:
Request is not available in this context
The syntax on your Register tag is incorrect. And I'm not sure why you're using a ".wuc" extension instead of the default ".ascx".
You need matching <% %> tags, like so:
<%# Register Src="~/UI/Human/HumanUserControl.wuc" TagPrefix="uc1" TagName="HumanUserControl" %>
If you delete the <%Register .../> and <uc1 .../> tags from your page completely, then in Visual Studio, drag-and-drop the user control from the Solution Explorer directly onto your page wherever you want the control to appear, it will automatically add the correct Register and user control tags for you.
If this doesn't clear things up, then provide more detailed error information, including the code where the exception is thrown.
The following works just fine:
~/UI/Human/HumanUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="HumanUserControl.ascx.cs" Inherits="UserControlTest.UI.Human.HumanUserControl" %>
<asp:Label ID="lblTest" runat="server" />
~/UI/Human/HumanUserControl.ascx.cs
namespace UserControlTest.UI.Human
{
public partial class HumanUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
lblTest.Text = Request.Browser.Browser;
}
}
}
~/Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UserControlTest._Default" %>
<%# Register Src="~/UI/Human/HumanUserControl.ascx" TagPrefix="uc1" TagName="HumanUserControl" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<uc1:HumanUserControl runat="server" id="HumanUserControl" />
</asp:Content>

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

I have a web user control:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="SitewideNotification.ascx.cs" Inherits="Controls_Fresh_SitewideNotification" %>
<asp:PlaceHolder runat="server" ID="Javascript">
test
</asp:PlaceHolder>
In my code behind I have:
protected void Page_Load(object sender, EventArgs e)
{
Page.Master.FindControl("JSContent").Controls.Add(Javascript);
}
My master page ends with:
<asp:ContentPlaceHolder runat="server" ID="JSContent"/>
</form>
</body>
</html>
But I get the error:
System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Page.Master.FindControl("JSContent").Controls.Add(Javascript);
I'm a bit lost as to why this is, does anyone know how to fix it?
I have usually seen this when I have javascript code with the <% %> block in the head section of the page. When you move it out of the head to below the body it seems to go away in the situations I have seen. Since you didn't show us that part of the code it is going to be hard to say exactly what the issue is but here is a good post on the problem:
http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/

HTML rendered incorrectly for dynamic HTML content using master page

I have a content page which has some dynamic HTML and some static HTML. This dynamic HTML is assigned to a hidden variable in page load.
Content page HTML
<asp:HiddenField id = "hid" runat="server"></asp>
CS of content page
protected void Page_Load(object sender, EventArgs e)
{
hid.Value = node1.InnerText;
}
This content page has master page. Below is its page directive
<%# Page Title="" Language="C#" MasterPageFile="~/log.Master"
AutoEventWireup="true" CodeBehind="Log.aspx.cs" Inherits="s.Log"
ValidateRequest="false" %>`
Below is snippet of Content page
<%# Page Title="" Language="C#" MasterPageFile="~/log.Master"
AutoEventWireup="true" CodeBehind="Log.aspx.cs" Inherits="s.Log"
ValidateRequest="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:HiddenField id = "hid" runat="server"/>
</asp:Content>`
Now when content page loads, static HTML is loaded inside master page, but dynamic contents are rendered outside master page hence shows wrong rendering.
The point of <asp:HiddenField> is that it is hidden - therefore any information contained within it should be definition never be rendered on the page.
If the contents of the field contain any HTML code that could cause it to break (such as HTML code that hasn't been encoded)... then it's possible that it will show something, but it is highly likely to be incorrect. I think this is what you are seeing.
I'm not entirely sure exactly why you're using the HiddenField, or what you're trying to do with the contents of it, but I would suggest you change the following line...
hid.Value = node1.InnerText;
With...
hid.Value = Server.HtmlEncode(node1.InnerText);

ASP.NET button click event only works when on Top level master page

I have base.master (top-level), second.master (submaster). A button in the top-level master fires the click event fine, on a page which ues that one master, if its on page where the second.master is used, that button click event doesn't work.
No buttons that I place in the submaster work either, heres the code:
protected void afd(object sender, EventArgs e)
{
var a = "C";
}
<asp:Button runat="server" OnClick="afd" Text="huh" />
Base.master directive:
<%# Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="True" CodeBehind="Base.master.cs" Inherits="UmbracoWebsite.masterpages.Base" %>
Register.master directive:
<%# Master CodeBehind="~/masterpages/Register.master.cs" Inherits="UmbracoWebsite.masterpages.Register" Language="C#" MasterPageFile="~/masterpages/Base.master" AutoEventWireup="True" %>
By this it means you are wiring up button_click event manually and also you are missing button name so i would assume it as btn1
so you also need to provide the following line on Page_Load of Page_Init of the page where using it:
btn1.Click += new EventHandler(afd);
Hope this helps....

Using ASPX Web User Control with Visual Studio 2010

I am trying to implement a Web User Control into one of my APSX pages but keep getting the following warning:
Element 'IntFilter' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing.
The user control is defined in the same web project as the aspx page.
Question:
How do I resolve this warning (I do not want to move the control to a separate project)?
Also, what do I need to do to enable IntelliSense for this control so I can set its FilterTypeSelection property from ASPX?
Code for "~/FilterControls/IntFilter.ascx"
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="IntFilter.ascx.vb" Inherits="StaffSupport.Filters.IntegerFilter" %>
<asp:DropDownList ID="typeFilterDropDownList" runat="server">
<asp:ListItem Selected="True" Text ="Any" Value="-1" />
<asp:ListItem Selected="False" Text ="Equal" Value= "0" />
</asp:DropDownList><br />
<asp:TextBox ID="TextBox1" runat="server" /><asp:CheckBox ID="CheckBox1" runat="server" Text="Inclusive" /><br />
<asp:TextBox ID="TextBox2" runat="server" /><asp:CheckBox ID="CheckBox2" runat="server" Text="Inclusive" /><br />
Code for "~/FilterControls/IntFilter.ascx.vb"
Namespace Filters
Public Class IntegerFilter
Inherits System.Web.UI.UserControl
Public Enum NumberFilterTypes As SByte
Any = -1
Equals = 0
End Enum
Public Property FilterTypeSelection As NumberFilterTypes
Get
Dim value As SByte
If Not Integer.TryParse(typeFilterDropDownList.SelectedValue, value) Then
value = -1
End If
Return CType(value, NumberFilterTypes)
End Get
Set(value As NumberFilterTypes)
typeFilterDropDownList.SelectedValue = CSByte(value)
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
End Namespace
Code for "OpenCases.aspx"
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/StaffSite.Master" CodeBehind="OpenCases.aspx.vb" Inherits="StaffSupport.OpenCases" %>
<%# Register TagPrefix="filters" TagName="IntFilter" src="~/FilterControls/IntFilter.ascx" %>
<asp:Content ID="bodyContent" ContentPlaceHolderID="cphBody" runat="server">
ID<br />
<filters:IntFilter ID="IntFilter1" runat="server" />
</asp:Content>
Code for "OpenCases.aspx.vb"
Public Class OpenCases
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Page.ViewStateMode = UI.ViewStateMode.Disabled
End Sub
Update 2012/02/21:
Fixed the "filters" vs "filter" miss match.
Also of note, if you drag the control from the Solution Explorer to the page in Design view it will add the references you need (though it was still generating the warning for me). If you drag it to the page in source view it will add an a tag with a href to the element.
Update 2012/02/21 b:
Found the solution, see my answer below.
Apparently you have to reference both the ASCX page and the assembly.
If you drag the ASCX page from the "Solution Explorer" window to the Design view for the page you are editing it will add the reference for the ASCX page, but you will have to add the assembly reference manually.
OpenCases.aspx
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/StaffSite.Master" CodeBehind="OpenCases.aspx.vb" Inherits="StaffSupport.OpenCases" %>
<%# Register Assembly="StaffSupport" Namespace="StaffSupport.Filters" TagPrefix="filters" %><%-- Assembly Reference --%>
<%# Register TagPrefix="filters" TagName="IntFilter" src="~/FilterControls/IntFilter.ascx" %>
<asp:Content ID="bodyContent" ContentPlaceHolderID="cphBody" runat="server">
ID<br />
<filters:IntFilter ID="IntFilter1" runat="server" />
</asp:Content>
Note: beware of object type collisions. For example the following would also work:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/StaffSite.Master" CodeBehind="OpenCases.aspx.vb" Inherits="StaffSupport.OpenCases" %>
<%# Register Assembly="StaffSupport" Namespace="StaffSupport.Filters" TagPrefix="pre1" %><%-- Assembly Reference --%>
<%# Register TagPrefix="pre2" TagName="IntFilter" src="~/FilterControls/IntFilter.ascx" %>
<asp:Content ID="bodyContent" ContentPlaceHolderID="cphBody" runat="server">
ID<br />
<pre1:IntFilter ID="IntFilter1" runat="server" />
</asp:Content>
This is why it started working for me Friday after I posted this, I had added a custom control which implemented System.Web.UI.WebControls.TextBox so I could drag and drop it from the Toolbox. Since it was in the same namespace the control added the assembly reference when it added the control to the page.
Note: if you are referencing dll files which are contained in your project then you may need to remove the page registrations, build, then add the page registrations back. Otherwise the compiler may complain that the dll files are not in the bin.
Update: 2013/04/18
It appears you only need to add the assembly reference if the UserControl is not defined in the same namespace.
If the parrent is defined in Proj.Presentation and the UserControl is defined in Proj.Presentation then you should not need the assembly reference.
If the parrent is defined in Proj.Page and the UserControl is defined in Proj.Page.UserControl then you should not need the assembly reference.
If the parrent is defined in Proj.Page and the UserControl is defined in Proj.UserControl then you need the assembly reference.
The control is declared as:
<%# Register TagPrefix="filters"
and in the markup
<filter:IntFilter
These must match.
You are registering a different prefix from the one you're attempting to use.
You can either change this:
<filter:IntFilter ID="IntFilter1" runat="server" />
to this:
<filters:IntFilter ID="IntFilter1" runat="server" />
Or change this:
<%# Register TagPrefix="filters" TagName="IntFilter"
to this:
<%# Register TagPrefix="filter" TagName="IntFilter"
Close Visual Studio, delete the schema cache, and re-open Visual Studio. You can find the schemas under something like:
C:\Users\Pavel\AppData\Roaming\Microsoft\VisualStudio\10.0\ReflectedSchemas
It is safe to delete all files in this folder.
Delete the contents of that above folder, and all is well.

Resources