Solved

CheckBoxList_SelectedIndexChanged Problem

Posted on 2013-06-20
8
360 Views
Last Modified: 2013-06-28
Hello Experts,

I have the following HTML and CodeBehind below that works ok. But the little problem that I'm facing is that if someone clicks on the actual CheckBox square then it runs the CheckBoxList_SelectedIndexChanged fine.

But if someone just clicks on the name/label of the CheckBox square value then the CheckBoxList_SelectedIndexChanged does not run.

Any idea how I can make the CheckBoxList_SelectedIndexChanged Event fire if the actual name/label is selected?

HTML:
<asp:Label ID="lblCurrentlyReceive" runat="server" CssClass="formlabel" Text="Do you currently receive:"></asp:Label>
                <asp:CheckBoxList ID="cblCurrentlyReceiveValues" OnSelectedIndexChanged="cblCurrentlyReceiveValues_SelectedIndexChanged" runat="server" AutoPostBack="true"></asp:CheckBoxList>
                <asp:Label ID="lblCurrentlyReceiveExplain" runat="server" Text="Explain"></asp:Label>
                <asp:TextBox ID="txtCurrentlyReceiveOtherInfo" runat="server"></asp:TextBox>

Open in new window


CodeBehind:
protected void Page_Load(object sender, EventArgs e)
    {
        txtCurrentlyReceiveOtherInfo.Visible = false;
        lblCurrentlyReceiveExplain.Visible = false;

        RetrieveCurrentlyReceieveData();
        }
    }  

protected void RetrieveCurrentlyReceieveData()
    {
        DataTable dtCurrentlyReceiveValues = new DataTable();

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TMF"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "RetrieveCurrentlyReceiveData";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        SqlDataAdapter adp = new SqlDataAdapter();
        adp.SelectCommand = cmd;
        adp.Fill(dtCurrentlyReceiveValues);

        try
        {
            conn.Open();

            cblCurrentlyReceiveValues.DataSource = dtCurrentlyReceiveValues;
            cblCurrentlyReceiveValues.DataValueField = "cr_id";
            cblCurrentlyReceiveValues.DataTextField = "cr_name";
            cblCurrentlyReceiveValues.DataBind();
        }

        catch (Exception ex)
        {
            ex.Message.ToString();
        }

        finally
        {
            conn.Close();
        }
    }

    protected void cblCurrentlyReceiveValues_SelectedIndexChanged(object sender, EventArgs e)
    {
        var strtext = cblCurrentlyReceiveValues.SelectedIndex.ToString();

        if (cblCurrentlyReceiveValues.SelectedIndex != -1)
        {
            strtext = cblCurrentlyReceiveValues.SelectedItem.Text;
        }

        if (strtext == "Other Government Assistance")
        {
            if (cblCurrentlyReceiveValues.SelectedItem.Selected)
            {
                txtCurrentlyReceiveOtherInfo.Visible = true;
                lblCurrentlyReceiveExplain.Visible = true;
            }
            else
            {
                txtCurrentlyReceiveOtherInfo.Visible = false;
                lblCurrentlyReceiveExplain.Visible = false;
            }
        }
        else
        {
            txtCurrentlyReceiveOtherInfo.Visible = false;
            lblCurrentlyReceiveExplain.Visible = false;
        }
    }
0
Comment
Question by:asp_net2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 13

Expert Comment

by:Molnar István
ID: 39263390
0
 
LVL 4

Author Comment

by:asp_net2
ID: 39263506
Hi quicksilver17,

Can you tell me or show me what I need to put in the TextChanged Event?
0
 
LVL 13

Expert Comment

by:Molnar István
ID: 39263690
i tried with the TextChanged but its not working
sorry but i thought that i can return the clicked text from the TextChanged EventArgs e

i'm sorry
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Molnar István
ID: 39263705
have you tried with other browsers too?
if i add items manually, it works
0
 
LVL 4

Author Comment

by:asp_net2
ID: 39263803
No, even if it did I can't have it solely rely on just certain browsers.
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39266392
When you debug the program which one is the value of the strtext on this line when you click on the text portion of the checkbox??

var strtext = cblCurrentlyReceiveValues.SelectedIndex.ToString();
0
 
LVL 4

Author Comment

by:asp_net2
ID: 39267491
@k-designers,

Hi, I set a breakpoint on the line that you suggested on your last post but when I ran my application that breakpoint was never hit, in other words there wasn't a value for strtext.

Below is the code that I'm using as mentioned in original post.

    protected void cblCurrentlyReceiveValues_SelectedIndexChanged(object sender, EventArgs e)
    {
        var strtext = cblCurrentlyReceiveValues.SelectedIndex.ToString();

        if (cblCurrentlyReceiveValues.SelectedIndex != -1)
        {
            strtext = cblCurrentlyReceiveValues.SelectedItem.Text;
        }

        if (strtext == "Other Government Assistance")
        {
            if (cblCurrentlyReceiveValues.SelectedItem.Selected)
            {
                txtCurrentlyReceiveOtherInfo.Visible = true;
                lblCurrentlyReceiveExplain.Visible = true;
            }
            else
            {
                txtCurrentlyReceiveOtherInfo.Visible = false;
                lblCurrentlyReceiveExplain.Visible = false;
            }
        }
        else
        {
            txtCurrentlyReceiveOtherInfo.Visible = false;
            lblCurrentlyReceiveExplain.Visible = false;
        }
    }

Open in new window

0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
ID: 39268785
That's the problem. Is not recognizing the event. Delete the sub and from the design side double click on the drop down to generate the select index event again and copy your code again. Add the breakpoint and check If pass throught the whole sub
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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