Results 1 to 8 of 8

Thread: Databinding Repeater controls

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Databinding Repeater controls

    Its been a while but i am well and truly stuck and in need of some assistance...

    I have a SQL table containing a list of questions which admin users can add new questions too as and when they feel necessary.

    I have a web form with a Repeater which displays these questions next to a checkbox and a textbox. When the user clicks the checkbox or the enteres data textbox I want to click and update button and store this data in a different table.

    My current thinking is that this would go into a table with 30 bit fields for the checkbox values (c1-c30) and 30 text fields for the textboxes (t1-t30).

    This would allow the admins upto 30 questions in the database at anyone time.

    The question is how do i bind the controls in each row of the Repeater to its corrosponding SQL database field i.e row1 t1 and c1, row 2 t2 and c2 etc.

    Code:
        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            Dim i = 0
            Dim item As RepeaterItem
            Dim checkBox As CheckBox = e.Item.FindControl("CheckBox5")
            Dim textbox As TextBox = e.Item.FindControl("TextBox2")
            Dim sqlitem As String = "C" & i
            SqlDataSource18.UpdateParameters.Item(sqlitem).DefaultValue = checkBox.Checked
            For Each item In Repeater1.Items
                i = i + 1
                SqlDataSource18.UpdateParameters.Item(sqlitem).DefaultValue = checkBox.Checked
            Next item
        End Sub
    I hope that makes sense and you can at least see what I am trying to achieve, any help greatly appreciated.

    Many Thanks

    Steve

  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Databinding Repeater controls

    Hello,

    Sorry, you lost me.

    Would it be possible for you to show a mockup of the interface that you are talking about, and also some sample data?

    It sounds like you just need a foreign key relationship between the questions table, and your other table, but without more information, it is difficult to say.

    Gary

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Resolved Re: Databinding Repeater controls

    I'll try my best...

    Table containing multiple questions (could be 1 could be 30) as below:

    ID Question
    1 Is this even a question?

    ASP.Net form listing each of the above 1-30 questions with a check box and a textbox alongside.

    Button to save the form would insert a new row of data into a new table with a field to capture 30 checkbox (true/false) and the contents of the 30 textboxes.

    Table would look something like:

    Id C1 T1 C2 T2
    1 True Textbox1 contents False NULL

    The first half of that works its binding the controls to the second datatable I am struggling with.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: Databinding Repeater controls

    Ok im going to narrow this down to the bit that us truly causing the issue in the hope i explain myself better.

    Here is the code i currently have in the repeater:

    Code:
        Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
            Dim i = 0
            Dim item As RepeaterItem
            Dim checkBox5 As CheckBox = e.Item.FindControl("CheckBox5")
            Dim textbox2 As TextBox = e.Item.FindControl("TextBox2")
            Dim sqlitem1 As String = "<%# Bind('C" & i & "') %>"
            Dim sqlitem2 As String = "<%# Bind('N" & i & "') %>"
            checkBox5.Attributes.Add("Text", sqlitem1)
            textbox2.Attributes.Add("Text", sqlitem2)
            For Each item In Repeater1.Items
                i = i + 1
                checkBox5.Attributes.Add("Text", sqlitem1)
                textbox2.Attributes.Add("Text", sqlitem2)
            Next item
        End Sub
    What I would expect this to look like when i view source is:

    Code:
    <input id="ctl00_ContentPlaceHolder1_Repeater1_ctl00_CheckBox5" type="checkbox" name="ctl00$ContentPlaceHolder1$Repeater1$ctl00$CheckBox5" checked="checked" Text="<%# Bind('C0') %>" />
    But what it actually looks like is:

    Code:
    <td><span Text="&lt;%# Bind('C0')%>"><input id="ctl00_ContentPlaceHolder1_Repeater1_ctl00_CheckBox5" type="checkbox" name="ctl00$ContentPlaceHolder1$Repeater1$ctl00$CheckBox5" checked="checked" /></span></td>

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Databinding Repeater controls

    Hello,

    I think that you have an issue with your For loop.

    The work that you are doing here:

    Code:
    Dim sqlitem1 As String = "<&#37;# Bind('C" & i & "') %>"
            Dim sqlitem2 As String = "<%# Bind('N" & i & "') %>"
    Is outside of the for loop, there at this point, i is always 0, and as a result, sqlitem1 and sqlitem2 are always going to have the first element in them.

    Gary

  6. #6
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Databinding Repeater controls

    Also, rather than do this:

    Code:
    "<&#37;# Bind('C" & i & "') %>"
    Which will result in your HTML being encoded when passed to the client, since you are already in the server side code, and already have access to the Text property of the TextBox and the Text property of the CheckBox, why not simply set the value? You already have all the information that you need. There is not need to include the Bind statement.

    Gary

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: Databinding Repeater controls

    Thanks Gep13 i spotted that mistake and set i=1 when i declared i.

    My textbox control looks correct when i preview and view source:
    HTML Code:
    <input id="ctl00_ContentPlaceHolder1_Repeater1_ctl00_Textbox2" type="Textbox" name="ctl00$ContentPlaceHolder1$Repeater1$ctl00$TextBox2" Text="<&#37;# Bind('N1') %>" />
    but it still doesnt bind to the database field N1 when i submit the form.

  8. #8
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Databinding Repeater controls

    The HTML input element does not have a Text property:

    http://www.w3schools.com/tags/tag_input.asp

    However a TextBox, which is a server side control does. This is where your problem is. I am sorry to say that the approach that you are using is not correct. You need to set the Text property of the TextBox on the server side, not attempt to add it as an attribute to the HTML element that is rendered to the page.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width