Solved

Unchecked checkboxes' enable = false does not take effect

Posted on 2010-11-20
7
605 Views
Last Modified: 2012-05-10
have 5 items in datalist with checkboxes,
once one of the checkboxes is checked,
all checkboxes' 'Enabled' should turn to false.

but they still remaining true (checkable).

aspx
----
<asp:Datalist ID="dlsinglemarkthread" runat="server" Height="33px" Width="800px" 
                onitemdatabound="dlsinglemarkthread_ItemDataBound" onitemcreated="dlsinglemarkthread_ItemCreated">
                                                          <ItemTemplate>
                                                          <fieldset>
                                                                <table width=800 style="table-layout:fixed;">
	                                                            <colgroup>
	                                                            <col width=100 valign=top>
	                                                            <col width=400 valign=top>
                                                                <col width=90 valign=top>
	                                                            <col width=70 valign=top>
	                                                            <col width=100 valign=top>
	                                                            </colgroup>
	                                                            <tr>
		                                                            <td class="datalisttitlefont"><%# DataBinder.Eval(Container.DataItem, "UTname")%></td>
		                                                            <td class="datalisttitlefont"></td>
                                                                    <td class="datalisttitlefont"></td>
		                                                            <td class="datalisttitlefont"></td>
                                                                    <td class="datalisttitlefont"></td>
		                                                            
	                                                            <tr>
	                                                            <tr>
		                                                            <td style="white-space:nowrap;" class="datalistcontentfont">
                                                                     <img src= '<%#DataBinder.Eval(Container.DataItem, "ImagePath") %>'   
                                                                  style="height:50px;width:50px;border:1px solid gray;"/>

                                                                    </td>
		                                                            <td class="datalistcontentfont"><%# DataBinder.Eval(Container.DataItem, "ContentDesc")%></td>
                                                                    <td style="white-space:nowrap;"></td>
		                                                            <td style="white-space:nowrap;" class="datalistcontentfont">
                                                                    <%# DataBinder.Eval(Container.DataItem, "DateCreated")%>
                                                                    <ItemTemplate>
                                                                     <asp:Label ID="lblid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TPID") %>' Visible="false" />  

                                                                                
                                                                    </ItemTemplate>
                                                                    
                                                                    </td>
		                                                            <td style="white-space:nowrap;" class="datalistcontentfont">
                                                                    Is Solution
                                                                    </td>
	                                                            <tr>
                                                                <tr>
                                                                    <td></td>
                                                                    <td></td>
                                                                    <td></td>
                                                                    <td class="datalistcontentfont">
                                                                    <asp:Image ID="imgstatus" runat="server" ImageUrl='<%#DataBinder.Eval(Container.DataItem, "Status") %>'
                                                                     Height="50" Width="50" />
                                                                  
                                                                  </td>
                                                                    <td>
                                                                   
                                                                    <asp:CheckBox ID="chkanswer" runat="server" AutoPostBack="true" />
                                                                   
                                                                    </td>
                                                                </tr>
	                                                           
                                                            </table>
 


                                                                  

                                                             </fieldset>
                                                           </ItemTemplate>
                                                        </asp:Datalist>
code-behind
-----------
private void chkanswer_CheckChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < dlsinglemarkthread.Items.Count; i++)
        {
            CheckBox _cbanswer = (CheckBox)dlsinglemarkthread.Items[i].FindControl("chkanswer");
            Label _lblid = (Label)dlsinglemarkthread.Items[i].FindControl("lblid");

           

                    if (_cbanswer.Checked)
                    {
                        try
                        {
                            if (_lblid.Text != String.Empty)
                            {
                                threadpost.UpdateThreadPost(Convert.ToInt32(_lblid.Text));

                            }

                            _cbanswer.Enabled = false;
                        }
                        catch (Exception ex)
                        {
                            lblthreadempty.Text = ex.Message.ToString();
                            lblthreadempty.Visible = true;
                        }
                    }
                    else if (!_cbanswer.Checked)
                    {
                        _cbanswer.Enabled = false;
                    }

        }

       // udpmarkthread.Update();
    }

protected void dlsinglemarkthread_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
        {
            (e.Item.FindControl("chkanswer") as CheckBox).CheckedChanged += new EventHandler(chkanswer_CheckChanged);
            scmgr01.RegisterAsyncPostBackControl(e.Item.FindControl("chkanswer"));
        }
    }

Open in new window

0
Comment
Question by:doramail05
7 Comments
 
LVL 9

Expert Comment

by:rfportilla
ID: 34179338
I looked at the code and I don't see anything wrong.  HOwever, it sounds like you should be using radio button instead.  A radio button group will give you the functionality you are looking for: you can only select one at a time.
0
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34179390
did you try to debug and step into chkanswer_CheckChanged method?

