Solved

problem with oncheckedchanged when dynamically created checkboxes

Posted on 2006-11-16
11
322 Views
Last Modified: 2012-05-05
Hi,i have an editor.aspx where i have a table named sbaTable. That table is dynamically filled with rows in my code page editor.aspx.vb...On every row I have one checkbox and one textbox.. what i want to do is to disable textbox whenever checkbox on same row is checked=true!

editor.aspx.vb i have a loop that counts as many rows as i need(works fine)..
Private Sub PopulateGrid()
...create tablerow
...create tablecell "tcOk"
Dim chkOK as new checkbox
chkOK.id = "ok-" + tdindex.text
chkOK.autopostback = true
''chkOK.oncheckedchanged" doesnot exist here for some reason(OnCheckedChanged exists in designer mode)!? that is why i tried with handlern below...
addhandler chkOK.checkedchanged, adressof chk_OncheckedChanged --

tcOk.controls.add(chkOK)
 
...create tablecell "tcNote"
Dim txtNote as new textbox
txtNote.id = "note-" + tdindex.text
...
tcNote.controls.add(txtNote)
...
...addrows and cells to the table mm..
End Sub

Public sub chk_OncheckedChanged(ByVal sender As Object,ByVal e As EventArgs)

Dim checkBox as checkbox = Ctype(sender, CheckBox)
Dim idString as string = checkbox.id
disable...
enable...
mm
End Sub

Problem = chk_OnCheckedChanged is never hit...can i do this the way i am trying oris there another solution??

Thanks in advance
0
Comment
Question by:adismesevic
  • 5
  • 4
11 Comments
 
LVL 10

Expert Comment

by:bchoor
ID: 17963144
I think the handler is correct.  However, make sure the table is not re-generated on postback. You may try to generate the table in Page_PreRender to ensure that the viewstate preserves the controls that you add.

HTH
~BC
0
 

Author Comment

by:adismesevic
ID: 17963917
Hi Thanks for your answer.. i tried to create
 Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
         'If Not Page.IsPostBack Then
            PopulateGrid()
         'End If
End Sub

it doesnot help table is regenereted anyway and i can't use 'If Not Page.IsPostBack Then either because then i dont get anything back when page postsback....at the same time i must set autopostback = true for my checkbox or nothing will happen when clicked!!
0
 

Author Comment

by:adismesevic
ID: 17971245
anyone know anything about this??..i have searched the google but cant find any solution there either!?
0
 
LVL 10

Expert Comment

by:bchoor
ID: 17975613
hmmm....did u try to enable viewstate?
0
 

Author Comment

by:adismesevic
ID: 17977631
yes, i tried that too now..still not working...is there an easier way to do this with javascript?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:bchoor
ID: 17980237
hmm....I'll try a sample today and keep you posted on how it works - give me until the evening
0
 
LVL 10

Expert Comment

by:bchoor
ID: 17983217
ok - got it - it was PreInit not PreRender as I initially said. See a quick and dirty implementation - hopefully u get the idea.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.PopulateGrid()
    End Sub
   
    Protected Sub PopulateGrid()
        Dim tb As New Table
        Dim tr As New TableRow
       
        Dim tc As TableCell = Nothing
       
        tc = New TableCell
        Dim chkok As New CheckBox
        chkok.AutoPostBack = True
        AddHandler chkok.CheckedChanged, AddressOf chk_oncheckchanged
        tc.Controls.Add(chkok)
       
        tr.Cells.Add(tc)
        tb.Rows.Add(tr)
       
        Me.form1.Controls.Add(tb)
    End Sub
   
    Protected Sub chk_oncheckchanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkok As CheckBox = CType(sender, CheckBox)
        Response.Write("Checked: " & chkok.Checked.ToString())
    End Sub


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>
0
 

Author Comment

by:adismesevic
ID: 18009468
Hi, first of all thank you very much for your answers I leaned some stuff that i didnot know, but unfortunately it didnt help me...when i tried your last suggestion i got some other stuff that i couldnot avoid mm
Anyway i solved it with javascript...here is solution if anyone else woould need it....

chkOk.Attributes.Add("onclick", "chkChecked(document.forms[0].note_" + tdIndex.Text + ")")

 <script type="text/javascript" language="JavaScript">
   // functions to disable/enable dynamically created checkboxes/textboxes in tblSba
                function chkChecked(obj)
                    {
                     obj.disabled = !(obj.disabled);
                    }
                     
                function txtChanged(chkobj, noteobj)
                    {
                        if (noteobj.value == '') {
                         chkobj.disabled = false;
                         }
                         else if (noteobj.value != ''){
                         chkobj.disabled = true;                        
                        }
                     }
   </script>
0
 
LVL 10

Expert Comment

by:bchoor
ID: 18014578
awesome.. glad u got it working!

~BC
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 18076596
PAQd, 250 points refunded.

DarthMod
CS Moderator
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now