How to refresh another user control declared in a ascx page from button click event of that page(ascx.cs)?
You can load it dynamically.
First from page load event
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
LoadControl();
}
}
And then load it from a button again on which you want to reload it
protected void Button_Click(object sender, EventArgs e)
{
LoadControl();
}
private void LoadControl()
{
usercontrols_SimpleControl ucSimpleControl = LoadControl("~/UserControlName.ascx") as usercontrols_SimpleControl;
Placeholder1.Controls.Add(ucSimpleControl);
}
Related
I have a MasterPage, and a Content page.
Whenever a method is called on the content page that does a redirect, EG:
protected void DoLogin(object sender, EventArgs e)
{
var result = AttemptLogin();
if (result.Success)
{
Response.Redirect("~/home");
}
else
{
... show error message
}
}
It appears Page_LoadComplete is not fired. The master page is set up as:
protected void Page_Init(object sender, EventArgs e)
{
Page.LoadComplete += Page_LoadComplete;
}
void Page_LoadComplete(object sender, EventArgs e)
{
DoSomething();
}
Should a redirect be stopping this from firing?
I'm trying to set an initial selection for a DropDownList by calling: drop.SelectedIndex = 5 in Page_Load.
This works, but then if I change the selection manually and want to save the form, I'm still getting the initial selection instead of the new selection when calling drop.SelectedValue. What's wrong?
You have forgotten you check if(!IsPostback). Otherwise you will select the 6th item again on postbacks before the SelectedIndexChanged event is triggered (or a button-click event):
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack) // do this only on the initial load and not on postbacks
dropDwonList1.SelectedIndex = 5;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//set up data here
}
}
if (Page.IsPostBack)
{
//do page reload logic in here
}
protected void foo(object sender, EventArgs e)
{
//get your selected value here
}
Try this code
You should be using if(!IsPostback) in the Page_Load function.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
drop.SelectedIndex = 5;
//yourcode
}
}
Through this your problem will be solved
I have this:
protected void Page_Load(object sender, EventArgs e)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
}
If Profile.dados_pessoais.nome is empty, nome_txt.Text is empty too. When I change nome_Txt.Text to teste for example, when I click on the button nome_Txt.Text is empty.
What am I doing wrong?
The Page_Load event run before the button click event so you always assign the text box to empty value.
To solve this, don't populate the textbox when you are in a Postback:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
}
As also stated in a comment, you probably have to save the profile after changing it otherwise it won't be saved when you next load the page:
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
Profile.Save()
}
In my application, i am trying to get button_click event in another button click event, but it is not working.
My code is,
protected void btn1_Click(object sender, EventArgs e)
{
Response.Write("hi.....");
}
protected void btn2_Click(object sender, EventArgs e)
{
btn1.Click += new EventHandler(this.btn1_Click);
}
What is wrong with this?
I suppose you want to call the code of btn1_click event.
If this is the case, you simple call it as a method in your btn2_Click.
protected void btn2_Click(object sender, EventArgs e)
{
btn1_Click (sender, e);
}
I want to have a user control containing a save button.
I want the click event attached to the save button to be handled by the containing page not the user control
Is this possible?
EDIT MORE SPECIFIC
Say I have a page called "Editor Page" and a user control called "Editor Buttons".
On the "Editor Buttons" user control I have a save button (which is a web control). I want the Click event of the save button to be handled by the "Editor Page" rather than the user control.
The reason for this is because I want to specify base pages that implement common behaviour.
This technique is commonly referred to as “event bubbling”.
Here is an exmple:
public partial class Test : System.Web.UI.UserControl
{
public event EventHandler buttonClick;
protected void Button1_Click(object sender, EventArgs e)
{
buttonClick(sender, e);
}
}
Then you can subscribe the event buttonClick at webpage to display the different information .
public partial class Test: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControlID.buttonClick+=new EventHandler(UserControlID_buttonClick);
}
protected void UserControlID_buttonClick(object sender, EventArgs e)
{
Response.Write("hello,I am jack.");
}
}
I will assume that you have the code for the UserControl. If that is the case, you can define a Save event in your user control and have it expose the Click event of the save button, either directly, or by internally setting up an event handler and raise the Save event when the button is clicked. That could look something like this in the UserControl:
public event EventHandler Save;
private void SaveButton_Click(object sender, EventArgs e)
{
OnSave(e);
}
protected void OnSave(EventArgs e)
{
// raise the Save event
EventHandler save = Save;
if (save != null)
{
save(this, e);
}
}
UserControl1.ascx
UserControl1.ascx.cs
public partial class UserControl1 : System.Web.UI.UserControl
{
public event EventHandler UserControl1Click;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_Click(object sender, EventArgs e)
{
UserControl1Click(sender, e);
}
}
UserControl2.ascx
UserControl1.ascx.cs
public partial class UserControl2 : System.Web.UI.UserControl
{
public event EventHandler UserControl2Click;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn2_Click(object sender, EventArgs e)
{
UserControl2Click(sender, e);
}
}
Then add one Asp.Net Text box(to display user control id) and above two user controls to Aspx page as shown below.
<title>Untitled Page</title>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox> <br />
<UC1:UC ID="uc1" runat="server" />
<UC1:UC ID="uc2" runat="server" />
</div>
</form>
Now add event handler for each user control to handle button click event of two controls as shown below.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
uc1.UserControl1Click += new EventHandler(uc1_UserControl1Click);
uc2.UserControl2Click += new EventHandler(uc2_UserControl2Click);
}
protected void uc1_UserControl1Click(object sender, EventArgs e)
{
txt1.Text = "User Control 1 Clicked";
}
protected void uc2_UserControl2Click(object sender, EventArgs e)
{
txt1.Text = "User Control 2 Clicked";
}
}
If we click User Control1, text box displays “User Control 1 Clicked” or if we click on User Control2, text box displays “User Control 2 Clicked”.