Why my button is not calling my function? - c#

I'm trying to call a function to change the contents of a <p> tag, it appears that my function is not being called, thanks in advance.
I've checked that all the tags have runat="server", and making sure the function name is correct.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace StepFollowingDemo
{
public partial class Test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
void Yeah()
{
string Some = Something.Value;
this.Result.InnerHtml = Some;
}
}
}
}
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Test1.aspx.cs" Inherits="StepFollowingDemo.Test1" %>
Test Case
<label runat="server" for="Something">Type:</label>
<input runat="server" type="Text" class="from-control" id="Something"/>
<button runat="server" type="button" onclick="Yeah();" class="btn btn-success">Project your words</button>
<br /><br />
<label runat="server" for="Result">Output: </label>
<p runat="server" id="Result"></p>

I assume you want to run that code server-side. I see two problems:
You will need to use an ASP Button control, like <asp:Button>. Look at that documentation for details about how to use it. The <button> tag is not an ASP control, so that onclick is running client-side. It is looking for a javascript function.
You declared that method inside Page_Load and with the wrong signature for an event. Move it out of Page_Load, under the class:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Yeah(object sender, CommandEventArgs e)
{
string Some = Something.Value;
this.Result.InnerHtml = Some;
}
Also, Yeah isn't the usual naming convention, but it would still work.

Related

Cannot put the text in input box in asp.net

I am trying to make the input box show some text in the asp.net using c#, but after I run the code behind, nothing is displayed in the input box. The ui is RadNumericTextBox uiDistance. In class, I put this.uiDistance.Text = "123"; But in the webpage after run the code. I cannot see "123" is put in the uiDistance text box. How can I fix it?
Here is my aspx UsageControl2SubControl1.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UsageControl2SubControl1.ascx.cs" Inherits="WebControls.UsageControl2SubControl1" %>
<asp:HiddenField ID="uiRemoved" Value="false" runat="server" />
<asp:HiddenField ID="uiID" runat="server" />
<div class="form-group">
<div class="col-md-2 customColumnPadding">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<telerik:RadTextBox ID="uiToLocation" runat="server" Width="100%" AutoPostBack="true"
OnTextChanged="uiToLocation_Leave" EmptyMessage="<%$ Resources:ResourceHKEx,Arrive_To %>" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="col-md-2 customColumnPadding" id="uiColumnDistance" runat="server">
<telerik:RadNumericTextBox ID="uiDistance" runat="server" Width="100%" MinValue="0" NumberFormat-DecimalDigits="2" EmptyMessage="<%$ Resources:Resource,Distance %>" />
</div>
</div>
<div class="hr-line-dashed"></div>
And Here is my code behind: UsageControl2SubControl1.ascx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
namespace WebControls
{
public partial class UsageControl2SubControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToBoolean(uiRemoved.Value))
{
this.Visible = false;
}
}
protected void uiAdd_Click(object sender, Telerik.Web.UI.ImageButtonClickEventArgs e)
{
}
protected void uiRemove_Click(object sender, Telerik.Web.UI.ImageButtonClickEventArgs e)
{
}
protected void RadComboBoxProduct_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
}
protected void uiAdd_Click1(object sender, ImageClickEventArgs e)
{
}
protected void uiRemove_Click1(object sender, ImageClickEventArgs e)
{
}
protected void uiToLocation_Leave(object sender, EventArgs e)
{
this.uiDistance.Text = "123";
}
}// end class
}// end namespace
Just to update your aspx code little bit
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="form-group">
<div class="col-md-2 customColumnPadding">
<telerik:RadTextBox ID="uiToLocation" runat="server" Width="100%" AutoPostBack="true"
OnTextChanged="uiToLocation_Leave" EmptyMessage="<%$ Resources:ResourceHKEx,Arrive_To %>" />
</div>
<div class="col-md-2 customColumnPadding" id="uiColumnDistance" runat="server">
<telerik:RadNumericTextBox ID="uiDistance" runat="server" Width="100%" MinValue="0" NumberFormat-DecimalDigits="2" EmptyMessage="<%$ Resources:Resource,Distance %>" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
As your textbox is only inside the update panel so, on post back only that portion is partially updated, you need to put RadNumericTextBox inside the update panel too.
Hope this will work.
You are using the wrong property. Use Value not Text
protected void uiToLocation_Leave(object sender, EventArgs e)
{
this.uiDistance.Value = "123";
}
As always, check the documentation. See: https://docs.telerik.com/devtools/aspnet-ajax/controls/numerictextbox/features/getting-and-setting-values
Your current code will works only when you tab out from uiToLocation textbox.
If you want to show it when page loads, just move your code in to Page_Load event instead uiToLocation_Leave.
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToBoolean(uiRemoved.Value))
{
this.Visible = false;
}
this.uiDistance.Text = "123"; // it will assign when page loads.
}
If you want to load it only once when page loads, use IsPostaback, see below.
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToBoolean(uiRemoved.Value))
{
this.Visible = false;
}
if (!IsPostBack)
{
this.uiDistance.Text = "123"; // it will assign only once when page loads.
}
}
IT seems you are using the wrong property to assign value to telerik Textbox.
Use .Value property as .Text property if for default aspx Textbox control
this.uiDistance.Value= "123"

