Solved

CheckedChanged Not Firing in DataList

Posted on 2015-01-05
6
96 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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