Code blocks for checking checkbox value - c#

I'm new to ASP and C# and need some advice on C# code blocks inside ASPX. I have tried changing label text depend on checkbox value.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%# Page Language="C#" %>
<html>
<body>
<form id="form1" runat="server">
<asp:CheckBox runat="server" id="CheckBox1" AutoPostBack="True" Checked="True"></asp:CheckBox>
<% if (CheckBox1.Checked==True) {%>
<asp:Label id="Label1" runat="server" Text="Checked"></asp:Label>
<% } else {%>
<asp:Label id="Label1" runat="server" Text="Not Checked"></asp:Label>
<% }%>
</form>
</body>
</html>
It is not working and i not sure if this is right way of doing this.

It would be better to separate your markup and code into 2 different files (eg Default.aspx & Default.aspx.cs ).
To change label text try to handle page's load event.
Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<form id="form1" runat="server">
<asp:CheckBox runat="server" id="CheckBox1" AutoPostBack="True" Checked="True"></asp:CheckBox>
<asp:Label id="Label1" runat="server"></asp:Label>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
Label1.Text = CheckBox1.Checked ? "Checked" : "Not checked";
}
}

This is because true is spelled with lowercase only.
your code works fine, im assuming you are not using an IDE because it would have told you that True is not defined. you have to change it to true
<% if (CheckBox1.Checked==true) {%>

Firstly, you will have to use javascript or jquery for the.. since aspx is a server side language, it will have no control on the web page once it loads.

Related

Label false property cannot be changed programatically by button click

Having looked around at pages such as this, I cannot see why a label text will not display in a simple test page when a submit button is clicked. As the file is short, I have included all coding, in case there is anything I have not thought of in the background.
The text does not display when the button is pressed in a render generated with Ctrl+F5 in Visual Studio Express 2015. Where have I gone wrong?
Code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="labelTest2.aspx.cs"
Inherits="contact_labelTest2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Label Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Send"/>
<asp:Label ID="lblMessage" runat="server" Visible="false">
Test M AFTERessage</asp:Label>
</div>
</form>
</body>
</html>
Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class contact_labelTest2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lblMessage.Visible = true;
}
}
You should also add OnClick event to the Button. Like this:
<asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click"/>

Request.Form is empty (asp.net c#)

I've been doing some really clever stuff (I think) in ASP.Net c#, so much so that the simple stuff is more difficult (if that makes sense)
I have this snippet of code in my page
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="hdnConfirm" value="Hello World" />
<asp:LinkButton runat="server" PostBackUrl="/confirm.aspx" Text="Confirm">
</asp:LinkButton>
</form>
I have this snippet of code in confirm.aspx.
if !(IsPostback)
{
lblConfirm.Text = Request.Form["hdnConfirm"]
}
I was expecting this to be nice and simple but when i click the button and go to page "confirm.aspx" the Request.Form has no values. What have I missed ?
[TESTING]
I ran a test on a brand new web forms project in VS2013. Dot.Net 4.5.1 This does not work. PreviouPage is always null. Whether surrounded by (!IsPostBack) or not. Doesn't matter if the submitting control is a Button, LinkButton or Hyperlink. Request.Form["hdn"] is also null. I have restarted my computer just in case and still no joy. I am missing something really simple I am sure of it but I can't see what
This is the first page nothing in the code behind
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton runat="server" PostBackUrl="~/WebForm2.aspx">click</asp:LinkButton>
<asp:HiddenField runat="server" ID="hdn" Value="3" />
</div>
</form>
</body>
</html>
This is the second page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string s = ((HiddenField)this.PreviousPage.FindControl("hdn")).Value;
}
}
}
On confirm.aspx use PreviousPage.FindControl instead :
HiddenField hdnFieldName = this.PreviousPage.FindControl("hdnConfirm") as HiddenField;
string hiddenValue = string.Empty;
if (hdnFieldName != null)
{
hiddenValue = hdnFieldName.Value;
}
Here is good example to get you started.
What's going on Here:
By default with VS 2013 and Asp.Net v4.5.1 , there is a feature FriendlyUrls which is enabled.
The FriendlyUrls feature does kind of Asp.Net routing , thus changing URLs from localhost/webform1.aspx to localhost/webfrom1
PostbackUrl property will not work in combination with Asp.Net Routing. When ASP.NET routing is in use the PreviousPage URL is the final routed URL. The runtime will check for a file represented by the Routed URL now, which will be webform1 and NOT webfrom1.aspx. Since there is NO such file as webform1, so it will always set the PreviousPage to null.
Possible Solutions:
So, now you know the issue at hand.
1.) Either don't use the Routing system of Asp.Net Friendly Urls , in this case, therefore try adding the <%# PreviousPageType VirtualPath="~/WebForm1.aspx"%> to webform2.aspx page and check.
2.) OR if you keep the FriendlyURLs and hence the Routing system, make changes in your code to read the Form values using some other alternatives.

Using Tinymce results in Internal server error in asp.net