How I can execute C# code using Roslyn in ASP.Net web form?

I have peice of code in ASP.Net for execute code of C# using roslyn compiler. However, I use ASP.Net web forms to build user interface for execute the code. So,I want to take code from one textbox and by "Compile" button I show the result in another textbox.
So, I have this controls:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>Compiler Tool</h1>
</hgroup>
<p>
You can compile your source code of C# using Roslyn Compiler from Microsoft</p>
<br/>
<p>
<asp:TextBox ID="TextBox3" runat="server" Height="185px" Width="480px" OnTextChanged="TextBox3_TextChanged"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button2" runat="server" onclick="Button_Click" Text="Compile" ForeColor="Black" />
</p>
<p>
<asp:TextBox ID="TextBox2" runat="server" Height="138px" Width="390px" ></asp:TextBox>
</p>
</div>
</section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
</asp:Content>
And here the Handler event for it using C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;
namespace WebApplication2
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
var engine = new ScriptEngine();
var session = engine.CreateSession();
String code = TextBox3.Text;
session.Execute(code);
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
}
}
}
How could I show the result of execution of Code (TextBox3) in TextBox2??
Iam beginner in ASP.Net environment, Any help??
I'm afraid I'm not near a dev pc, so I can't test the code, but here's what comes to mind:
You're not using the result of session.Execute(code);, so if a user were to put in 3+3, it would get executed, and the return value would be 6, but since it's not captured, it simply would go out of scope. If instead, you did object result = session.Execute(code);, the output would be the integer 6; You could then call .ToString() on result and put that in your text box.
And of course be careful allowing users to execute arbitrary code on your webserver...

Can't Access Textbox in ASP

