Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 6th, 2006, 03:59 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default Checkbox Value in Repeater

Hi,

I have a dynamic form that's built with a Repeater. I have checkboxes and textboxes. I can get the values from the textboxes but I'm having trouble assigning a value to the checkbox or getting a value from the checkbox.

Code:
<asp:Repeater ID="rptGases" Runat="server" >
<ItemTemplate>                    
<asp:Checkbox Text="" ID="chkGas" Value="" Runat="server" /> <%#DataBinder.Eval(Container.DataItem, "GasID")%>
<asp:TextBox ID="txtResponseTime1" Runat="server" />
<asp:TextBox ID="txtResponseTime2" Runat="server" />
<asp:TextBox ID="txtResponseTime3" Runat="server" />
</ItemTemplate>
</asp:Repeater>
Currently I have a work around like this:

Code:
For Each i As RepeaterItem In rptGases.Items
Dim GasID As Integer
Dim SQL As String = "SELECT GasID FROM Gases INNER JOIN GasTypes ON Gases.GasTypeID = GasTypes.GasTypeID WHERE (Gases.OfficeID = " & ddlOfficeID.SelectedValue & ") AND (Gases.Status = 1) ORDER BY GasTypes.OrderBy;"
Dim CMD As New SqlCommand(SQL, _oConn)
Dim oRS As SqlDataReader = CMD.ExecuteReader
Dim n As Integer = 0
Do While oRS.Read
If n = i.ItemIndex Then
GasID = oRS("GasID")
End If
n += 1
Loop
oRS.Close()
Dim chkGas As CheckBox = i.FindControl("chkGas")
Dim txtResponseTime1 As TextBox = i.FindControl("txtResponseTime1")
Dim txtResponseTime2 As TextBox = i.FindControl("txtResponseTime2")
Dim txtResponseTime3 As TextBox = i.FindControl("txtResponseTime3")
If chkGas.Checked Then
[Do something]
End If
Next
It just seems like there must be an easier way to access this data.

Thanks for your help.

Richard

 
Old September 6th, 2006, 05:51 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

For the text portion of the checkbox have you tried:

text='<%#Container.DataItem("GasID")%>'

I use a similar approach in datagrids without fail.


"The one language all programmers understand is profanity."
 
Old September 6th, 2006, 06:56 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi,

Thanks for the reply. When I add that the checkbox doesn't show up at all.

<asp:Checkbox Text="<%#Container.DataItem("GasID")%>" ID="chkGas" Value="" Runat="server" />

I can work fine with CheckBoxLists in getting the DataValueField but just have never done anything like this before. I can get the value with the code I posted its just not very elegant. And I know, "Hey it works", don't worry about.it. Just thought there was another way to do this.

Much appreciated.

