locked
Listview Updating and Deleting events not working RRS feed

  • Question

  • User856008996 posted
    <asp:ScriptManager ID="smQ" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="questionUP" runat="server" UpdateMode="Conditional">
            <ContentTemplate>   
          <asp:ListView ID="questionlist" runat="server" AutoPostBack="true" onitemupdating="questionlist_ItemUpdating" onitemdeleting="questionlist_ItemDeleting" OnItemDataBound="questionlist_ItemDataBound">
                            <LayoutTemplate>
                            <asp:PlaceHolder runat="server" ID="itemPlaceholder">
        <div ID="itemPlaceholderContainer" runat="server">
    
         </div>
    
        </asp:PlaceHolder>
                        </LayoutTemplate>
              <ItemTemplate> 
                  <asp:Panel CssClass="question" runat="server">    
    
                        <div class="exactly" runat="server">
                            
                            <asp:Button ID="exactly2" Text="" runat="server" CommandName="" />
                            <asp:Label ID="exactlynum" CssClass="exactlylbl" Text='' runat="server" />
                        </div>
                        <div class="questiontitle">
                            <asp:Label ID="questionID" Text='<%# Eval("ID") %>' runat="server"/>
                            <a href="s.aspx?ID=<%# Eval("ID")%>" class="questiontitlelbl" ><%# Eval("questionTitle").ToString().Substring(0,Math.Min(52,Eval("questionTitle").ToString().Length)) %></a>
                            
                        </div>
                        <div class="questionview" runat="server">
                            <div class="qaimg"><img width="16" src="e.png" /></div>
                            <a><asp:Label ID="viewlbl" CssClass="qaviewlbl" Text='<%#Eval("questionView")%>' runat="server" /></a>
                        </div>
                        
                        <div class="questionanswer" runat="server">
                            <div class="qaimg"><img width="16" src="p.png" /></div>
                            <a><asp:Label ID="answerlbl" CssClass="qaviewlbl" Text='<%#Eval("questionAnswer")%>' runat="server" /></a>
                        </div>
                        <div class="questiondate" runat="server">
                            <div class="calendar"><i class="fa fa-calendar-o"></i></div>
                            <a><asp:Label ID="questiondate" CssClass="questiondatelbl" Text='<%#Bind("questionDate", "{0:dd-M-yyyy}")%>' runat="server" /></a>
                        </div>
                   </asp:Panel>
    </ItemTemplate>
                        </asp:ListView>
                </ContentTemplate>
        </asp:UpdatePanel>
    MySqlConnection cn = new MySqlConnection("Server=; Database=; Uid=; Password=;");
            MySqlCommand cmd = new MySqlCommand();
            MySqlDataReader dr;
    
            private void question()
            {
                
    
                cmd = new MySqlCommand("Select * from question order by ID desc", cn);
    
                cn.Open();
    
                dr = cmd.ExecuteReader();
                questionlist.DataSource = dr;
                questionlist.DataBind();
    
                cn.Close();
               
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                question();
               
                
            }
    
            protected void questionlist_ItemUpdating(object sender, ListViewUpdateEventArgs e)
            {
                bool z;
                Label questionID = (questionlist.Items[e.ItemIndex].FindControl("questionID")) as Label;
                Label questionPlus = (questionlist.Items[e.ItemIndex].FindControl("exactlynum")) as Label;
                Button exactly2 = (questionlist.Items[e.ItemIndex].FindControl("exactly2")) as Button;
                //ImageButton exactly = (questionlist.Items[e.ItemIndex].FindControl("exactly")) as ImageButton;
    
                cmd = new MySqlCommand("Select * FROM questionLike WHERE questionID=@qID AND userID=@uID", cn);
                cmd.Parameters.AddWithValue("@qID", questionID.Text);
                cmd.Parameters.AddWithValue("@uID", Session["userID"]);
                cn.Open();
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    z = true;
                }
                else
                {
                    z = false;
                }
                cn.Close();
    
                if (z == false && Session["userID"] != null)
                {
                    if (exactly2.CommandName == "Update")
                    {
                        string cmds = string.Format("Insert into questionLike(questionID, userID) values(@qID, @uID)");
                        cmd = new MySqlCommand(cmds, cn);
                        cmd.Parameters.AddWithValue("@qID", questionID.Text);
                        cmd.Parameters.AddWithValue("@uID", Session["userID"]);
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        exactly2.Text = "Vazgeç";
                        exactly2.CommandName = "Delete";
                        cn.Close();
                    }
                }
                int uQPLus = -1;
                cmd = new MySqlCommand("select count(userID) from questionLike where questionID=@qID", cn);
                cmd.Parameters.AddWithValue("@qID", questionID.Text);
                cn.Open();
                uQPLus = Convert.ToInt32(cmd.ExecuteScalar());
                questionPlus.Text = uQPLus.ToString();
                cn.Close();
    
          
            }
    
            protected void questionlist_ItemDeleting(object sender, ListViewDeleteEventArgs e)
            {
                
                Label questionID = (questionlist.Items[e.ItemIndex].FindControl("questionID")) as Label;
                Label questionPlus = (questionlist.Items[e.ItemIndex].FindControl("exactlynum")) as Label;
                Button exactly2 = (questionlist.Items[e.ItemIndex].FindControl("exactly2")) as Button;
                //ImageButton exactly = (questionlist.Items[e.ItemIndex].FindControl("exactly")) as ImageButton;
    
                if (exactly2.CommandName == "Delete")
                {
                    cmd = new MySqlCommand("DELETE FROM questionLike WHERE userID=@uID AND questionID=@qID", cn);
                    cmd.Parameters.AddWithValue("@uID", Session["userID"]);
                    cmd.Parameters.AddWithValue("@qID", questionID.Text);
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    exactly2.Text = "Beğen";
                    exactly2.CommandName = "Update";
                    cn.Close();
                }
                int uQPLus = -1;
                cmd = new MySqlCommand("select count(userID) from questionLike where questionID=@qID", cn);
                cmd.Parameters.AddWithValue("@qID", questionID.Text);
                cn.Open();
                uQPLus = Convert.ToInt32(cmd.ExecuteScalar());
                questionPlus.Text = uQPLus.ToString();
                cn.Close();
    
            }
    
            protected void questionlist_ItemDataBound(object sender, ListViewItemEventArgs e)
            {
                Label questionID = (Label)e.Item.FindControl("questionID");
                Label questionPlus = (Label)e.Item.FindControl("exactlynum");
                Button exactly2 = (Button)e.Item.FindControl("exactly2");
            
               
                //ImageButton exactly = (ImageButton)e.Item.FindControl("exactly");
    
                MySqlConnection cn2 = new MySqlConnection("Server=; Database=; Uid=; Password=;");
                MySqlCommand cmd2 = new MySqlCommand();
                MySqlDataReader dr2;
    
    
                int uQPLus = -1;
                cmd2 = new MySqlCommand("select count(userID) from questionLike where questionID=@qID", cn2);
                cmd2.Parameters.AddWithValue("@qID", questionID.Text);
                cn2.Open();
                uQPLus = Convert.ToInt32(cmd2.ExecuteScalar());
                questionPlus.Text = uQPLus.ToString();
                cn2.Close();
    
                cmd2 = new MySqlCommand("Select * from questionLike where questionID=@qID and userID=@uID", cn2);
                cmd2.Parameters.AddWithValue("@uID", Session["userID"]);
                cmd2.Parameters.AddWithValue("@qID", questionID.Text);
                cn2.Open();
                dr2 = cmd2.ExecuteReader();
    
                if (dr2.Read())
                {
                    exactly2.Text = "Vazgeç";
                    exactly2.CommandName = "Delete";
    
    
                }
                else
                {
                    exactly2.Text = "Beğen";
                    exactly2.CommandName = "Update";
    
                }
    
                cn2.Close();
    
          
    
            }

    The same code I wrote here works fine on another page.

    But these codes do not work with a button in ListView.

    Tuesday, January 30, 2018 8:16 AM

Answers

  • User856008996 posted

    Source of error;

    question();

    Code running smoothly;

    if(!IsPostBack){

    question();

    }

    Thank you for your interest

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 31, 2018 11:44 AM

All replies

  • User-1838255255 posted

    Hi ulaskayalar,

    You say Updating and Deleting events not working, if you meet any exception message or other phenomenon? Also you say through button to trigger events, i checked your code, only one button in your code and the commandname is null of this button. Maybe this caused this issue. 

    As far as i know, you need assign the command name, then trigger the corresponding event. Please check this tutorial: 

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname(v=vs.80).aspx 

    Also i hope you could add a breakpoint to check the event if be trigger and check code how to run! 

    Best Regards,

    Eric Du 

    Wednesday, January 31, 2018 8:07 AM
  • User856008996 posted
    Thank you for your comment, but it does not trigger when I give the command name.
    Wednesday, January 31, 2018 9:30 AM
  • User856008996 posted

    Source of error;

    question();

    Code running smoothly;

    if(!IsPostBack){

    question();

    }

    Thank you for your interest

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 31, 2018 11:44 AM