Posts Tagged Code Markup

Passing Variables Between Web Forms and handling HTTP Post

Recently I used web forms to gather some user information.  Before I had always used pure HTML forms but I wanted to provide validation with asp.net validation handlers so I needed some web controls to do this.

The one problem I ran into is how to get the information from the forms.  In my scenario I have two pages:

  • User_Information.aspx (gathers users email) FORM POST
  • Add_Info.aspx (Checks if user is already in system, emails the user, and posts a response so the user can see what happened)GATHER USER INFORMATION FROM USER_INFORMATION.ASPX

This is the setup(2 pages) I thought would work best.  The only problem is that accessing form data from another page is not so simple and I played around with a lot of ideas.  Upon searching there are many ways to gather posted information from a form (session variables, etc.) Frankly I can’t remember all of them.  Here is one idea I thought might work and was really frustrating me.

        Dim mailrecipient As String
        mailrecipient = PreviousPage.Request.Form("txtEmail")

At first I thought my form post was not even working.  After stepping through the sub routine with the feature in visual studio, I realized it was finding the posted material but somehow it was not grabbing what I needed.  The problem is that when you use a web control asp.net assigns the control it’s own id.  ASP.NET assigned my textbox for email this

“ctl00$ContentPlaceHolder1$txtEmail”

No wonder the above code didn’t work.  I was using the wrong name (txtEmail).  How could I reference the above name?  Does this name change?  Several things bounced through my head and I came up with the following to remedy my problem with the help of some other people’s posts.

        Dim postedValues As NameValueCollection = Request.Form
        Dim nextKey As String
        For i As Integer = 0 To postedValues.AllKeys.Length - 1
            nextKey = postedValues.AllKeys(i)
            If InStr(1, nextKey, "txtEmail") > 0 Then
                mailrecipient = Trim(postedValues(i))
            End If
        Next

This may not look so pretty but it works. What it does is:

  1. postedValues grabs the entire form collection (in my case this is not much because I have only two textboxes but there are other things included in this collection like Viewstate and other things I don’t know too much about)
  2. After the collection is grabbed it goes through the form loop so I can find my control
  3. I use an InStr(in string) function to find the controls name (txtEmail) within that big long ASP.NET generated id (ctl00$ContentPlaceHolder1$txtEmail)

While it is not elegant it does work.  One reason I did it this way is that I am unsure of something.

  • Is the collection always returned in the same order?  If it is then I could grab the whole collection and then just use postedValues(3) or whatever the position(not 3) of the web control I needed was.  (The position is 0 based.)

I ran my code in the receiving page’s(Add_Info.aspx) Page_Load event. Hope it helps someone.



Other Links

ASP.NET Forum Discussion about How to: Pass Values Between ASP.NET Web Pages


Tags: , , , , ,

Putting HTML and other code into your Wordpress post

The problem

Again I am not really sure why these basic things are overlooked and WordPress is impressively powerful in other areas…. The simple task was to add some html tags into my blog but I did not want them rendered… So I wanted to show HTML like (</p>
<p>
or <br />) or whatever else trips your trigger without my browser reading them like a webpage(hands off broswer).

This is no easy task apparently…. This post on the WordPress Website about Writing Code in Your Posts makes it sound so easy but it did not help me much…..other than learning these few:

< = &lt;
> = &gt;
/ = &#47;
] = &#93;
[ = &#91;
" = &#34;
' = &#39;

Using these has worked for me in post titles but I can’t get them to stay in my actual posts(post body) so I looked for something to help.


The Solution / Answer / Help

I found another Plugin to take care of my needs.  It seems there is a Plugin for everything… I am still testing this with my version(2.8.6) of WordPress but it worked… kind of… Until I find I better solution I will pass it on… The plugin is called…


Code Markup

I used version 1.3…. It was a little quirky but did work after some negotiations between the 2 of us… It could be that this is quirky because I have other Plugins installed. Whatever the case I got it to work… Read on to learn more…)


Note: I found that if I saved while in HTML mode of my post editor it worked fine.  However, if I saved in the visual mode of the the WordPress editor it would erase my html and throw in a bunch of crazy code(random HTML)… The editor just can not leave some things alone. The internal editor for WordPress makes changes to what you post when you save it… It means well but this can leave anyone frustrated. First it was <br /> tags… like I talk about in this post about Adding Line Breaks <br /> With WordPress. All of this is a bit annoying.  It did eventually (knock on wood) giving me a working post with the HTML showing up… I tested it in I am open to other options but for now this will do…  Apparently you can also add other types of code with this Plugin but I have not tried yet.


Tags: , , , , ,