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>
...
Related
I'm trying to generate a user control, which consists of a label, dropdown menu, and text box, dynamically so I can set the value of the label. When I run it, I have an issue with the dropdown and textbox because they need to be in a form tag with runat server. When I add the form tag, the compiler complains that there can only be one form tag. Please help. This is my code:
User Control:
<%# Control Language="C#" AutoEventWireup="true"
CodeFile="IngredientQuantityControl.ascx.cs" Inherits="IngredientQuantityControl" %>
<asp:Label ID="IngredientDisplay" runat="server" Text="Label"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem Value=null >Please Choose a Unit</asp:ListItem>
<asp:ListItem Value="Unit">Unit(s)</asp:ListItem>
<asp:ListItem>Cup</asp:ListItem>
<asp:ListItem>Tbsp</asp:ListItem>
<asp:ListItem>Tsp</asp:ListItem>
<asp:ListItem>To Taste</asp:ListItem>
<asp:ListItem>Milligrams</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
CodeBehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class IngredientQuantityControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetLabelText(String s)
{
IngredientDisplay.Text = s;
}
}
Dynamically Adding to the panel
protected void Button1_Click(object sender, EventArgs e)
{
Page.Controls.Add(LoadControl("IngredientQuantityControl.ascx"));
IngredientQuantityControl uc = (IngredientQuantityControl)Page.LoadControl("IngredientQuantityControl.ascx");
uc.SetLabelText("Halleluyah!");
Panel1.Controls.Add(uc);
//string text = listBox1.GetItemText(listBox1.SelectedItem);
}
}
You are adding the IngredientQuantityControl.ascx as control twice.
protected void Button1_Click(object sender, EventArgs e)
{
IngredientQuantityControl uc = (IngredientQuantityControl)Page.LoadControl("IngredientQuantityControl.ascx");
uc.SetLabelText("Halleluyah!");
Panel1.Controls.Add(uc);
//string text = listBox1.GetItemText(listBox1.SelectedItem);
}
i want to show a value in DropDownList at page load and the value is "1 Year".
here is my Database Table
id instal
0 Choose
6 6 Month
12 1 Year
24 2 Year
36 3 Year
here is my ASPX code:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="instal" DataValueField="id"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:paconn %>"
SelectCommand="SELECT * FROM [cms_instal] ORDER BY [id]">
</asp:SqlDataSource>
</asp:Content>
and its my C# code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using System.Web.SessionState;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.Text = "1 Year";
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
but problem is that when my page is loaded it gives an error
"'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value"
i am using ASP.Net C#, SQL Server 2008.
The text property of a DropDownList gets or sets the SelectedValue. Since your DataValueField is set to "id", you would need to set the Text property to "12".
Move the text to the LoadComplete event
void Page_LoadComplete(object sender, EventArgs e)
{
DropDownList1.Text = "1 Year";
}
The list binding has not occurred in page load so the list is not able to take another value which is not in the list. Setting the value after page load will allow it to use an existing value.
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...
Here is all my code:
Site.Master
<%# Master Language="C#" AutoEventWireup="true"
CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
</form>
</body>
</html>
Site.Master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication1._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
</asp:Content>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ListBox ListBox1 = new ListBox();
ListBox1.FindControl("ListBox1");
ListBox1.Items.Add(new ListItem("Hello"));
}
}
}
Question: Why is my ListBox not populating?
When running the code I get a page with a small box that is completely empty.
I tried the following already and I will tell you the result -
Remove
ListBox ListBox1 = new ListBox();
ListBox1.FindControl("ListBox1");
Result: The name 'ListBox1' does not exist in the current context
Add a reference to ListBox1 manually into the designer.
Result: Object reference not set to an instance of an object.
To fix that error adding the line ListBox ListBox1 = new ListBox(); works, but still does not display.
Any ideas? Thanks.
You should only need...
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Add(new ListItem("Hello", "H"));
}
See this link too
EDIT: Same code works for me.
To directly answer your question of why ListBox1 isn't getting populated:
protected void Page_Load(object sender, EventArgs e)
{
ListBox ListBox1 = new ListBox(); //You are creating a new local ListBox named ListBox1.
ListBox1.FindControl("ListBox1"); //You are trying to find the Control named ListBox1 that is held within the Control ListBox1 (and not doing anything with the result of the search)
ListBox1.Items.Add(new ListItem("Hello")); //You are adding a new ListItem to the local ListBox1.
}
As christiandev said, you should just be able to do this:
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Add(new ListItem("Hello"));
}
I have no idea why you can't seem to access the ListBox1 on the aspx page.
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.