what is the difference loading Web user control loading on page? - c#

i try to load some user control on my Default.aspx page selecting dropdown control. i searched some data from net i 've learn 2 methods there is first one :
http://blah.winsmarts.com/2006/05/20/loadcontrol-a-usercontrol--and-pass-in-constructor-parameters.aspx
Second one:
http://www.csharpnedir.com/articles/read/?filter=&author=&cat=aspx&id=689&title=Kullan%C4%B1c%C4%B1%20Web%20Kontrollerini%20Daha%20Etkin%20Kullanmak
Secand one is simple:
protected void Page_Init(object sender, EventArgs e)
{
AdresBilgisi kontrol1=(AdresBilgisi)LoadControl("AdresBilgisi.ascx");
AdresBilgisi kontrol2 = (AdresBilgisi)LoadControl("AdresBilgisi2.ascx");
kontrol1.Ilce = "İlçe giriniz...";
kontrol2.PostaKodu = "90000";
phKontroller.Controls.Add(kontrol1);
phKontroller.Controls.Add(kontrol2);
}
which one do you prefer to loadASCx control to page? And Why? please give some detail pros and cons of 2 method(first and second)

Your second approach is the preferred one. Because you have more control of the UI and you can see the UI layout of your desired location where you put it on the page.
Regarding the first approach; it can be used unless there is some special need. e.g. If you want to load a user control at runtime depending on some situation/condition.

Related

C# web forms - How to pass data from master page to master page when redirect is on web form?

When on master page I have dropDownList with event. When event Raise I want to load/redirect webForm.aspx. In this webForm.aspx I want to load data based on dropDownList selected item. At the same time I want to load again dropDownList and select Item on which clicked was.
I created some solution, where I was using Response.Redirect("webform.aspx?courseID=4") on masterPage.
Is this good approach to this problem? I think there must be better, but I cant see it.
Thanks for answers.
One way is your solution: to use querystring.
The second way is: on dropdownlist's value changing you changing value of some session value (e.g. *Session["course_id"]), then Response.Redirect("webform.aspx") and restore by Session["course_id"] value.
protected void ddlCourses_SelectedIndexChanged(object sender, EventArgs e){
Session["course_id"] = ddlCourses.SelectedValue;
Response.Redirect("webform.aspx");
}
ABOUT YOUR SECOND QUESTION (HOW TO TAKE ROUTE VALUE FROM MASTER PAGE). I will give just brief explanation and a little example.
Create in your master page some method:
protected void IWantToWarkWithRouteValue(){
//here you will do something
//and to take value from routing, just use Session["myValue"] (I will explain this value later)
}
You should call this method in Page_Load, not in Pre_Init or Init
Then in your Course.aspx (caz' you using routing here) create the next method:
protected void SetCourseSessionFromTheRoute(){
if (Page.RouteData.Values["courseID"] != null)
Session["myValue"] = Page.RouteData.Values["courseID"].ToString();
}
And call this method in Page_PreInit:
protected void Page_PreInit(object sender, EventArgs e){
SetCourseSessionFromTheRoute();
}
Thanksgiving to ASP.NET page's life cycle, you may do anything you need.
You may read about page's lifecycle here
As I said, it is just an example. You may find a better solution.
Hope it helps.

How to write a page whitelist in an asp.net website

I have a website and as one of security options (prevent path traversal) I am planning to use a white list of pages which can be accessed. But my problem is I don't know how to do it. Can you share some articles or simple of code how to create a white list?
How many pages does the site have? If there are not too many pages and if pages are not added/removed frequently you can manually create a list of pages. If you are using master pages you can set this up in Page_Load method.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Dictionary<string,string> allowedUrls = LoadAllowedURLs();
if (!allowedUrls.ContainsKey(Request.Path))
{
Response.Redirect("Some_default_redirect_page.aspx");
}
}
}
Now, if you do have a lot of pages you would need more sophisticated solution that uses web.config and more…
If I understand the question correctly, you would like to allow certain users access to certain pages on your website - have you considered ASP.NET Security Trimming?

Activating a ASP Multiview control's view from other page without querystring/session

