Solved

CheckBoxList_SelectedIndexChanged Problem

Posted on 2013-06-20
8
355 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

17 Experts available now in Live!

Get 1:1 Help Now