I think your event handler chkanswer_CheckChanged is not executing because its not associated with the OnCheckedChanged event

change the method to 'protected' then add this code to the check box:
OnCheckedChanged="chkanswer_CheckChanged"
0
 
LVL 6

Expert Comment

by:iandian
ID: 34179532
it sounds like you mean .checked=false instead of .enabled=false
.checked controls whether the checkbox is checked or not
.enabled control whether users can do something with the checkbox
if .enables=false the checkbox will remain in the same state it was (checked or unchecked) but the state can no longer be changed.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 20

Expert Comment

by:informaniac
ID: 34180347
As pointed to by abdkhalif, place a breakpoint in your checkchanged event. Looks like you are using Ajax.

Is your DataList inside an updatepanel?
0
 
LVL 1

Accepted Solution

by:
doramail05 earned 0 total points
ID: 34181217
in the other project, they are encapsulated with updatepanel

and the checkbox is inside panel.

when doing this :
if (!objChk.Checked)
                    {
                        objChk.Enabled = false;

                       
                    }

all of the checkboxes turned enabled false with updatepanel update.
in this project, i first tested out without updatepanel and also tested checkboxed with panel, they won;t work.
now im having a response.redirect back to the page and set the checkboxes invisi and they worked,,
<asp:UpdatePanel ID="udppanel" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                        
                            <asp:Repeater ID="repTopicExam" runat="server" 
                                OnItemDataBound="repTopicExam_ItemDataBound" 
                                OnItemCreated="repTopicExam_ItemCreated" 
                                onitemcommand="repTopicExam_ItemCommand">
                            <HeaderTemplate></HeaderTemplate>
                            <ItemTemplate>   
                                <div class="repeaterbg">                     
                                <table width="100%" border="0" cellpadding="0" cellspacing="0">
                                    <tr >
                                        <td align="right" style="background-color:#F2F2F2; padding:3px;">
                                            <div style="float:right;">|
                                                <asp:HyperLink ID="hlcomment" runat="server" Text="Question Comment"  
                                                    onclick="somename=window.open('','questioncomment', 'resizable=no,scroll
                                                    bars=yes,status=no,toolbar=yes,height=500,width=300,left=50,top=40');somenam
                                                    e.focus();return true;">
                                                </asp:HyperLink>
                                            </div>
                                            <div style="float:right; padding-right:10px; line-height:15px;">
                                                <asp:Label ID="lblquestioncode" runat="server" Font-Size="Smaller" /><br />
                                                <asp:Label ID="lblqid" runat="server" Visible="false" />
                                                <asp:Label ID="lblchildid" runat="server" Visible="false" />
                                                <asp:Label ID="lblqno" runat="server" Visible="false" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td width="100%" valign="top" align="left">
                                            <div class="questionSection">
                                                <div class="questionNumber"><asp:Label id="lblquestionno" runat="server" /></div>
                                                <div class="questionContent"><asp:Label ID="lblTitle" runat="server"></asp:Label></div>
                                            </div>      
                                        </td>
                                        <td valign="top"  align="left">
                                            
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left" style="padding-left:15px;">
                                                <asp:Repeater ID="repAnswer" runat="server">
                                                <HeaderTemplate></HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:Panel ID="pnlAnswer" CssClass="answercontent" runat="server">
                                                            <div style="float:left; margin-top:10px; margin-bottom:10px; padding-right:5px;">
                                                                <asp:CheckBox ID="chkAnswer" CssClass="checkboxLayout" runat="server" AutoPostBack="true"/>
                                                            </div>
                                                            <div style="float:left;">
                                                                <asp:Label ID="lblanswer01" runat="server" />
                                                            </div>
                                                            <div style="padding:10px 0 0 0px; margin-left:100px;">
                                                                <asp:Image ID="imgAnswer" runat="server" Visible="false" ImageUrl="~images/icon_successful.png" />
                                                            </div>                                                          
                                                            
                                                        </asp:Panel>
                                                        
                                                    </ItemTemplate>
                                                </asp:Repeater>
                                          </td>
                                        </tr>           
                                </table>
                                 </div>   
                                    </ItemTemplate>
                             </asp:Repeater>
                            
                        </ContentTemplate>
                    </asp:UpdatePanel>