I have a simple ASP.NET page with a button and a textbox, I want to click the button and have "Hello" appear in the textbox.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="SOM.aspx.cs" Inherits="SOM" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div><br />
</div>
<asp:Button ID="MyBtn" runat="server" Text="Click" OnClick="SearchBtn_Click" />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="268px"></asp:TextBox>
in the code behind I am able to use Intellisence to autocomplete TextBox1.Text = "Hello";
but then when I try to debug, the project fails to build saying
The name TextBox1 does not exist in the current context
Is there a property that I need to set?
///Code behind
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Input;
public partial class SOM: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SearchBtn_Click(object sender, EventArgs e)
{
//TextBox1.Text = "HI";
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
If any of the great answers here do not work, here's another solution that once caused me a few gray hairs, especially if you're using VS 2012.
If your project is a web application, delete the designer.cs file. Then right click on the aspx file and select "Convert to Web Application".
For as great as Visual Studio is, sometimes these stupid WTF things drive me absolutely nuts.
Your aspx page is not referencing the correct code behind.
In the head of your aspx page replace with
<%# Page Language="C#" AutoEventWireup="true" CodeFile="myName.aspx.cs" Inherits="myName" %>
That should work
NOTE: I now know this isn't the problem, I but updated my answer in case it solves someone else's problem.
If you are using a namespace somewhere between your codebehind or the aspx page, you have to make it consistent.
I will use "MyProject" as a sample namespace.
namespace MyProject
{
public partial class SOM: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SearchBtn_Click(object sender, EventArgs e)
{
TextBox1.Text = "HI";
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
In your aspx page, change Inherits="SOM" to Inherits="MyProject.SOM".

Session data from textbox inputed by user

I have this project where I want to transfer data from textbox1 with session "sam" on the about.aspx on its label. The problem is that when I input a number, it is not shown on label. My problem is that I type something on the "Αριθμός Επιβατών:" textbox (a number for example) and after that I click submit I need to get the number on the about page next to "Ari8mos epivatwn :"
Defaultaspxcs.txt:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click2(object sender, EventArgs e)
{
string txt = TextBox1.Text;
Session["sam"] = txt;
}
}
}
Aboutaspxcs.txt:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = (string)Session["sam"];
}
}
}
Aboutaspx.txt:
<%# Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="About.aspx.cs" Inherits="WebApplication4.About" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
Ari8mos epivatwn :
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>
</asp:Content>
https://www.dropbox.com/sh/4ftlddfhqo8n99p/gm-TNvol0S
It's likely that the content for the Label is being written twice, and not in the order your expect.
Start by familiarizing yourself with the asp.net page life-cycle
You will notice that PageLoad is called long before Render.
So, the page loads like this:
PageLoad Label text set to some number from your code
A bunch of other stuff
Render sets the Label text to "Label" instead, since that is what you have defined in your chtml.
Review this part of your code:
...
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
...
Try this instead:
...
<asp:Label ID="Label1" runat="server"></asp:Label>
...

Processing events on Dynamically Loaded User Controls

I have a number of pages that I need to Dynamically Load User Controls and process events on controls on them. I have included sample code below for a trivial example.
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 xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="BtnLoadControl" runat="server" Text="Load Control 1"
onclick="BtnLoadControl_Click" /><br />
<asp:Button ID="BntLoadControl2" runat="server" Text="Load Control 2"
onclick="BntLoadControl2_Click" />
<asp:PlaceHolder ID="ControlArea" runat="server" />
</div>
</form>
</body>
</html>
Deafualt.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)
{
}
protected void BtnLoadControl_Click(object sender, EventArgs e)
{
Control controlToAdd = Page.LoadControl("~/control1.ascx");
this.ControlArea.Controls.Add(controlToAdd);
}
protected void BntLoadControl2_Click(object sender, EventArgs e)
{
Control controlToAdd = Page.LoadControl("~/control2.ascx");
this.ControlArea.Controls.Add(controlToAdd);
}
}
Control1.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="control1.ascx.cs" Inherits="control1" %>
<div style="border: 1px solid red;">Test Control
<br /><asp:Button runat="server" ID="testButton" Text="test" onclick="testButton_Click" /></div>
Control1.ascx.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 control1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void testButton_Click(object sender, EventArgs e)
{
Response.Write("Button Clicked on Control 1!");
}
}
Control2.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="Control2.ascx.cs" Inherits="Control2" %>
<div style="border: 1px solid red;">Test Control 2
<br /><asp:Button runat="server" ID="testButton2" Text="test me"
onclick="testButton2_Click" /></div>
Control2.ascx.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 Control2 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void testButton2_Click(object sender, EventArgs e)
{
Response.Write("Button Clicked on Control 2!");
}
}
This is a very trivial example to show what I would like to do. My actual control is more complex and has several properties that I set for the control I load as I load it (in the default.aspx.cs files event handler).
With this example what happens is when the button is clicked on one of the loaded controls, the page is re-loaded and the control is no longer there. How can I keep the control loaded as well as process any events that happen on the control?
You're asking if you can keep the control loaded in the page?
You can't keep the control loaded in the page, because the page doesn't exist! It's recreated on every request. It's created from the markup (compiled or not), and from running any code in the CodeBehind. If you want your controls to be there after a PostBack, then you need to recreate them on the PostBack. If you want to process events on those controls, then you'll have to wire the controls to event handlers when you recreate them after PostBack. Then the events will fire.

Categories

Resources