Prevent loading of ContentPlaceHolder from MasterPage - c#

I have a masterpage, contentplaceholder and an .ascx page.
The user enters his username-password at Masterpage.
I want to prevent the load of the contentplaceholder, if the user enters wrong username&password combination. Currently I am just disabling it's visibility, which does the trick but the page is still loaded, goes to database etc. which is useless since all of them will not be shown anyway.

You can load the Controls dynamically.
private WebUserControl1 userControl;
protected void Button1_Click(object sender, EventArgs e)
{
if (loginOK == true)
{
buildControls();
}
}
private void buildControls()
{
userControl = (WebUserControl1)LoadControl("~/WebUserControl1.ascx");
PlaceHolder1.Controls.Add(userControl);
}
Dynamically added controls need to be recreated on every Page_load
(that includes PostBack). So always call buildControls() when a user
is logged in.

Related

Invoking a method from the parent page after user control has been loaded?

May I know if there is any way to invoke a method from the child page(.aspx) after the page load of the user control is finished?
Right now I have an issue of being unable to retrieve the value from the child page because the variables from the user control has not been assigned the value yet.
To put it simply
FROM MY .ASPX FILE
Page_Load(object sender, EventArgs e)
{
x = getValueFromUserControl();
}
FROM MY USER CONTROL
Page_Load(object sender, EventArgs e)
{
int x = getvalueFromDatabase();
}
getValueFromuserControl()
{
return x;
}
Since the ASP.NET Life Cycle goes from the child page(.aspx) page_load -> user control page_load, I am unable to retrieve the value of x.
That said, ideally I would not like to put the function in the child page and call it from the user control as the user control is being used in other pages.
In short, I would like to invoke a method from my .aspx page after the page_load in my user control ends, Thank you!
Get the value at a later page event:
protected override void OnLoadComplete(EventArgs e) {
x = getValueFromUserControl();
}
Unless, of course, there is a specific reason why you must get the value on Page_Load. There are other, probably more appropriate ways to handle this, but without knowing what x is and what you need to do with it, it is hard to give any other advice. For example, maybe the UserControl should fire an event that is handled by the page.

textbox does not accept a value in asp.net

This code a have written for an asp.net website, v2005
System.Web.UI.WebControls.TextBox txtEFName = new System.Web.UI.WebControls.TextBox();
phFname.Controls.Add(txtEFName);
placeHolder1.Controls.Add(TextBox1);
This code when executed, always shows the value of the textbox "" even if I enter some string.
Please help.
Dynamic controls need to be re-created each time the page loads. So you need to have that code execute during the Page_Init event. Note: You need to assign a unique ID for this control that stays the same each time the page loads.
Why all this?
Because the control is not contained in the markup (the .aspx file), you need to add it again every time the page loads. So how does it retain its value? The value will be stored in the ViewState, and as long as the control has the same ID it will be repopulated with the correct value.
To keep things running smoothly, let's put the code for adding the control in a separate function.
Private void AddMyControl()
{
System.Web.UI.WebControls.TextBox txtEFName = new System.Web.UI.WebControls.TextBox();
txtEFName.ID = something unique;
phFname.Controls.Add(txtEFName);
}
So we can call it from both the click handler and the Page_Init handler, but we only need to call it in the Page_Init if we have already clicked. So let's store that as a flag inside a Session variable (you can also store it in the ViewState if you like, but let's keep it that way for now). So our click handler will now look something like this:
void ButtonSomething_Click(Object Sender, EventArgs e)
{
AddMyControl();
Session["MyControlFlag"] == true;
}
Now we need our Page_Init handler:
Public void Page_Init(Object Sender, EventArgs e)
{
if(Session["MyControlFlag"]!=null && (bool)Session["MyControlFlag"])
AddMyControl();
}

Get a control inside another control

I've been racking my brain over this for a while, its been a while since I had to do this and know its possible I've done it in another project in the past that I don't have a backup of to refer to.
I have a Login View on a page inside the login view is 2 panels one panel with a login control (to login) and one panel with a createuserwizard (to register) and a second button to click to register.
I'm trying to hide the panel with the login control and show the panel with the register control via a button click but all I end up with is a null reference exception.
this is what I have currently.
protected void Register_Click(object sender, EventArgs e)
{
FindControl("LoginView1").FindControl("LoginPanel").Visible = false;
FindControl("LoginView1").FindControl("RegPanel").Visible = true;
}
I appreciate any help thanks.
I figured out what the problem was so I'll leave the question here for anyone who may have the same problem and stumble across this
I was so used to working with controls from a master page but within a page that's inside the master page you don't need the first findcontrol its simply:
protected void Register_Click(object sender, EventArgs e)
{
LoginView1.FindControl("LoginPanel").Visible = false;
LoginView1.FindControl("RegPanel").Visible = true;
}

User control not displayed after postback

I have a home.aspx page, where i have two panel. In first panel I have dynamically bound a User Control (for displaying meiny at left side) and in second I have displayed pages.
I bound user control dynamically at page load like.
if (!IsPostBack)
{
UserControl uc = (UserControl)Page.LoadControl("~/settings/Links/Navigation.ascx");
Accordion1.Controls.Add(uc);
}
when page first time loaded my usercontrol is bind and my menus are displayed, but when I clicked on any menu item it hides(user control),
Please help me, thanks in advance!
Put this line of code on Page_Init event of Page life cycle.
UserControl uc = (UserControl)Page.LoadControl("~/settings/Links/Navigation.ascx");
Accordion1.Controls.Add(uc);
Proper way:
protected void Page_Init(object sender, EventArgs e)
{
//MyControl is the Custom User Control with a code behind file
MyControl myControl = (MyControl)Page.LoadControl("~/MyControl.ascx");
//UserControlHolder is a place holder on the aspx page where I want to load the
//user control to.
UserControlHolder.Controls.Add(myControl);
}
If you use if (!IsPostBack) then after postback it will not be added to the page. At the first time you will be able to see the control on the page.
Reference:
ASP.NET Custom user control to add dynamically
How to: Create Instances of ASP.NET User Controls Programmatically
It's a dynamic control which must be recreated and readded to the page on every postback.
So this will work:
//if (!IsPostBack)
//{
UserControl uc = (UserControl)Page.LoadControl("~/settings/Links/Navigation.ascx");
Accordion1.Controls.Add(uc);
//}
For Dynamic control there is no need for !IsPostBack Property of page, Remove this property and use
UserControl uc = (UserControl)Page.LoadControl("~/settings/Links/Navigation.ascx");
Accordion1.Controls.Add(uc);
It must be loaded on each postback. keep the user control loading code outside if(!IsPostBack){}.

loading usercontrols dynamically - postback issue

Im trying to load a usercontrol dynamically on a aspx page however it works but i get postback issues?? I have a image button on usercontrol which I want to show a image however when i do click on button the page refreshes and does not show image?. I have a placeholder on aspx page and backend code i have this :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Control uc = Page.LoadControl("~/UserControls/Mycontrol.ascx");
placeholder1.Controls.Add(uc);
}
}
Do i need to do something in page_preinit or page_init ??
You should always reload the user control on the Page_init on each post back.
Dynamic controls added to the page must be added on every postback, not just the first one. Remove the !IsPostBack condition. Secondly, they have to be added during init or preinit, because that way, viewstate will be captured and restored properly (ASP.NET restores viewstate between init and load events).
Give the control an ID and also load the control in Page_init
uc.ID = "your id";

Categories