Solved

CheckBoxList_SelectedIndexChanged Problem

Posted on 2013-06-20
8
352 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
Comment Utility
0
 
LVL 4

Author Comment

by:asp_net2
Comment Utility
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
Comment Utility
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
Comment Utility
have you tried with other browsers too?
if i add items manually, it works
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Author Comment

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

Expert Comment

by:Jesus Rodriguez
Comment Utility
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
Comment Utility
@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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 33
C# Update Status Bar.... 1 22
Exception in Log4Net 1 17
Round a string to two digits 12 23
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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

10 Experts available now in Live!

Get 1:1 Help Now