• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

Unchecked checkboxes' enable = false does not take effect

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
doramail05
Asked:
doramail05
1 Solution
 
rfportillaCommented:
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
 
abdkhlaifCommented:
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
 
iandianCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Obadiah ChristopherCommented:
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
 
doramail05Author Commented:
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
 
Obadiah ChristopherCommented:
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
 
doramail05Author Commented:
workaround might solved
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now