Thanks,
Richard

 
Old September 6th, 2006, 07:36 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hey Rich,
   I am curious about a few things: (Please don't get offeneded if I ask seeming 'beginner' questions)

First, you can omit Value="" from the checkbox control it is not supported.

On your backend you are doing something like this, yes?
rptGases.datasource = datatable (or whatever your datasource is)
rptGases.databind()

Is you databind call inside of an Not IsPostBack?? (If it is, take it out)

When you bind the checkbox are you doing:

checkbox text="<%#Container.DataItem("GasID")%>"
or
checkbox text='<%#Container.DataItem("GasID")%>'
(If you are using double quotes, use single quotes instead)

What is very strange is that I threw an app together real quick (I very rarely use repeaters so I wanted to make sure this could actually be done) and I was able to bind 'lastname' column of the Employees table from the northwind database to my checkbox without a problem and everything displayed fine.

ASPX page:
        <form id="Form1" method="post" runat="server">
            <asp:Repeater ID="rptGases" Runat="server">
                <ItemTemplate>
                    <asp:Checkbox Text='<%#DataBinder.Eval(Container.DataItem, "lastName")%>' ID="chkGas" Runat="server" />
                    <asp:TextBox ID="txtResponseTime1" Runat="server" />
                    <asp:TextBox ID="txtResponseTime2" Runat="server" />
                    <asp:TextBox ID="txtResponseTime3" Runat="server" />
                </ItemTemplate>
            </asp:Repeater>
        </form>

VB Page:
        Dim da As New DataAccess
        Dim dt As New DataTable
        Dim sql As String

        sql = "Select * From employees"
        dt = da.getDataTable(sql)

        rptGases.DataSource = dt
        rptGases.DataBind()

(Dont worry about the object DA i built this page real quick inside of an existing application, da is just the class I use to retrive data from my Database Servers)

This displayed the lastname as the text of each checkbox???

Let me know if I am misunderstanding your post.

"The one language all programmers understand is profanity."
 
Old September 6th, 2006, 11:50 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi Dave,

No. I'm not offended at all. I did find out already that 'Value' doesn't work for checkboxes. Just didn't pull it yet. But thanks. You can tell me anything.. almost.

OK. The repeater is not binding in the If not IsPostBack.

I took the double quotes out from the the Text='' and that solved one of the problems I was having. I was having to put the name for the checkbox outside of the control. So that's fixed.
Yeah, that's very cool. I never would have thought that it made any difference at all if it was single of double quotes. I had always been in the habit of using double quotes everywhere.

So the checkboxes for the gases have the name but the biggest problem I'm having is that the DataValueField for checkbox and retrieving that value. I need the GasID for the database and not the name when I do the save. So, I don't want to have the name of the checkbox 27 or 34, etc., the database ID. So, how do I get the value for that checkbox in the code behind when I save it? I'm not having any problem getting the values for the textboxes and actually I can get the value for the checkboxes but if you look at the code its really a round about way to do it.

I think you are understanding me pretty well so far. If I put the GasID into the control I can easily find that value now that you got the double quotes out of there. chkGas.Text will do the trick if I use GasID. The only problem is, is that I don't want the database ID for the Gas to show as the Text for the checkbox. Very close I think. I need to be able to do is something like chkGas.Value and be able to assign a value to it just like you would if you were creating a checkboxlist with a Text value and a Data value.

Anyway, thanks for your input. Much appreciated.

Rich

 
Old September 7th, 2006, 07:30 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Rich,
   What I think you may need to do is use a Checkbox list as opposed to a checkbox. You can then have a DataValue and DataText field and you could then iterate through the list by

Dim chkGas As CheckBoxList = i.FindControl("chkGas")
Dim li as ListItem

for each li in chkGas.Items
  if li.checked [do something] (you can then access li.value, li.text)
next

There should be no difference here for you since your list will only have one item anyway it will only appear as one checkbox (i think anyway).

hth

"The one language all programmers understand is profanity."
 
Old September 7th, 2006, 11:30 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi,

She no worky.

'checked' is not a member of 'System.Web.UI.WebControls.ListItem'

I tried monkeying around with different combinations of the last code you sent and couldn't get anything to work.

Well, at least I have something that works. It just seems like there must be a way to get the values for the check boxes besides what I'm doing. But hey, that's alright. I'd really be cussing if I didn't have any way. If you got any more suggestions, I'm listening.

Thanks,
Richard




 
Old September 7th, 2006, 11:47 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Try li.selected as opposed to li.checked

"The one language all programmers understand is profanity."
 
Old September 7th, 2006, 01:08 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

I tried this and there are no errors but it doesn't write anything to the page when one of the checkboxes is checked. I may be implementing your suggestion wrong. Here's the code.

Code:
            For Each i As RepeaterItem In rptGases.Items
                Dim chkGas As CheckBoxList = i.FindControl("chkGas")
                Dim li As ListItem
                For Each li In chkGas.Items
                    If li.Selected Then
                        Response.Write(li.Value & "<br />")
                    End If
                Next
            Next







Similar Threads
Thread Thread Starter Forum Replies Last Post
checkbox checked by default by html:checkbox sachin.tathod Struts 3 December 4th, 2006 03:41 PM
why do Repeater is used... kk_katepally General .NET 1 December 22nd, 2004 06:36 PM
Repeater chiraagb ADO.NET 1 May 31st, 2004 02:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.