I was looking for an alternative that may be used to activate a multiview control of other page without passing a querystring/session variable.
Basically, my Home.aspx page has a link that takes us to a specific page say "NewPage.aspx". The NewPage.aspx page has a multiview control that has three child views.
I want to click on the link of the Home.aspx and go to NewPage.aspx with MultiView1.ActiveViewIndex=1. Please remember that I do not want to pass any querystring variable as that link already contains some encrypted data as querystring and adding another variable can cause the data to corrupt. Maintaining a session isn't a solution as well because the application is quite big.
Any inbuilt method that can activate that view? (I don't seem to be talking practical but any help is really appreciated)
If you are asking about how navigate to this page I would wire up a button event (I prefer to do so in OnInit). Something like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.btnlinkclick.Click += new EventHandler(btnlinkclick_Click);
}
void btnlinkclick_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 1;
}
This should work for you.

Maintained state of a web page

In my web app I has a GridView and some other control. User is allow to sort and filter the grid view.They also allow to click the link and go to other page.Then they also can go to other more pages. But when they cum back to the first gridview page. The grid is same as what they left the page. For example paging ,sorting and other .
I found a solution about this . But I not really understand.
http://www.codeproject.com/Articles/7655/Persisting-the-state-of-a-web-page
Here is my coding WebForm1.aspx
protected void Button2_Click(object sender, EventArgs e)
{
PersistentStatePage abc = new PersistentStatePage();
abc.RedirectSavingPageState("WebForm2.aspx");
}
WebForm2.aspx
protected void Button1_Click(object sender, EventArgs e)
{
PersistentStatePage.RedirectToSavedPage("WebForm1.aspx", true);
}
Can anyone guild me some example?
That codeproject article is not really meant for this sort of scenario. It is about saving viewstate to a secure, on server location rather than sending it all to the user in a hidden input field which leads to a lot of bloat.
The best way to go about this would be session.
Create a custom object to store the info you want to persist and put it in session.
Check out the following for an article about using session http://asp.net-tutorials.com/state/sessions/
Then you don't have to worry about passing the details around all the other pages.

If one page executes a Response.Redirect() to a different web page, can the new page access values in asp.net controls from the original page?

I have a text string value that I'd like to persist from one web page to another without using query strings or the session/view states. I've been trying to get the ASP http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx">HiddenField control to pass information from one web form to a different form.
All the hiddenfield control examples that I've seen is to preserve round trips from the client to the server for the same form.
Is there way for a form to access the ASP controls (and their values) from the previously-rendered form? Or is the initial form simply disposed of in memory by the time the second form executes it's OnLoad method?
Quick answer is no. As others have noted, you can use Server.Transfer and then you can - however this is to be used with caution. It is a "server side redirect" eg.
Your user is on http://mysite.com/Page1.aspx they click a button and you perform a Server.Transfer("Page2.aspx"). Page2.aspx will be rendered in their browser, but the URL will still be Page1.aspx, this can cause confusion and mess up back/forward navigation.
Personally I would only use Server.Transfer as a last resort - in the world of the web, sharing data across pages generally means you need to use a storage mechanism; Cookie, QueryString, Session, Database - take your pick.
You can't get the previous page fields with Response.Redirect.
You can with cross page posting :
if (Page.PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
}
}
If both pages live in the same application you can use Server.Transfer:
firstpage.aspx:
protected void Page_Load(object sender, EventArgs e)
{
Server.Transfer("~/secondpage.aspx");
}
secondpage.aspx:
protected void Page_Load(object sender, EventArgs e)
{
Page previousPage = (Page) HttpContext.Current.PreviousHandler;
Label previousPageControl = (Label) previousPage.FindControl("theLabel");
label.Text =previousPageControl.Text;
}
A somewhat better solution would be implementing an interface on your first page where you expose properties for the values needed by the second page.
I would presume that the Response.Redirect() sends a Location: HTTP header to do a redirect.
As HTTP is stateless, I'd also presume that these variables are inaccessible.
There are however, solutions.
Print a form with hidden fields, and use javascript to submit it
Redirect in the code internally (load up the thing it needs to get to manually)
Store the data in some temporary database table somewhere, and pass along a unique ID
However, from my experience, I can't understand why you might need to do this (other than re-submitting a form after a user authentication - which hopefully you should be able to use method 2 for
Remember, a Response.Redirect instructs the browser to issue another request to the server. So far as the server is concerned, this next request is indistinguishable from any other incoming request. It's certainly not connected to a previous form in any way.
Could you explain your aversion to storage in the session, so we can propose some viable alternatives?

Categories