Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 October 9th, 2003, 04:35 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default checkbox in aspx




hi

i am doing my web page in asp.net
i have problem with check box.here i am explaining detailly

i have 2 arrays both contains some names.second array is subset of fisrt one.
explamle first array contains 100 names and second one contains 20 names.
i want to show the names of first array with check box option.if names of first array exist in second array
i want to show check box with checked option.other wise checkbox will be unchecked.i did it in asp (like below example)
and working fine.


flag=false
for i=0 to firstarr.count-1
    for j=0 to secondarr.count-1
            if trim(firstarr.item(i))=trim( secondarr.item(j)) then
                flag=true
                exit for
            end if
    next
        if flag=true then
            <td><input type=CheckBox ID="firstarr<%=i%>" checked value="1"><%=firstarr.item(i)%></td>

        else
            <td><input type=CheckBox ID="firstarr<%=i%>" value="0"><%=firstarr.item(i)%></td>
        end if
    flag=false
next





but aspx its giving problem.because in asp:checkbox is not allowing duplicate id and also not allowing dynamically assaiging

id dynamically like

<%if flag=true then%>

<td><asp:CheckBox Runat =server ID="firstarr<%=i%>" checked /><%=firstarr.item(i)%></td>
<%else%>

<td><asp:CheckBox runat=server ID="firstarr<%=i%>" /><%=firstarr.item(i)%></td>

<%end if%>


i need the id because later i want to do updates in the database based on this ids.

i hope i cleared the problem.pl advice me
thanks sr






 
Old October 9th, 2003, 09:31 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You should really try to avoid programming ASP.Net in the old ASP style (script embedded in the HTML). If you want to add checkbox items to the page dynamically, you should do so in the code section of your page (either the runat server script tag or in the code-behind depending on what method you are using.

Using either method, here's my recommendation (put this in your code section's page_load function):

For i = 0 To firstarr.Count - 1
    For j = 0 To secondarr.Count - 1
        If Trim(firstarr.Item(i)) = Trim(secondarr.Item(j)) Then
            flag = True
            Exit For
        End If
    Next
    objCheckBox = New CheckBox
    objCheckBox.Checked = flag
    objCheckBox.ID = "firstarr" & i
    Me.Controls.Add(objCheckBox)
    flag = False
Next

You might want to put an asp:Placeholder in your page where you want to put the checkboxes, then use that instead of "Me" in the code above.


Peter
 
Old October 9th, 2003, 09:55 PM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
Tnahks for response.I just followed your code .
i can display the first array with checkboxes.but while updating i am not getting the checked boxes ids means

after display the check boxes by click on some button user may update checkboxes(checked or unchecked) so i need to update the those checked boxes in my database.so how to get dispalyed checkbox ids. iam using the placholder in my code.

when i see view source of my web i can see the checkbox ids like firstarr1,firstarr2.......firstarr40 .when i try to refer as firstarr1.checked its giving error.i think its dynamic object i cant refer it in my code behind.so how to get those ids in my code behind.pl help me.

thanks sr


 
Old October 10th, 2003, 03:49 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
here i am giving detailly my prom.
When user click on "retrive" button I am creating the checkboxes and adding them to placeholder .And these are displaying perfectly in my web page.
now user may update the checkboxes ocording to their wish.

when try to get whether checkbox is checked or not in another button(say update) click its not working. i mean below code

For Each item In plh.Controls

If (TypeOf (item) Is System.Web.UI.WebControls.CheckBox) Then

Dim checkbox1 As System.Web.UI.WebControls.CheckBox = CType(item, System.Web.UI.WebControls.CheckBox)

If checkbox1.Checked Then
Response.Write("This has been checked!")
Else
'Response.Write("This is not checked! ")
End If

End If


Next


if i copy this code in "retrive" button click its working but i need to get the checkbox value or checked or not in "update" button click.placeholder control does not contain anything if controle reaches the update button clicked event.

pl advice me thanks


 
Old October 10th, 2003, 10:46 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Where are you building the checkboxes? Are you building them in a button event or page load?

I just did a test and it seems the viewstate doesn't restore checkboxes if you added them into a placeholder in a button event. I don't know why this is, it doesn't make sense to me. It should be doing that. Perhaps it has to do with the placeholder object, but I can't imagine why that would be.

If you can have the checkboxes built in the page_load instead of an event, you should be able to get this to work. You just need to make sure they are built the same each time. Then all the checked states will be maintained and you can go thru the controls and find which are checked.

Peter





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
New.aspx works but login.aspx fails Validation nigelhamilton BOOK: ASP.NET Website Programming Problem-Design-Solution 8 September 13th, 2004 02:29 AM
New.aspx works but not Login.aspx ? reidcor BOOK: ASP.NET Website Programming Problem-Design-Solution 2 May 24th, 2004 10:32 AM
HowTo Pass Form data from Page1.aspx to Page2.aspx dati ASP.NET 1.0 and 1.1 Basics 6 January 27th, 2004 06:57 AM





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