dynamic server control

Discussion in 'ASP .Net' started by angus, Nov 1, 2004.

  1. angus

    angus Guest

    Hi All,

    i have an aspx form for the user to input, and those data will be stored
    into the database.

    I would like to build up the form dynamically by using the data in the a
    database table, named question

    for example, i have a question table and the data in the question table is
    as follow

    select * from question

    ---------------------------------
    | question_data |
    ---------------------------------
    | 1 + 1 = ? |
    | 2 - 1 = ? |
    | 2 + 2 = ? |
    ---------------------------------


    Then, in my aspx page, the form will be as follow, (i assume that the form
    is built dynamically)

    (/* aspx Label here */) (/* aspx TextBox here */)

    1 + 1 = ? |_____________|
    2 - 1 = ? |_____________|
    2 + 2 = ? |_____________|

    And all the answer in the TextBox, would be saved into the database.

    If i would like to add question for the form, just insert the question into
    the question table

    e.g.

    I would like to add a question "how old are you?"

    i just "insert into question (question_data) values ('how old are you?') "

    Then, the aspx page will be

    (/* aspx Label here */) (/* aspx TextBox here */)

    1 + 1 = ? |_____________|
    2 - 1 = ? |_____________|
    2 + 2 = ? |_____________|
    how old are you? |_____________|


    So, my question is, how to build up the form dynamically like the above
    example?
    And what server control should i use?
    And how can i pass the TextBox data to the backend codehind

    Thank you.

    Regards,
    Angus
     
    angus, Nov 1, 2004
    #1
    1. Advertising

  2. angus

    Karl Seguin Guest

    Keeping the example simple, like you have, it's really quite easy (code
    isn't compiled, so there could be errors, just going off the top of my head
    to give you ideas):

    HTML

    <asp:repeater id="questions" runat="server">
    <HeaderTemplate>
    <table border="0">
    <HeaderTemplate>
    <ItemTemplate>
    <Tr>
    <td><%# DataBinder.Eval(Container.DataItem, "Question_Data")
    %><input type="hidden" id="questionId" runat="server" value='<#
    DataBinder.Eval(Container.DataItem, "Question_ID")%>' /></td>
    <td><asp:textbox id="answer" runat="server" ><asp:</td>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:repeater>
    <asp:button id="Save" runat="server" Text="Save" />


    CodeBehind

    protected questions as Repeater
    protected withevents save as Button

    Sub Page_Load
    questions.DataSource = GetAllQuestions()
    questions.DataBind()
    end sub

    protected sub Save_OnClick(s as object, e as ComandEventArgs) Handles
    Save.Click
    for each item as RepeaterItem in Repeater.Items
    dim answer as TextBox = ctype(item.FindControl("answer"), textbox)
    dim questionId as HtmlInputHidden =
    ctype(item.FindControl("questionId"), HtmlinputHidden)
    if not answer is nothing andalso not questionId is nothing
    SaveAnswerToDataBase(questionId.Value, answer.Text)
    'fictisious function to save values
    end if
    next
    end sub

    publicshared function GetQuestions() as DataTable
    dim cacheKey as string = "allQuestions"
    dim dt as DataTable = ctype(HttpRuntime.Cache(cacheKey), DataTable)
    if dt is nothing then
    dim connection as new SqlConnection(CONNECTION STRING)
    dim command as new SqlCommand("SELECT * FROM Question", connection)
    dim da as new SqlDataAdapter(command)

    try
    connection.open()
    da.fill(dt)
    finally
    connection.dispose()
    command.dispose()
    da.dispose()
    end try

    HttpRuntime.Cache.Insert(cacheKey, dt, null, DateTime.Now.AddHours(1),
    TimeSpan.Zero)
    end if
    return dt
    end function


    Hope this sends you in the right direction.
    Karl
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "angus" <> wrote in message
    news:...
    > Hi All,
    >
    > i have an aspx form for the user to input, and those data will be stored
    > into the database.
    >
    > I would like to build up the form dynamically by using the data in the a
    > database table, named question
    >
    > for example, i have a question table and the data in the question table is
    > as follow
    >
    > select * from question
    >
    > ---------------------------------
    > | question_data |
    > ---------------------------------
    > | 1 + 1 = ? |
    > | 2 - 1 = ? |
    > | 2 + 2 = ? |
    > ---------------------------------
    >
    >
    > Then, in my aspx page, the form will be as follow, (i assume that the form
    > is built dynamically)
    >
    > (/* aspx Label here */) (/* aspx TextBox here */)
    >
    > 1 + 1 = ? |_____________|
    > 2 - 1 = ? |_____________|
    > 2 + 2 = ? |_____________|
    >
    > And all the answer in the TextBox, would be saved into the database.
    >
    > If i would like to add question for the form, just insert the question

    into
    > the question table
    >
    > e.g.
    >
    > I would like to add a question "how old are you?"
    >
    > i just "insert into question (question_data) values ('how old are you?') "
    >
    > Then, the aspx page will be
    >
    > (/* aspx Label here */) (/* aspx TextBox here */)
    >
    > 1 + 1 = ? |_____________|
    > 2 - 1 = ? |_____________|
    > 2 + 2 = ? |_____________|
    > how old are you? |_____________|
    >
    >
    > So, my question is, how to build up the form dynamically like the above
    > example?
    > And what server control should i use?
    > And how can i pass the TextBox data to the backend codehind
    >
    > Thank you.
    >
    > Regards,
    > Angus
    >
    >
     
    Karl Seguin, Nov 1, 2004
    #2
    1. Advertising

  3. angus

    angus Guest

    Hi Karl,

    I get what you mean, and the problem solved.
    Thank you so much

    Regards,
    Angus

    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > Keeping the example simple, like you have, it's really quite easy (code
    > isn't compiled, so there could be errors, just going off the top of my
    > head
    > to give you ideas):
    >
    > HTML
    >
    > <asp:repeater id="questions" runat="server">
    > <HeaderTemplate>
    > <table border="0">
    > <HeaderTemplate>
    > <ItemTemplate>
    > <Tr>
    > <td><%# DataBinder.Eval(Container.DataItem, "Question_Data")
    > %><input type="hidden" id="questionId" runat="server" value='<#
    > DataBinder.Eval(Container.DataItem, "Question_ID")%>' /></td>
    > <td><asp:textbox id="answer" runat="server" ><asp:</td>
    > </ItemTemplate>
    > <FooterTemplate>
    > </table>
    > </FooterTemplate>
    > </asp:repeater>
    > <asp:button id="Save" runat="server" Text="Save" />
    >
    >
    > CodeBehind
    >
    > protected questions as Repeater
    > protected withevents save as Button
    >
    > Sub Page_Load
    > questions.DataSource = GetAllQuestions()
    > questions.DataBind()
    > end sub
    >
    > protected sub Save_OnClick(s as object, e as ComandEventArgs) Handles
    > Save.Click
    > for each item as RepeaterItem in Repeater.Items
    > dim answer as TextBox = ctype(item.FindControl("answer"), textbox)
    > dim questionId as HtmlInputHidden =
    > ctype(item.FindControl("questionId"), HtmlinputHidden)
    > if not answer is nothing andalso not questionId is nothing
    > SaveAnswerToDataBase(questionId.Value, answer.Text)
    > 'fictisious function to save values
    > end if
    > next
    > end sub
    >
    > publicshared function GetQuestions() as DataTable
    > dim cacheKey as string = "allQuestions"
    > dim dt as DataTable = ctype(HttpRuntime.Cache(cacheKey), DataTable)
    > if dt is nothing then
    > dim connection as new SqlConnection(CONNECTION STRING)
    > dim command as new SqlCommand("SELECT * FROM Question", connection)
    > dim da as new SqlDataAdapter(command)
    >
    > try
    > connection.open()
    > da.fill(dt)
    > finally
    > connection.dispose()
    > command.dispose()
    > da.dispose()
    > end try
    >
    > HttpRuntime.Cache.Insert(cacheKey, dt, null, DateTime.Now.AddHours(1),
    > TimeSpan.Zero)
    > end if
    > return dt
    > end function
    >
    >
    > Hope this sends you in the right direction.
    > Karl
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "angus" <> wrote in message
    > news:...
    >> Hi All,
    >>
    >> i have an aspx form for the user to input, and those data will be stored
    >> into the database.
    >>
    >> I would like to build up the form dynamically by using the data in the a
    >> database table, named question
    >>
    >> for example, i have a question table and the data in the question table
    >> is
    >> as follow
    >>
    >> select * from question
    >>
    >> ---------------------------------
    >> | question_data |
    >> ---------------------------------
    >> | 1 + 1 = ? |
    >> | 2 - 1 = ? |
    >> | 2 + 2 = ? |
    >> ---------------------------------
    >>
    >>
    >> Then, in my aspx page, the form will be as follow, (i assume that the
    >> form
    >> is built dynamically)
    >>
    >> (/* aspx Label here */) (/* aspx TextBox here */)
    >>
    >> 1 + 1 = ? |_____________|
    >> 2 - 1 = ? |_____________|
    >> 2 + 2 = ? |_____________|
    >>
    >> And all the answer in the TextBox, would be saved into the database.
    >>
    >> If i would like to add question for the form, just insert the question

    > into
    >> the question table
    >>
    >> e.g.
    >>
    >> I would like to add a question "how old are you?"
    >>
    >> i just "insert into question (question_data) values ('how old are you?')
    >> "
    >>
    >> Then, the aspx page will be
    >>
    >> (/* aspx Label here */) (/* aspx TextBox here */)
    >>
    >> 1 + 1 = ? |_____________|
    >> 2 - 1 = ? |_____________|
    >> 2 + 2 = ? |_____________|
    >> how old are you? |_____________|
    >>
    >>
    >> So, my question is, how to build up the form dynamically like the above
    >> example?
    >> And what server control should i use?
    >> And how can i pass the TextBox data to the backend codehind
    >>
    >> Thank you.
    >>
    >> Regards,
    >> Angus
    >>
    >>

    >
    >
     
    angus, Nov 2, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,950
    Scott M.
    Oct 11, 2003
  2. Chris Thunell
    Replies:
    3
    Views:
    3,431
    Edward
    Jul 21, 2004
  3. Replies:
    5
    Views:
    5,590
  4. Replies:
    0
    Views:
    2,068
  5. Larry Bud
    Replies:
    1
    Views:
    351
    bruce barker
    Jan 10, 2007
Loading...

Share This Page