Ulas Kayalar

Ulas Kayalar

  • NA
  • 12
  • 1.5k

Listview Updating and Deleting events not working

Jan 30 2018 2:27 AM
  1. <asp:ScriptManager ID="smQ" runat="server"></asp:ScriptManager>  
  2.     <asp:UpdatePanel ID="questionUP" runat="server" UpdateMode="Conditional">  
  3.         <ContentTemplate>     
  4.       <asp:ListView ID="questionlist" runat="server" AutoPostBack="true" onitemupdating="questionlist_ItemUpdating" onitemdeleting="questionlist_ItemDeleting" OnItemDataBound="questionlist_ItemDataBound">  
  5.                         <LayoutTemplate>  
  6.                         <asp:PlaceHolder runat="server" ID="itemPlaceholder">  
  7.     <div ID="itemPlaceholderContainer" runat="server">  
  8.   
  9.      </div>  
  10.   
  11.     </asp:PlaceHolder>  
  12.                     </LayoutTemplate>  
  13.           <ItemTemplate>   
  14.               <asp:Panel CssClass="question" runat="server">      
  15.   
  16.                     <div class="exactly" runat="server">  
  17.                           
  18.                         <asp:Button ID="exactly2" Text="" runat="server" CommandName="" />  
  19.                         <asp:Label ID="exactlynum" CssClass="exactlylbl" Text='' runat="server" />  
  20.                     </div>  
  21.                     <div class="questiontitle">  
  22.                         <asp:Label ID="questionID" Text='<%# Eval("ID") %>' runat="server"/>  
  23.                         <a href="s.aspx?ID=<%# Eval("ID")%>" class="questiontitlelbl" ><%# Eval("questionTitle").ToString().Substring(0,Math.Min(52,Eval("questionTitle").ToString().Length)) %></a>  
  24.                           
  25.                     </div>  
  26.                     <div class="questionview" runat="server">  
  27.                         <div class="qaimg"><img width="16" src="e.png" /></div>  
  28.                         <a><asp:Label ID="viewlbl" CssClass="qaviewlbl" Text='<%#Eval("questionView")%>' runat="server" /></a>  
  29.                     </div>  
  30.                       
  31.                     <div class="questionanswer" runat="server">  
  32.                         <div class="qaimg"><img width="16" src="p.png" /></div>  
  33.                         <a><asp:Label ID="answerlbl" CssClass="qaviewlbl" Text='<%#Eval("questionAnswer")%>' runat="server" /></a>  
  34.                     </div>  
  35.                     <div class="questiondate" runat="server">  
  36.                         <div class="calendar"><i class="fa fa-calendar-o"></i></div>  
  37.                         <a><asp:Label ID="questiondate" CssClass="questiondatelbl" Text='<%#Bind("questionDate", "{0:dd-M-yyyy}")%>' runat="server" /></a>  
  38.                     </div>  
  39.                </asp:Panel>  
  40. </ItemTemplate>  
  41.                     </asp:ListView>  
  42.             </ContentTemplate>  
  43.     </asp:UpdatePanel>  
  1. MySqlConnection cn = new MySqlConnection("Server=; Database=; Uid=; Password=;");  
  2.         MySqlCommand cmd = new MySqlCommand();  
  3.         MySqlDataReader dr;  
  4.   
  5.         private void question()  
  6.         {  
  7.               
  8.   
  9.             cmd = new MySqlCommand("Select * from question order by ID desc", cn);  
  10.   
  11.             cn.Open();  
  12.   
  13.             dr = cmd.ExecuteReader();  
  14.             questionlist.DataSource = dr;  
  15.             questionlist.DataBind();  
  16.   
  17.             cn.Close();  
  18.              
  19.         }  
  20.         protected void Page_Load(object sender, EventArgs e)  
  21.         {  
  22.             question();  
  23.              
  24.               
  25.         }  
  26.   
  27.         protected void questionlist_ItemUpdating(object sender, ListViewUpdateEventArgs e)  
  28.         {  
  29.             bool z;  
  30.             Label questionID = (questionlist.Items[e.ItemIndex].FindControl("questionID")) as Label;  
  31.             Label questionPlus = (questionlist.Items[e.ItemIndex].FindControl("exactlynum")) as Label;  
  32.             Button exactly2 = (questionlist.Items[e.ItemIndex].FindControl("exactly2")) as Button;  
  33.             //ImageButton exactly = (questionlist.Items[e.ItemIndex].FindControl("exactly")) as ImageButton;  
  34.   
  35.             cmd = new MySqlCommand("Select * FROM questionLike WHERE questionID=@qID AND userID=@uID", cn);  
  36.             cmd.Parameters.AddWithValue("@qID", questionID.Text);  
  37.             cmd.Parameters.AddWithValue("@uID", Session["userID"]);  
  38.             cn.Open();  
  39.             dr = cmd.ExecuteReader();  
  40.             if (dr.Read())  
  41.             {  
  42.                 z = true;  
  43.             }  
  44.             else  
  45.             {  
  46.                 z = false;  
  47.             }  
  48.             cn.Close();  
  49.   
  50.             if (z == false && Session["userID"] != null)  
  51.             {  
  52.                 if (exactly2.CommandName == "Update")  
  53.                 {  
  54.                     string cmds = string.Format("Insert into questionLike(questionID, userID) values(@qID, @uID)");  
  55.                     cmd = new MySqlCommand(cmds, cn);  
  56.                     cmd.Parameters.AddWithValue("@qID", questionID.Text);  
  57.                     cmd.Parameters.AddWithValue("@uID", Session["userID"]);  
  58.                     cn.Open();  
  59.                     cmd.ExecuteNonQuery();  
  60.                     exactly2.Text = "Vazgeç";  
  61.                     exactly2.CommandName = "Delete";  
  62.                     cn.Close();  
  63.                 }  
  64.             }  
  65.             int uQPLus = -1;  
  66.             cmd = new MySqlCommand("select count(userID) from questionLike where questionID=@qID", cn);  
  67.             cmd.Parameters.AddWithValue("@qID", questionID.Text);  
  68.             cn.Open();  
  69.             uQPLus = Convert.ToInt32(cmd.ExecuteScalar());  
  70.             questionPlus.Text = uQPLus.ToString();  
  71.             cn.Close();  
  72.   
  73.         
  74.         }  
  75.   
  76.         protected void questionlist_ItemDeleting(object sender, ListViewDeleteEventArgs e)  
  77.         {  
  78.               
  79.             Label questionID = (questionlist.Items[e.ItemIndex].FindControl("questionID")) as Label;  
  80.             Label questionPlus = (questionlist.Items[e.ItemIndex].FindControl("exactlynum")) as Label;  
  81.             Button exactly2 = (questionlist.Items[e.ItemIndex].FindControl("exactly2")) as Button;  
  82.             //ImageButton exactly = (questionlist.Items[e.ItemIndex].FindControl("exactly")) as ImageButton;  
  83.   
  84.             if (exactly2.CommandName == "Delete")  
  85.             {  
  86.                 cmd = new MySqlCommand("DELETE FROM questionLike WHERE userID=@uID AND questionID=@qID", cn);  
  87.                 cmd.Parameters.AddWithValue("@uID", Session["userID"]);  
  88.                 cmd.Parameters.AddWithValue("@qID", questionID.Text);  
  89.                 cn.Open();  
  90.                 cmd.ExecuteNonQuery();  
  91.                 exactly2.Text = "Begen";  
  92.                 exactly2.CommandName = "Update";  
  93.                 cn.Close();  
  94.             }  
  95.             int uQPLus = -1;  
  96.             cmd = new MySqlCommand("select count(userID) from questionLike where questionID=@qID", cn);  
  97.             cmd.Parameters.AddWithValue("@qID", questionID.Text);  
  98.             cn.Open();  
  99.             uQPLus = Convert.ToInt32(cmd.ExecuteScalar());  
  100.             questionPlus.Text = uQPLus.ToString();  
  101.             cn.Close();  
  102.   
  103.         }  
  104.   
  105.         protected void questionlist_ItemDataBound(object sender, ListViewItemEventArgs e)  
  106.         {  
  107.             Label questionID = (Label)e.Item.FindControl("questionID");  
  108.             Label questionPlus = (Label)e.Item.FindControl("exactlynum");  
  109.             Button exactly2 = (Button)e.Item.FindControl("exactly2");  
  110.           
  111.              
  112.             //ImageButton exactly = (ImageButton)e.Item.FindControl("exactly");  
  113.   
  114.             MySqlConnection cn2 = new MySqlConnection("Server=; Database=; Uid=; Password=;");  
  115.             MySqlCommand cmd2 = new MySqlCommand();  
  116.             MySqlDataReader dr2;  
  117.   
  118.   
  119.             int uQPLus = -1;  
  120.             cmd2 = new MySqlCommand("select count(userID) from questionLike where questionID=@qID", cn2);  
  121.             cmd2.Parameters.AddWithValue("@qID", questionID.Text);  
  122.             cn2.Open();  
  123.             uQPLus = Convert.ToInt32(cmd2.ExecuteScalar());  
  124.             questionPlus.Text = uQPLus.ToString();  
  125.             cn2.Close();  
  126.   
  127.             cmd2 = new MySqlCommand("Select * from questionLike where questionID=@qID and userID=@uID", cn2);  
  128.             cmd2.Parameters.AddWithValue("@uID", Session["userID"]);  
  129.             cmd2.Parameters.AddWithValue("@qID", questionID.Text);  
  130.             cn2.Open();  
  131.             dr2 = cmd2.ExecuteReader();  
  132.   
  133.             if (dr2.Read())  
  134.             {  
  135.                 exactly2.Text = "Vazgeç";  
  136.                 exactly2.CommandName = "Delete";  
  137.   
  138.   
  139.             }  
  140.             else  
  141.             {  
  142.                 exactly2.Text = "Begen";  
  143.                 exactly2.CommandName = "Update";  
  144.   
  145.             }  
  146.   
  147.             cn2.Close();  
  148.   
  149.         
  150.   
  151.         }  
These codes do not work on the page that I am currently viewing when running on another page. When I want to detect an error with a breakpoint, I can not get any feedback because the button is not fired.

Answers (5)