code-behind
-----------
 private void chkAnswer_CheckChanged(object sender, EventArgs e)
    {
        Boolean blnValid = false;
        string strItemID = (sender as CheckBox).ClientID;

        foreach (Control objItem in (sender as CheckBox).Parent.Parent.Parent.Controls)
        {
            blnValid = false;

            if (objItem.Controls.Count > 0)
            {
                Panel objPanel = ((objItem as RepeaterItem).FindControl("pnlAnswer") as Panel);

                if (objPanel.GetType().Name == "Panel")
                {
                    CheckBox objChk = (objPanel.FindControl("chkAnswer") as CheckBox);
                                       
                    string[] strArray = objChk.Attributes["item_id"].ToString().Split(new char[] { ':' });
     
                    //-- Start Validation

                    if (objChk.Checked)
                    {
                        

                        //using (dt1TableAdapters.AnswersTableAdapter adpt110 = new dt1TableAdapters.AnswersTableAdapter())
                        //{

                        //    DataTable dt110 = adpt110.GetAnswerData(Convert.ToInt32(strArray[1]));

                        using (SqlConnection sqlconn = new SqlConnection(connstring))
                        {
                            DataTable dt110 = new DataTable();
                            string strSQL = "SELECT id, question_id, name, status, createby, datecreate, updateby, dateupdate " +
                                "FROM Answers WHERE id = " + Convert.ToInt32(strArray[1]);
                            SqlDataAdapter adapter = new SqlDataAdapter(strSQL, sqlconn);
                            adapter.Fill(dt110);
                            
                            if (dt110.Rows.Count > 0)
                            {

                                    if (dt110.Rows[0]["status"].ToString() == "True")
                                    {
                                        //string jScriptValidator;
                                        //jScriptValidator = "<script>" +
                                        //            " function playSound(soundfile) { \n";
                                        //jScriptValidator += "document.getElementById(\" + dummy + \").innerHTML = \n ";
                                        //jScriptValidator += "<embed src=soundfile hidden=\"true\" autostart=\"true\" loop=\"false\" />; \n ";
                                        //jScriptValidator += "} </script>";
                                        //Page.RegisterStartupScript("regJSval", jScriptValidator);
                                        //objChk.Attributes.Add("OnCheckedChanged", "return playSound('files/applause.wav');");

                                        blnValid = true;

                                        if (Convert.ToInt32(Session["intcorrectcounter"]) == 3)
                                        {
                                            Session["intcorrectcounter"] = 3;
                                        }
                                        else
                                        {
                                            Session["intcorrectcounter"] = Convert.ToInt32(Session["intcorrectcounter"]) + 1;
                                        }

                                        if (Convert.ToInt32(Session["intcorrectcounter"]) == 1)
                                        {
                                            SoundPlayer myPlayer = new SoundPlayer();
                                            myPlayer.SoundLocation = Server.MapPath("~/files/applause.wav");
                                            myPlayer.Play();
                                        }
                                        else if (Convert.ToInt32(Session["intcorrectcounter"]) == 2)
                                        {
                                            SoundPlayer myPlayer = new SoundPlayer();
                                            myPlayer.SoundLocation = Server.MapPath("~/files/chainsaw.wav");
                                            myPlayer.Play();
                                        }
                                        else if (Convert.ToInt32(Session["intcorrectcounter"]) == 3)
                                        {
                                            SoundPlayer myPlayer = new SoundPlayer();
                                            myPlayer.SoundLocation = Server.MapPath("~/files/electric.wav");
                                            myPlayer.Play();


                                        }

                                                                                                      
                                        Session["correctanswercount"] = Convert.ToInt32(Session["correctanswercount"]) + 1;

                                        Session["eTopicCorrectAnswer"] = Session["correctanswercount"];

                                        Image objImg = (objPanel.FindControl("imgAnswer") as Image);
                                        objImg.Visible = blnValid;

                                        objChk.Enabled = false;
                                                                                                                     }
                                    else if (dt110.Rows[0]["status"].ToString() == "False")
                                    {
                                        Session["intcorrectcounter"] = 0;
                                    }
                    
                            }
                        }

                       
                    }

                    if (!objChk.Checked)
                    {
                        objChk.Enabled = false;

                        
                    }

                    //Image objImg = (objPanel.FindControl("imgAnswer") as Image);
                    //objImg.Visible = blnValid;
                }
            }
        }

        int intCountPercentage = CalculatePoint(Convert.ToInt32(Session["eTopicCorrectAnswer"]), Convert.ToInt32(Session["eTopicCountQuestion"]));

        Session["intCountPercentageTopic"] = intCountPercentage;

        udppanel.Update();
    }

Open in new window

0
 
LVL 20

Expert Comment

by:informaniac
ID: 34183435
It's good that u found a workaround. Although it works, can u show us the triggers for the updatepanel that u have set, to check if we can find a solution...
0
 
LVL 1

Author Closing Comment

by:doramail05
ID: 34237316
workaround might solved
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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