Dynamic Variables-ASP.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BezerkRogue
    New Member
    • Oct 2007
    • 68

    Dynamic Variables-ASP.NET

    I have created a form using a datalist that is dynamically populated with report items based on search criteria. The form loads correctly but I can't seem to find a way to call the variables for the data so I can enter it into the database.

    I have looked at the dynamically generated variable names but can't figure out how to call them in my aspx.vb file. Any help would be appreciated.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Suppose you dynamically generate a control in your page:
    Code:
    Textbox t = new TextBox();
    t.value="This will be shown in the textbox";
    Page.Controls.Add(t);
    If you say this:
    Code:
    Textbox t = new TextBox();
    t.ID="tbSomeUniqueName";
    t.value="This will be shown in the textbox";
    Page.Controls.Add(t);
    You can then use the .FindControl() function:
    Code:
    Control c=Page.FindControl("tbSomeUniqueName");
    if (c!=null)
    {//it found a control with the given name
       if (c.GetType()==typeof(TextBox))
       {//the found control was of type textbox
       }
    }

    Comment

    • BezerkRogue
      New Member
      • Oct 2007
      • 68

      #3
      I'm not sure if that would work. The report inputs load into one form. The number of inputs and text boxes is different. Here is the code that I am using to load the page.

      [code=asp]
      <asp:DataList ID="RN" runat="server" DataSourceID="S qlDataSource2">
      <ItemTemplate >
      <asp:Label ID="Label1" runat="server" Text='<%# Eval("Header") %>' Width="454px" Font-Size="24px" Font-Bold="true"></asp:Label>
      </ItemTemplate>
      </asp:DataList><a sp:SqlDataSourc e ID="SqlDataSour ce2" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:someconnecti on string %>"
      SelectCommand=" SELECT * FROM [ReportType] WHERE (([InputType] = @InputType) AND ([Frequency] = @Frequency))">
      <SelectParamete rs>
      <asp:QueryStrin gParameter Name="InputType " QueryStringFiel d="strDep" Type="String" />
      <asp:QueryStrin gParameter Name="Frequency " QueryStringFiel d="strFreq" Type="String" />
      </SelectParameter s>
      </asp:SqlDataSour ce>
      <br />
      <br />
      <!-- #include file = "~/includes/CalStyle.inc -->
      Date:&nbsp;<asp :Calendar ID="occDate" runat="server"> </asp:Calendar>
      <asp:DataList ID="IL" runat="server" DataSourceID="S qlDataSource1">
      <ItemTemplate >
      <asp:Label ID="L" runat="server" Text='<%# Eval("DESCRIPTI ON") %>' Width="320px" ></asp:Label>
      <asp:TextBox ID="I" runat="server"> </asp:TextBox>&nb sp;Notes:&nbsp; <asp:TextBox
      ID="N" runat="server"> </asp:TextBox>
      </ItemTemplate>
      </asp:DataList>
      <asp:Button ID="cmdSubmit" runat="server" Text="Submit" />
      <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:some connectionstrin g%>"
      SelectCommand=" SELECT * FROM [BACKBONE] WHERE (([DEPARTMENT] = @DEPARTMENT) AND ([FREQUENCY] = @FREQUENCY))">
      <SelectParamete rs>
      <asp:QueryStrin gParameter Name="DEPARTMEN T" QueryStringFiel d="strDep" Type="String" />
      <asp:QueryStrin gParameter Name="FREQUENCY " QueryStringFiel d="strFreq" Type="String" />
      </SelectParameter s>
      </asp:SqlDataSour ce>
      [/code]
      It gives each label and textbox a unique ID in this manner: DL_ctl00_I where the 00 iterates for each new row.
      Last edited by Plater; Feb 7 '08, 02:28 PM. Reason: added [CODE] tags

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Have you tried to use .FindControl with one of those unique names to see if it can find it?

        Comment

        • BezerkRogue
          New Member
          • Oct 2007
          • 68

          #5
          This is the code that I am using. I'm not sure if I missed something.
          [code=vbnet]
          Dim c As Control = Page.FindContro l("IL$ctl00$N ")
          If c IsNot Nothing Then
          'it found a control with the given name
          'the found control was of type textbox
          If c.[GetType]() Is GetType(TextBox ) Then
          Response.Redire ct("http://www.yahoo.com")
          End If
          End If
          [/code]
          The response.redire ct was just to see if it found the control. How can I get it to loop through since the only thing that changes is the number after the ctl portion of the control name?
          Last edited by Frinavale; Feb 12 '08, 02:10 PM. Reason: Added [Code] Tags

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            What are those $ signs in the control name? Are those even legal characters?

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by BezerkRogue
              I have created a form using a datalist that is dynamically populated with report items based on search criteria. The form loads correctly but I can't seem to find a way to call the variables for the data so I can enter it into the database.

              I have looked at the dynamically generated variable names but can't figure out how to call them in my aspx.vb file. Any help would be appreciated.
              Could you please post the code that you have for creating the dynamic variable names.

              Originally posted by plater
              What are those $ signs in the control name? Are those even legal characters?
              When the variables are rendered in the browser the name of the HTML element contains $'s in them. I have a feeling that the OP here has looked at the HTML name and tried to use that to retrieve their dynamically created control.

              $ are not legitimate variable name characters.
              The are filtered out by ASP.NET when the page is being processed before it reaches the code.

              That's why we need to see how you are dynamically creating your variable names...this way we can help you in determining the best way to retrieve the controls.

              -Frinny

              Comment

              • BezerkRogue
                New Member
                • Oct 2007
                • 68

                #8
                The actual variable names contain underscores. I use a the following code to generate the datalist.
                [code=asp]
                <asp:datalist runat="server" id="DataList1"> </asp:datalist>
                <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:someconnecti onstring%>"
                SelectCommand=" SELECT * FROM [BACKBONE] WHERE (([DEPARTMENT] = @DEPARTMENT) AND ([FREQUENCY] = @FREQUENCY))">
                <SelectParamete rs>
                <asp:Paramete r DefaultValue="H R" Name="DEPARTMEN T" Type="String" />
                <asp:Paramete r DefaultValue="D aily" Name="FREQUENCY " Type="String" />
                </SelectParameter s>
                </asp:SqlDataSour ce>[/code]
                Last edited by Frinavale; Feb 12 '08, 02:01 PM. Reason: Added [Code] Tags

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by BezerkRogue
                  The actual variable names contain underscores. I use a the following code to generate the datalist.
                  [code=asp]
                  <asp:datalist runat="server" id="DataList1"> </asp:datalist>
                  <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:someconnecti onstring%>"
                  SelectCommand=" SELECT * FROM [BACKBONE] WHERE (([DEPARTMENT] = @DEPARTMENT) AND ([FREQUENCY] = @FREQUENCY))">
                  <SelectParamete rs>
                  <asp:Paramete r DefaultValue="H R" Name="DEPARTMEN T" Type="String" />
                  <asp:Paramete r DefaultValue="D aily" Name="FREQUENCY " Type="String" />
                  </SelectParameter s>
                  </asp:SqlDataSour ce>[/code]

                  The DataSource for your DataList object is dynamically generated, but your DataList itself is not dynamically generated. Please take a look at the DataList Class for more details on how to use the DataList object. (You can directly refer to your DataList by accessing the variable DataList1)

                  There are a bunch of walkthroughs in the link I listed above...I think you would be interested in the walkthrough on How to: Allow Users to Edit Items in DataList Web Server Controls

                  -Frinny

                  Comment

                  • BezerkRogue
                    New Member
                    • Oct 2007
                    • 68

                    #10
                    I will look at it. This is the revised code. I mistakenly entered the code from a previous attempt. The menu passes the values of for the deparment and frequency and the datalist is built from the resulting recordset with two textboxes added.
                    [code=asp]
                    <asp:DataList ID="IL" runat="server" DataSourceID="S qlDataSource1">
                    <ItemTemplate >
                    <asp:Label ID="L" runat="server" Text='<%# Eval("DESCRIPTI ON") %>' Width="320px" ></asp:Label>
                    <asp:TextBox ID="I" runat="server"> </asp:TextBox>&nb sp;Notes:&nbsp; <asp:TextBox
                    ID="N" runat="server"> </asp:TextBox>
                    </ItemTemplate>
                    </asp:DataList>

                    <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:somestring%> "
                    SelectCommand=" SELECT * FROM [BACKBONE] WHERE (([DEPARTMENT] = @DEPARTMENT) AND ([FREQUENCY] = @FREQUENCY))">
                    <SelectParamete rs>
                    <asp:QueryStrin gParameter Name="DEPARTMEN T" QueryStringFiel d="strDep" Type="String" />
                    <asp:QueryStrin gParameter Name="FREQUENCY " QueryStringFiel d="strFreq" Type="String" />
                    </SelectParameter s>
                    </asp:SqlDataSour ce>[/code]
                    Last edited by Frinavale; Feb 14 '08, 03:18 PM. Reason: Added [Code] Tags

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Originally posted by BezerkRogue
                      I will look at it. This is the revised code. I mistakenly entered the code from a previous attempt. The menu passes the values of for the deparment and frequency and the datalist is built from the resulting recordset with two textboxes added.
                      [code=asp]
                      <asp:DataList ID="IL" runat="server" DataSourceID="S qlDataSource1">
                      <ItemTemplate >
                      <asp:Label ID="L" runat="server" Text='<%# Eval("DESCRIPTI ON") %>' Width="320px" ></asp:Label>
                      <asp:TextBox ID="I" runat="server"> </asp:TextBox>&nb sp;Notes:&nbsp; <asp:TextBox
                      ID="N" runat="server"> </asp:TextBox>
                      </ItemTemplate>
                      </asp:DataList>

                      <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:somestring%> "
                      SelectCommand=" SELECT * FROM [BACKBONE] WHERE (([DEPARTMENT] = @DEPARTMENT) AND ([FREQUENCY] = @FREQUENCY))">
                      <SelectParamete rs>
                      <asp:QueryStrin gParameter Name="DEPARTMEN T" QueryStringFiel d="strDep" Type="String" />
                      <asp:QueryStrin gParameter Name="FREQUENCY " QueryStringFiel d="strFreq" Type="String" />
                      </SelectParameter s>
                      </asp:SqlDataSour ce>[/code]
                      Hi BezerkRogue,
                      As a full member now, you should know that we expect your code to be posted in [CODE] tags (See How to Ask a Question).
                      This makes it easier for our Experts to read and understand your code. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.
                      Please use [CODE] tags in future.

                      Thanks
                      -Frinny

                      Comment

                      • BezerkRogue
                        New Member
                        • Oct 2007
                        • 68

                        #12
                        I apologize for that lapse in decorum. Here is the code with the code tags.

                        [CODE=asp]
                        <asp:DataList ID="RN" runat="server" DataSourceID="S qlDataSource2">
                        <ItemTemplate >
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Header") %>' Width="454px" Font-Size="24px" Font-Bold="true"></asp:Label>
                        </ItemTemplate>
                        </asp:DataList><a sp:SqlDataSourc e ID="SqlDataSour ce2" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:some connectionstrin g %>"
                        SelectCommand=" SELECT * FROM [ReportType] WHERE (([InputType] = @InputType) AND ([Frequency] = @Frequency))">
                        <SelectParamete rs>
                        <asp:QueryStrin gParameter Name="InputType " QueryStringFiel d="strDep" Type="String" />
                        <asp:QueryStrin gParameter Name="Frequency " QueryStringFiel d="strFreq" Type="String" />
                        </SelectParameter s>
                        </asp:SqlDataSour ce>
                        <br />
                        <br />
                        <!-- #include file = "~/includes/CalStyle.inc -->
                        Date:&nbsp;<asp :Calendar ID="occDate" runat="server"> </asp:Calendar>
                        <asp:DataList ID="IL" runat="server" DataSourceID="S qlDataSource1">
                        <ItemTemplate >
                        <asp:Label ID="L" runat="server" Text='<%# Eval("DESCRIPTI ON") %>' Width="320px" ></asp:Label>
                        <asp:TextBox ID="I" runat="server"> </asp:TextBox>&nb sp;Notes:&nbsp; <asp:TextBox
                        ID="N" runat="server"> </asp:TextBox>
                        </ItemTemplate>
                        </asp:DataList>

                        <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:Louisville-IPConnectionStr ing %>"
                        SelectCommand=" SELECT * FROM [BACKBONE] WHERE (([DEPARTMENT] = @DEPARTMENT) AND ([FREQUENCY] = @FREQUENCY))">
                        <SelectParamete rs>
                        <asp:QueryStrin gParameter Name="DEPARTMEN T" QueryStringFiel d="strDep" Type="String" />
                        <asp:QueryStrin gParameter Name="FREQUENCY " QueryStringFiel d="strFreq" Type="String" />
                        </SelectParameter s>
                        </asp:SqlDataSour ce>
                        [/CODE]
                        Last edited by BezerkRogue; Feb 16 '08, 01:45 AM. Reason: Name specific connection string in code.

                        Comment

                        Working...