Solved

CheckedChanged Not Firing in DataList

Posted on 2015-01-05
6
97 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
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…

809 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