Dynamic WebForm!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HyperManTT
    New Member
    • Nov 2009
    • 6

    Dynamic WebForm!

    Hi guys..I was recently asked to create a console application that takes the content of a text file and creates a webform in aspx format based on the the text file. For example if the text file contains " FirstName,TextB ox,LastName,Tex tBox" the console application would read this file and produce a webform with the labels FirstName and LastName and a text box to entire the desired information. Since the contents of the text file can change so can the webfom. I am new to c# and any help you guys can give would help alot. Oh it was hinted that I create a test webpage and use it as a template..I don't understand how that works though.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    This sounds like class homework. There is only so much we can do for coursework.

    Stop. Think about the steps that your program will have to go through, then make sure you know how to do each little piece on its own. Once you can do the descrete little functions *then* worry about threading them together.

    Can you read a text file?
    Can you break down the contents at the commas?
    Can you send your code to different functions based on the dismantled text file?
    Can you programmaticall y add a control to a webpage file?
    Can you determine the size and location of that control?

    Comment

    • HyperManTT
      New Member
      • Nov 2009
      • 6

      #3
      Thank you so much for the reply and I have indeed been doing my research and have found how to read a text file one word at a time. I was wondering after reading each word, can I have like a series of 'if' conditions that would determine the type of control to add to the webpage. For example " if (word=="text"){ *add control to display textbox*}. I was thinking along those lines. Is that a feasible idea or wouldn't that work?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        A series of "if" statements would work. So would a "switch" construct.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Originally posted by tlhintoq
          A series of "if" statements would work. So would a "switch" construct.
          Incoming digression...

          You know, I love the switch statement... it's just much more fun to code than a giant if-block (plus that whole more optimal at run-time thing... but you know, whatever... it's that more fun part ;)). However, I find it somewhat annoying that you can't use a variable as a condition in the switch statement.

          For example, if I have...

          Code:
          string productName = "";
          switch(code)
          {
            case "001":
              productName = "bananas";
              break;
            case "002"
              productName = "apples";
              break;
            ...
          }
          ... and then when I make my programs more extensible I want to do something like...

          Code:
          string productName = "";
          switch(code)
          {
            case ProductCodes.Bananas:
              productName = ProductNames.Bananas;
              break;
            case ProductCodes.Apples:
              productName = ProductNames.Apples;
              break;
            ...
          }
          ... Where you can now configure the names and codes of the products. Of course, you can't do this because switch statements don't work with dynamic value comparison. I know, I know, this isn't the best example but it's all I could come up with on the fly.

          I realize the optimization of the switch statement comes in comparing against constants, but it would be nice to have the structure of a switch statement but allow comparison against dynamic values. The switch statement just has a better readability and also the fall-through for stacking cases, though I guess C# doesn't allow true fall-through anyway, which sucks a little (in my humble opinion).

          Anyway, back to you in the news room...

          Comment

          • HyperManTT
            New Member
            • Nov 2009
            • 6

            #6
            Originally posted by tlhintoq
            A series of "if" statements would work. So would a "switch" construct.
            Ok guys I know how to programatically add controls to a webform. The problem I am having is that I learnt to do this in Visual Web Developer where a blank webpage was already available. I am creating the console application in Visual C# and was wondering how I would get a blank webform to display the controls?

            Comment

            • HyperManTT
              New Member
              • Nov 2009
              • 6

              #7
              "Can you send your code to different functions based on the dismantled text file?"
              Can you also explain what you mean by 'different functions based on the dismantled text file"? I didn't really catch that and I think that is the only problem, along with the one above, that I am having.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                However, I find it somewhat annoying that you can't use a variable as a condition in the switch statement.
                Sure you can. I do it all the time.
                As a matter of fact, the code you provided saying can't be done, works fine on my computer.

                Code:
                        enum ProductCodes
                        {
                            Bananas = 1,
                            Apples = 2,
                        }
                
                private void SomeTest()
                {
                            ProductCodes code = ProductCodes.Apples;
                            switch(code)
                            {
                                case ProductCodes.Bananas:
                                    productName = "Bananas";
                                break;
                                case ProductCodes.Apples:
                                productName = "Apples";
                                break;
                            }
                            Console.WriteLine(productName);
                }
                Produces the text "Apples" on my console window.

                Comment

                • HyperManTT
                  New Member
                  • Nov 2009
                  • 6

                  #9
                  Yes my switch statements work but I kind of need some help in linking the data to a webpage.
                  Code:
                              string file_path = @"C:\Users\HyperMan\Desktop\demo.txt";
                              StreamReader streamReader = new StreamReader(file_path);
                              string text = streamReader.ReadToEnd();
                              //Console.WriteLine(text);
                              streamReader.Close();
                  
                              // separate individual items between commas
                              string[] textFile = text.Split(new char[] { ',' });
                  
                              //Console.WriteLine("Each individual item: ");
                  
                              foreach (string word in textFile)
                              {
                  
                                  switch (word)
                                  {
                                      case "text":
                                          //WHAT TO PUT HERE TO OUTPUT A TEXTBOX IN A WEBPAGE??
                                          break;
                  
                                      case "DDB":
                                          //WHAT TO PUT HERE TO OUTPUT A DROPDOWNBOX IN A WEBPAGE??
                                          break;
                  
                                      case "label":
                                          //WHAT TO PUT HERE TO OUTPUT A LABEL IN A WEBPAGE??
                                          break;
                  
                                      default:
                                          Console.WriteLine(word);
                                          break;
                                  }
                  
                  
                              }
                              // Console.WriteLine("");
                              Console.Read();
                  
                          }

                  Comment

                  • GaryTexmo
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1501

                    #10
                    Originally posted by tlhintoq
                    Sure you can. I do it all the time.
                    As a matter of fact, the code you provided saying can't be done, works fine on my computer.

                    ...
                    Well, that's not quite what I meant... your codes are still constants there. I realize the example wasn't exactly clear, but I wasn't meaning ProductCodes to be an enum. What I actually meant was that it was something more like this...

                    Code:
                    public class ProductCodes
                    {
                      public string Bananas { get; set; }
                      public string Apples { get; set; }
                      ...
                    
                      public bool ReadProductCodesFromFile(string fileName) { ... }
                    }

                    Comment

                    • GaryTexmo
                      Recognized Expert Top Contributor
                      • Jul 2009
                      • 1501

                      #11
                      Originally posted by HyperManTT
                      Yes my switch statements work but I kind of need some help in linking the data to a webpage.

                      ...
                      thlintoq might have a better answer for you since I know nothing about the web (damn you web!! :P) but if I were to do it with winforms I'd instantiate a control.

                      Code:
                      Control newControl = null;
                      switch (code)
                      {
                        case "Textbox":
                          control = new TextBox();
                          // set the appropriate properties
                          break;
                        case "Button":
                          control = new Button();
                          // set the appropriate properties
                          break;
                        ...
                      }
                      
                      if (control != null)
                      {
                        this.Controls.Add(control);
                      }
                      I'm not sure if you actually instantiate a control on a webpage, or if you'd write out the code that would create it on the webpage. Hopefully that gives you some ideas.

                      Comment

                      • HyperManTT
                        New Member
                        • Nov 2009
                        • 6

                        #12
                        Hmmm seems like a good idea but I am not using winforms =( . Any ideas on what statements I can put in the switch cases to display the various controls for the web form. My only problem basically is how am I getting/linking this console application to a webform? =O

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          I've never used Visual Studio for a website/page. I use it for Windows forms applications. But it was my impression that it would be very similar.

                          Comment

                          Working...