I am a newbie to asp.net. I have an asp.net page which uses a user control.
On Page_Load event of this control, I want to change the title of the parent aspx page.
I need help on this please.
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "New Title";
}
You can try create a property in usercontrol and next call this property using your instance of usercontrol in page like
In UserControl
protected void Page_Load(object sender, EventArgs e)
{
this.MyProperty = "This is a test";
}
public string MyProperty { get; set; }
`
In Page
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl11.PreRender += new EventHandler(WebUserControl11_PreRender);
}
void WebUserControl11_PreRender(object sender, EventArgs e)
{
string str = WebUserControl11.MyProperty;
this.Header.Title = str;
}
Set this in your USERCONTROL:
this.Page.Master.Page.Header.Title = "text text title title";
Related
I have an asp.net project with two web forms. I would like to pass all items of listbox3(which is home page) to textbox that is in statistics page.I try following code but didnt work.
Home page:
protected void Button5_Click(object sender, EventArgs e)
{
Response.Redirect("Statistics.aspx?ListBox3");
}
Statistics Page
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = ListBox3.Items;
}
}
You can't refer any controls which are not in the current page with its ID.
So it may required to keep the items from ListBoxin Session or pass it through QueryString
Home Page
protected void Button5_Click(object sender, EventArgs e)
{
string Items = string.Empty;
foreach(var item in ListBox3.Items)
{
Items += item + ",";
}
//Session["ListBox3"] = Items;
Response.Redirect("Statistics.aspx?ListBox3=" + Items);
}
Statistics Page
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = Request.QueryString["ListBox3"].ToString();
//TextBox3.Text = Session["ListBox3"].ToString();
}
protected void Button5_Click(object sender, EventArgs e)
{
string allItems;
for(int i = 0; i < ListBox3.Items.Count; i++)
allItems += ListBox3.Items[i].ToString() + "--";
Response.Redirect("Statistics.aspx?ListBox3=" + allItems.SubString(0, allItems.Length - 2));
}
Access it using
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = Request.QueryString["ListBox3"];
}
}
I want to call a method of a Content from a master page sending a Parameter to manipulate one label.
public partial class MasterCategoria : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSalada_Click(object sender, ImageClickEventArgs e)
{
produtosCategoria x = new produtosCategoria();
x.changeLabel("Salada");
}
}
Manipulating this button on this WebForm which is a Content
public partial class produtosCategoria : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void changeLabel(string name)
{
lblTexto.Text = name;
}
But this isn't working. How can I do this work?
Thank you guys, and sorry about my english.
The object of type produtosCategoria is already created and it can be accessed from your master page via this.Page.
So to change the label of your content page you can do as in the snippet below.
Also, I added a simple type check so you won't get an error if another content page is loaded
protected void btnSalada_Click(object sender, ImageClickEventArgs e)
{
// Check if it is the correct content page
if (this.Page.GetType() == typeof(produtosCategoria))
{
produtosCategoria x = (produtosCategoria)this.Page;
x.changeLabel("Salada");
}
}
Note: When code executes in the master page this is the master page and this.Page is the content page
I'm fairly new to ASP.Net but have used ASP Classic before.
I'm trying to figure out how to return a status message to my front-end page from a "code behind" page.
public partial class test: System.Web.UI.Page
{
private String msg;
protected void Page_Load(object sender, EventArgs e)
{
status.Text = msg;
}
protected void action(object sender, EventArgs e)
{
msg = "Hello world!";
}
}
When my page posts to itself I'm not able to see the msg I'm expecting in the status label of my front-end page.
I'm guessing this is because the Page_Load function is executing before my action is performed or something like that.
I hope it's clear what I'm trying to achieve, can anyone point me in the right direction?
Set your text on OnPreRender instead of OnLoad. It fires after the event and should be used to do as much UI as possible.
public partial class test: System.Web.UI.Page
{
private String msg;
protected void OnPreRender(object sender, EventArgs e)
{
status.Text = msg;
}
protected void action(object sender, EventArgs e)
{
msg = "Hello world!";
}
}
Typically if you're running through a few events this is the best way to do it - you don't know which order the events will fire in so you want your message to be set at the end. However unless you need to do anything more complex why not just set it in the event itself and get rid of the private variable and extra method call?
public partial class test: System.Web.UI.Page
{
protected void action(object sender, EventArgs e)
{
status.Text = "Hello world!";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!isPostBack)
{
status.Text = "First time on page";
}
}
protected void action(object sender, EventArgs e)
{
status.Text = "Hello world!";
}
You could achieve this using Session assuming you have button or any other other control that causes postback and that triggers action function.
public partial class test: System.Web.UI.Page
{
private String msg;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
Session["Message"] = null;
}
else
{
status.Text = Session["message"].ToString();
}
}
protected void action(object sender, EventArgs e)
{
msg = "Hello world!";
Session["message"] = msg;
}
}
protected void action(object sender, EventArgs e)
{
Response.Write("<script type=\"text/javascript\">alert('Your Message');</script>");
}
I have a website in c#, in the first page I have some listbox, I need that
the last user's choice goes to a label in other page, how can I do that?
In my code if
the user chooses a value a button is visible, and in the click event of that button
redirect to to another page, but I need that value in a label in the page2
if (ddlFunciones.SelectedValue.Equals("15"))
{
lblAgregarNuevoServicio.Visible = true;
//lblIdFuncion.Visible = true;
lblDescripcion.Visible = true;
//txtId_funcion.Visible = true;
txtDescripcionFuncion.Visible = true;
btnAgregarNuevaFuncion.Visible = true;
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnVerCargos_Click(object sender, EventArgs e)
{
if (btnVerCargos.Enabled)
{
ListBoxCargo.Visible = true;
}
else
{
ListBoxCargo.Visible = false;
}
}
protected void ListBoxCargo_SelectedIndexChanged(object sender, EventArgs e)
{
}
If this is in Asp.Net you can pass information between pages in a number of ways.
You can use the Session object
protected void ListBoxCargo_SelectedIndexChanged(object sender, EventArgs e)
{
Session["MyVar"] = ListBoxCargo.SelectedValue;
}
and in your other page
object value;
if (Session["MyVar"] != null)
{
value = Session["MyVar"]
}
OR
By passing them in the QueryString see Passing-variables-between-pages-using-QueryString
And using Request.QueryString["MyVar"]
and of course there are more, please explain what exactly are you trying to do...
Edit: Based on OPs comments:
In page1:
protected void Button1_Click(object sender, EventArgs e)
{
Session["Page1Value"] = ListBox3.SelectedItem.Text;
//Response.Redirect("~/Page2.aspx");
}
In Page2:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Page1Value"] != null)
{
Label1.Text = Session["Page1Value"].ToString();
}
}
Before you redirect the user to another page, store the selected value in the user's session.
protected void Button1_Click(object sender, EventArgs e)
{
Session["userSelectedValue"] = ListBox1.SelectedValue;
Response.Redirect("OtherPage.aspx");
}
On the other page just extract the selected value from the session.
For example:
protected void Page_Load(object sender, EventArgs e)
{
var selectedValue = Session["userSelectedValue"];
}
More than enough examples of working with session variables available on the interwebs.
I suggest you read up on ASP.NET Session State.
I've been trying to template control panels in my site so I can take a panel and populate it fully. I'm good up until the point where my event handling needs to access functions on my page. My current test will take me to a login redirect page. So how can I get this event handler to perform a redirect?
public class DebugButton : Button
{
public string msg;
public DebugButton()
{
this.Click += new System.EventHandler(this.Button1_Click);
this.ID = "txtdbgButton";
this.Text = "Click me!";
msg = "not set";
}
protected void Button1_Click(object sender, EventArgs e)
{
msg = "Event handler clicked";
}
}
*on the Page*
protected void Page_Load(object sender, EventArgs e)
DebugButton btnDebug = new DebugButton();
PnlMain.Controls.Add(btnDebug);
Really appreciate the help. Thanks!
To do a redirect you can use:
Note:
Assuming that your login page is named login.aspx and it is located in root folder of your website.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/login.aspx");
}
or
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("login.aspx");
}
If you want the event to have access to the page, then the page needs to subscribe to the click event.
Aka:
on the Page
protected void Page_Load(object sender, EventArgs e)
{
DebugButton btnDebug = new DebugButton();
btnDebug.Click += new System.EventHandler(Button1_Click);
PnlMain.Controls.Add(btnDebug);
}
protected void Button1_Click(object sender, EventArgs e)
{
// access whatever you want on the page here
}
I just found out that that System.Web.HttpContext.Current will get me the current context of the page. So long as the custom class is part of the app(this one is in the apps folder of course) I'm good to go. Heres a sample of my quick TestTemplate that I used to make a custom button.
public class TestTemplate : Button
{
public TestTemplate()
{
this.Text = "Click Me";
this.ID = "btnClickMe";
this.Click += new System.EventHandler(this.EventHandler);
//
// TODO: Add constructor logic here
//
}
public void EventHandler(object sender, EventArgs e)
{
//System.Web.HttpContext.Current.Server.Transfer("Default.aspx");
System.Web.HttpContext.Current.Response.Write("This is a test!");
}
}