Solved

CheckedChanged Not Firing in DataList

Posted on 2015-01-05
6
98 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
[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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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