in

ASP.NET Weblogs

[karsten samaschke]

ASP.NET daily. Or weekly.

February 2003 - Posts

  • How to do a Response.Redirect to another frame

    One of the most often asked questions is:

    Is it possible to do a Response.Redirect to another frame or a new window?

    There is just one answer to this question: No, it isn't. Nope. No chance. No way. Never. Forget it.

    Why isn't this possible?

    It is not possible because of frames and windows are client-side concepts. The server just don't knows about these concepts. And if it does not know about it - how should it be able to address a different window or frame?

    The workaround

    The only possible workaround is the usage of JavaScript. Simply register a client-side script doing the desired redirect by using window.open or <parentframe>.location.href. Or use targets in your forms.

  • How to change the text in a panel

    A tricky question is this one:

    How can I change the text in an asp:Panel?

    Let's assume, a panel like this is given in your ASPX-page:

    <asp:Panel ID="thePanel" runat="server">Hi, this is some text</asp:Panel>

    Usually, you would start searching for a .Text- or .InnerText-Property. But - and this is the sad news - no such property exists. You don't have the chance to change the panel's text.

    Really?

    No, of course you can change the text. See the inner text as a LiteralControl and it will work. In your CodeBehind, you'll have to write code like this:

    [VB.NET]
    Dim objPanelText As LiteralControl = _
       CType(thePanel.Controls.Item(0), LiteralControl)
    objPanelText.Text = "Welcome to my VB.NET-page!"

    [C#]
    LiteralControl objPanelText = thePanel.Controls[0] as LiteralControl;
    objPanelText.Text = "Welcome to my C#-page!";

    That's it. Really simple, isn't it?

  • How to handle dynamically created buttons in ASP.NET

    This problem is very often discussed in Newsgroups:

    How can I handle clicks on dynamically created buttons?

    Ususally you're adviced to declare the buttons in the CodeBehind of your aspx-Page. But this is not the way to create buttons (or other controls) dynamically, because there is no real dynamic in the process.

    If you create buttons on-the-fly, you do not have the possiblity to use their eventhandlers. Take a look at the following codesnippet:

    Dim objButton As New Button
    objButton.Text = "Click me!"
    Page.Controls.Add(objButton)

    Once you added the button to your page, you don't have a chance to react on the click.

    Really?

    What, if there was a chance to react on the button's click? Even if this reaction was not handled by the button's eventhandler? Look at this:

    ' Registering a hidden field in the Page
    Page.RegisterHiddenField(Me.UniqueID & _
       "_buttonClicked", "")

    ' Defining the Button
    Dim objButton As New Button
    objButton.Text = "Click me!"

    ' Here's the trick... :)
    objButton.Attributes.Add("onClick", _
       "document.forms[0]." & Me.UniqueID & _
       "_buttonClicked.value='" & _
       objButton.UniqueID & "';document.forms[0].submit();")

    ' Adding the button to the page
    Page.Controls.Add(objButton)

    What was done here? Really simple: We added a JavaScript to the button which sets a value in the hidden field. On PostBack we're able to track this hidden field and detect, wheter the button was clicked or not:

    ' Use this snippet on PostBack
    If Page.IsPostBack Then
       If Request.Item(Me.UniqueID & _
          "_buttonClicked").Length > 0 Then
          Response.Write("Button " & _
             Request.Item(Me.UniqueID & _
                "_buttonClicked") & _
                " was clicked!")
       End If
    End If

    On this place you can add your own handling of the event or raise another event. In any case: You are able to track, wheter the button was clicked or not. Problem solved. :)

  • Problems starting debugging with ASP.NET and VS.NET

    An often asked question in the Newsgroups goes like this:

    Hey, my Visual Studio isn't able to start debugging on the server. What's wrong with it?

    Usually, only a few points need to be adjusted. Microsoft has provided an article covering this topic in their Knowledge-Base. It is available in different languages, and I took the English and the German versions:

       http://support.microsoft.com/default.aspx?scid=kb;en;306172 (English)
       http://support.microsoft.com/default.aspx?scid=kb;de;306172 (German)

  • About this WebLog

    So, finally. It got me. I'm in. Yes, even me.

    Who I am

    My name is Karsten Samaschke, I live in East-Berlin, Germay. I'm 26 years old and I'm not married, but I have a beautiful girlfriend.

    What I do

    Basically: Write this WebLog ;)

    No, to be honest, I'm a programmer (ASP / VBScript, ASP.NET, C#, VB.NET, J#, J2EE), I write books and technical articles, became awarded twice as Microsoft MVP, sometimes I'm on conferences as a speaker and you may book me for trainings on the above stuff.

    What this WebLog will be about

    The topics covered here are my bad English, my bad humor and my bad programming abilities :))) 

    You will find some snippets of code on the page and - hopefully - useful links for your work with ASP, ASP.NET and related stuff. On such a weblog must be a place for personal thoughts - but I will try to place them in the appropriate folder, so you don't have to read them, if you don't want to.

    So, let's have some fun!

More Posts