Solved

CheckedChanged Not Firing in DataList

Posted on 2015-01-05
6
91 Views
Last Modified: 2015-01-07
When I click on the checkbox, I want the value to be inserted into the database. What am I doing wrong?

                    <asp:DataList ID="PicturesView" DataKeyField="PicturesID"  runat="server">
                    <ItemTemplate>
                        <asp:CheckBox ID="PrimaryPhoto" AutoPostBack="true" OnCheckedChanged="PrimaryPhoto_CheckedChanged" runat="server" />
			<asp:Image ID="PictureImage" ImageUrl='<%#Eval("PicturePath")%>' runat="server" />
                    </ItemTemplate>
                </asp:DataList>    

	protected void PrimaryPhoto_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            DataListItem item = (DataListItem)chk.NamingContainer;
            if (chk.Checked)
            {
                string checkedValue = PicturesView.DataKeys[item.ItemIndex].ToString();
                SqlConnection conn;
                SqlCommand comm;
                string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                conn = new SqlConnection(connectionString);
                comm = new SqlCommand("UPDATE ProfilePictures SET PrimaryPicture=@PrimaryPicture WHERE ProfilePicturesID = @ID", conn);
                comm.Parameters.Add("@ID", System.Data.SqlDbType.BigInt);
                comm.Parameters["@ID"].Value = checkedValue;
                comm.Parameters.Add("@PrimaryPicture", System.Data.SqlDbType.VarChar);
                comm.Parameters["@PrimaryPicture"].Value = "Y";
                try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
                finally
                {
                    conn.Close();
                }
            }
        }

Open in new window

0
Comment
Question by:RecipeDan
  • 3
  • 2
6 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 250 total points
ID: 40531641
I would put a catch in your try statement to see what type of error is being generated
that's a good palce to start
try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
 catch (Exception ex)
{
// make a label
lbl1.text = ex.message;
}
                finally
                {
                    conn.Close();
                }

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40531665
I am not getting any error message.

            CheckBox chk = sender as CheckBox;
            DataListItem item = (DataListItem)chk.NamingContainer;
            if (chk.Checked)
            {
                string checkedValue = PicturesView.DataKeys[item.ItemIndex].ToString();
                SqlConnection conn;
                SqlCommand comm;
                string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                conn = new SqlConnection(connectionString);
                comm = new SqlCommand("UPDATE ProfilePictures SET PrimaryPicture=@PrimaryPicture WHERE ProfilePicturesID = @ID", conn);
                comm.Parameters.Add("@ID", System.Data.SqlDbType.BigInt);
                comm.Parameters["@ID"].Value = checkedValue;
                comm.Parameters.Add("@PrimaryPicture", System.Data.SqlDbType.VarChar);
                comm.Parameters["@PrimaryPicture"].Value = "Y";
                try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    label1.Text = ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

Open in new window

0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40531670
you Stepped through everything with F8 ?


try it also without the if statement
 string checkedValue = PicturesView.DataKeys[item.ItemIndex].ToString();
                SqlConnection conn;
                SqlCommand comm;
                string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                conn = new SqlConnection(connectionString);
                comm = new SqlCommand("UPDATE ProfilePictures SET PrimaryPicture=@PrimaryPicture WHERE ProfilePicturesID = @ID", conn);
                comm.Parameters.Add("@ID", System.Data.SqlDbType.BigInt);
                comm.Parameters["@ID"].Value = checkedValue;
                comm.Parameters.Add("@PrimaryPicture", System.Data.SqlDbType.VarChar);
                comm.Parameters["@PrimaryPicture"].Value = "Y";
                try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    label1.Text = ex.Message;
                }
                finally
                {
                    conn.Close();
                }

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:RecipeDan
ID: 40531693
Yes. I even tried just a Response.Redirect("http://www.google.com") by itself just to see if the oncheckedchanged will fire and nothing happens.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 250 total points
ID: 40534001
- Did you try setting breakpoint in Page_Load?
- Are you doing any DataBinding in PageLoad?
- Are there any javascript errors?: If yes, they prevent postback
- Do you have any validations on the page? - If some validation is failing, it will prevent postback
- Make sure your page don't have AutoEventWireup="false",

Share your entire .aspx and .cs code.
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40536053
Thank you both for your assistance. It turns out the error that nothing to do with the oncheckedchanged. A validator was causing the oncheckedchanged not to fire.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

19 Experts available now in Live!

Get 1:1 Help Now