TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ulas Kayalar
NA
12
1.6k
Listview Updating and Deleting events not working
Jan 30 2018 2:27 AM
<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 =
"Begen"
;
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 =
"Begen"
;
exactly2.CommandName =
"Update"
;
}
cn2.Close();
}
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.
Reply
Answers (
5
)
Data export from Excel to XML
How to convert my HTML Code to Image in asp.net c#