?
Solved

CheckBoxList_SelectedIndexChanged Problem

Posted on 2013-06-20
8
Medium Priority
?
364 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
Independent Software Vendors: 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

770 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