http___programlamaipuclari.blogspot.com.tr_2011_11_aspnet-ispostback-ozelligi.html_1.xml
1.1 1.2 2.1 2.2 3.1 3.2 4.1 4.2 5.1 6.1 8.1 10.1 11.1 11.2 11.3 11.4 12.1 12.2 12.3 12.4 12.5 13.1 13.2 14.1 14.2 17.1 18.1 18.2 18.3 19.1 21.1 21.2 22.1 22.2 22.3 22.4 22.5 24.1 25.1 26.1 26.2 27.1 27.2 28.1 28.2 28.3 29.1 30.1 30.2 30.3 31.1 31.2 31.3 31.4 31.5 34.1 34.2 35.1 35.2 35.3 36.1 37.1 37.2 38.1 38.2 39.1 40.1 40.2 40.3 41.1 42.1
For some time I would just comment out this code until the rest of the module was complete. The problem with that is that as the application became more & more sophisticated, there was assorted initialization code elsewhere that has to be executed each & every time. For the Run-Debug cycle it became exceptionally time consuming (and annoying) to manually go through the entire login process every time. Here's an example of the steps involved: Oh sure, it's just a few keystrokes and mouse clicks each time but repeated dozens (or hundreds!) of times per day, one soon hopes for a way to automate the process. And I found it! In Visual Studio's Solution Explorer, right-click on whatever page you're working on and choose "Set As Start Page" from the popup menu. Note: You've probably already done this! Now in the Page_Init event handler of your Master Page (FYI all my projects use Master Pages) add this code: protected void Page_Init(object sender, EventArgs e) IsPostBack) // If we're on a development machine then record the Start Page that Visual Studio is set to string currPage = Page. ResolveUrl(Request. ServerVariables["URL"]. ToString()); if (System. Diagnostics. Debugger. IsAttached && SessionData. GetCurrentUser() == "") if (SessionData. GetSessionObject("CurrentDebugPage") == null) SessionData. SaveSessionObject("CurrentDebugPage", currPage); Then in the Page_Load event handler of your login page add this code: // Note: This must be set above: using System. Web. Security; protected void Page_Load(object sender, EventArgs e) // This tests whether we're running on a development machine. If so, we'll automatically login. if (System. Diagnostics. Debugger. IsAttached && SessionData. GetCurrentUser() == "") string currUser = "your_username"; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(currUser, false, 5); string encryptedTicket = FormsAuthentication. Encrypt(ticket); HttpCookie authCookie = new HttpCookie(FormsAuthentication. FormsCookieName, encryptedTicket); Response. Cookies. Add(authCookie); // If a specific page has been set as the Start Page then redirect to it; otherwise go to the Default page string currDebugPage = SessionData. GetSessionObject("CurrentDebugPage"). ToString(); HttpContext. Current. Response. Redirect((currDebugPage == null) ? "~/default.aspx" : currDebugPage, true); Note: The various SessionData methods are just simple ones I've constructed to ease the process of storing & retrieving variables in the Session State. I will gladly provide them upon request. The result is that now, every time I run my project in Visual Studio, it jumps right to the desired web page, logging me in automatically. And yet when run in non-Debug mode it works normally. So I don't have to alter any code whenever I release a new production version. In case you're wondering how this works, the process is quite straightforward: - The Master page's Page_Init event handler is run before every Content page's Page_Load event handler. It records the start page that the developer wanted to go to, placing this string into a Session variable. - When that desired page's Page_Load event handler is initially entered, the security checks therein reject entry because the user is not logged in. They then redirect to the Login page. - In the Login page the special test in Page_Load passes because the code is being run from Visual Studio and because the user is not yet logged in. - A FormsAuthenticationTicket is instantiated and then placed in a Cookie. This allows one to artificially login through the ASP. Net Login mechanism. - The desired start page, originally stored by the Master Page, is retrieved from the Session State and control is redirected to that page. - This second time the security checks in the desired page's Page_Load event handler pass because the user is now officially logged in.
1.1 2.1 3.1 5.1 6.1 8.1 10.1 11.1 13.1 14.1 15.1
Sayfanın gene kendine yönlenmesine kısaca postback adı verilir. IsPostBack, page nesnesinin bir özelliğidir. Sayfanın geri postalama durumunu yönetir. Sayfanın ilk yüklendiği durum ile daha sonradan PostBack(geri bildirim) yapılması durumundaki işlemler bu özellik sayesinde belirlenebilir. Peki, bunun anlamı nedir? Konuyu anlamak için bir örnek yapalım. protected void Page_Load(object sender, EventArgs e) if (!Page.IsPostBack) LabelMesaj.Text = "Sayfa postback olmadı."; protected void ButtonGonder_Click(object sender, EventArgs e) if(Page.IsPostBack) LabelMesaj.Text = "Sayfa postback oldu."; Sayfama bir label bir de buton ekliyorum. Sayfa ilk yüklendiğinde şöyle bir ekranla karşılaşıyorum : Butona bastıktan sonra ise şöyle bir ekranla karşılaşıyorum: Butona bastıktan sonra sayfam sunucuya gönderiliyor. Sunucu sayfayı işledikten sonra geri gönderiyor. Sunucunun sayfayı geri göndermesi işlemine postback adı veriliyor. Page.IsPostBack bool bir değer tutuyor. Eğer sayfam postback olmuşsa true, değilse false değerini alıyor.