I am using Tinymce in my web page and in client side every thing works fine. But when I try to access the tinymce textbox in code behind the page control doesnot render. I mean Page load Didn't Render. Here is my HTML Code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="RichText.aspx.cs" Inherits="TESTING.RichText"
ValidateRequest="true" %>
<!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>Rich Text</title>
<script src="<%=ResolveClientUrl("~/Script/jquery-ui-1.7.1.custom.min.js") %>" type="text/javascript"></script>
<script type="text/javascript" src="Scirpt/tinymce/jscripts/tiny_mce/tiny_mce.js"> </script>
<script type="text/javascript" src="Scirpt/tinymce/jscripts/tiny_mce/InitializeRichTextBox.js"> </script>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 400px">
<div style="height: 100px; width: 100%;">
</div>
<asp:TextBox ID="TextBox1" runat="server" TextMode="multiline" CssClass="RichTextBox"
Width="150px"></asp:TextBox>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
</html>
Here is my Code Behind code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TESTING
{
public partial class RichText : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string here = TextBox1.Text;
}
}
}
When i leave empty text then Page Load renders and when I write something and click on Button then this error occurs A potentially dangerous Request.Form value was detected from the client (TextBox1="<ul><li><em><span ..."). I tried to find the solution in internet but unable to find the solution. Please help me out to find the solution.
Add this in Web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime requestValidationMode="2.0"/>
</system.web>
</configuration>
and add ValidateRequest = "false" in Page directive like this
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
for your reference click here

Handle Multiple Form tag in asp.net page?

My page contains a user control and inherits a master page. Both have a button. I have put it in a form tag. I am just trying to do an event bubbling from master page as well as user control. Now it doesn't load because of multiple form tags error. How to do it?
Masterpage :
<%# Master Language="C#" AutoEventWireup="True" CodeBehind="SampleSite.master.cs" Inherits="EventBubbling.SampleSite" %>
<!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>
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server" Text="MasterPageButton" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
User control :
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="SampleUserControl.ascx.cs"
Inherits="EventBubbling.Controls.SampleUserControl" EnableViewState="false" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnUC" runat="server" OnClick="btnUC_Click" Text="UserControlButton" />
</div>
</form>
</body>
</html>
User control.cs page :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EventBubbling.Controls
{
public partial class SampleUserControl : System.Web.UI.UserControl
{
public event EventHandler buttonClick;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUC_Click(object sender, EventArgs e)
{
buttonClick(sender, e);
}
}
}
Main Page :
<%# Page Language="C#" AutoEventWireup="True" MasterPageFile="~/SampleSite.Master"
CodeBehind="Default.aspx.cs" Inherits="EventBubbling._Default" %>
<%# Register Src="~/Controls/SampleUserControl.ascx" TagPrefix="uc" TagName="SampleUserControl" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<div>
<uc:SampleUserControl ID="UC1" runat="server" />
</div>
</asp:Content>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EventBubbling
{
public partial class _Default : System.Web.UI.Page
{
Button btn = new Button();
protected void Page_Load(object sender, EventArgs e)
{
UC1.buttonClick += new EventHandler(UC1_buttonClick);
btn = this.Master.FindControl("Button1") as Button;
btn.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
Response.Write("EventBubbling from MasterPage");
}
void UC1_buttonClick(object sender, EventArgs e)
{
Response.Write("EventBubbling from User control");
}
}
}
My goal is to learn event bubbling but I get some basic error with this code I posted.
your user control should not have a < form> tag, this is taken care of by the MasterPage all pages / controls are then rendered inside this master control < form> tag.
If you want to have your user control with its own form you will have to change to a different MasterPage and to be honest I am not sure how that would even work.
Keep Id of form different every where you are using form tag.
every where its <form id="form1" runat="server">.
Make it as Id=1 , Id=2 ,etc.
In master page keep form Id=1 , in user control page make it as 2 and in main aspx make it as 3.
Or Remove form tag from usercontrol page.
Since you have placed controls in master page, you need to keep the <form> tag thereon.
The error message: A page can have only one server-side Form tag. should actually be taken as: A page can have only one visible server-side Form tag.
if you want you can have more than one form tags.
Read here one Great tricks of Asp.net: http://blogs.msdn.com/b/kaevans/archive/2005/10/19/482778.aspx . Definitely not of much practical use.
Else, You need to correct the Markup of your userControl. Remove tags like: head, html, form.
Your final markup of UserControl will be:
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="SampleUserControl.ascx.cs"
Inherits="EventBubbling.Controls.SampleUserControl" EnableViewState="false" %>
<div>
<asp:Button ID="btnUC" runat="server" OnClick="btnUC_Click" Text="UserControlButton" />
</div>
I hope you get the idea what all needs to be removed.
All such pages or usercontrols that are going to be a part of this master page must not contain any <form> tag.

Call child iframe method from Parent aspx page

I've created one aspx page Default.aspx. It contains iframe with child.aspx. File Child.cs contains web methods which should be called by parent page.
As I understand I need to receive the instance of Child class so then I'll be able to them call. How can I make this?
I will be really appreciated for constructive solution,
Default.aspx (Parent page)
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Parent_Child_Iframe._Default" %>
<!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">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="btGetDataFromChild" runat="server" Text="Get Data from Child" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<iframe id="I1" height="300px" name="I1" src="child.aspx" width="400px"></iframe>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Child.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Child.aspx.cs" Inherits="Parent_Child_Iframe.Child" %>
<!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">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
Child.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Parent_Child_Iframe
{
public partial class Child : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string getResultData ()
{
return TextBox1.Text + TextBox2.Text;
}
}
}
So How I can call getResultData () from Child.aspx.cs by clicking Button on the parent page.
The main difficulty is that I can't use Javascript to collect the information from TextBoxes because the task is more complicated and I can use only server methods.
Thank you in advance,
Best wishes,
Greg.
Thats not really the way iframes work. Remember, HTTP is a stateless protocol, which means that each request for a page is an independent request, and won't have any context of the previous request. What I think you would need to do, is either request the Child.aspx page with specific form or request variables, and the Child page should output the correct page accordingly.
Could you explain what it is you want to achieve?

Categories