-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.aspx.cs
More file actions
206 lines (183 loc) · 6.75 KB
/
chat.aspx.cs
File metadata and controls
206 lines (183 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class chat : System.Web.UI.Page
{
public string MsgFrm = "You have a message from";
string conString = ConfigurationManager.ConnectionStrings["MessengerConnection"].ConnectionString;
SqlConnection con;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Timeout == 0)
{
FormsAuthentication.SignOut();
Response.Redirect("Login.aspx");
}
else
{
if (Session["UserName"] != null)
{
Master.logout.Visible = true;
Master.lblusername.Text = "Hello ," + Session["UserName"].ToString();
Master.login.Visible = false;
Master.imgprofile.Visible = true;
Master.signup.Visible = false;
LabelSender.Text = Session["UserName"].ToString();
HtmlGenericControl FooterControl = (HtmlGenericControl)Page.Master.FindControl("ContentFooter");
FooterControl.Visible = false;
SqlConnection con = con = new SqlConnection(conString);
SqlCommand cmd;
cmd = new SqlCommand("select Avater from UsersData where Username='" + Session["UserName"].ToString() + "'", con);
con.Open();
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
Master.imgprofile.ImageUrl = read["Avater"].ToString();
}
con.Close();
}
else
{
Response.Redirect("Login.aspx");
}
if (!IsPostBack)
{
LoadChatList();
txtboxMsg.Focus();
}
else
{
int count = CheckNewMsg();
if (DataList4.Items.Count > 0)
{
// ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Javascript", "javascript:PlayTone()", true);
LoadChatList();
txtboxMsg.Focus();
}
else
{
// ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Javascript", "javascript:PauseTone()", true);
}
}
Page.Title = "Hi, " + LabelSender.Text;
}
}
protected void UsernameLabel_Click(object sender, EventArgs e)
{
LabelRecevier.Text = ((LinkButton)sender).Text;
LoadChatList();
MsgPanel.Visible = true;
txtboxMsg.Focus();
}
//صندوق الرسايل الجديد
int CheckNewMsg()
{
int count = 0;
count= DataList4.Items.Count;
return count;
}
//عرض الشات
void LoadChatList()
{
con = new SqlConnection(conString);
cmd = new SqlCommand("select Sender,ChatMsg from Chat where (Sender = @Sender and Receiver = @Receiver) or (Sender = @VSender and Receiver = @VReceiver) order by Id", con);
cmd.Parameters.AddWithValue("@sender", LabelSender.Text);
cmd.Parameters.AddWithValue("@Receiver", LabelRecevier.Text);
cmd.Parameters.AddWithValue("@VSender", LabelRecevier.Text);
cmd.Parameters.AddWithValue("@VReceiver", LabelSender.Text);
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
DataList2.DataSource = ds.Tables[0];
DataList2.DataBind();
// DataList2.;
con.Close();
SeenMsg();
}
// لو اليوزر شاف الرساله
void SeenMsg()
{
con = new SqlConnection(conString);
cmd = new SqlCommand("update Chat set RecevierSeen = 1 where Receiver = @Receiver and Sender = @Sender", con);
cmd.Parameters.AddWithValue("@Receiver", LabelSender.Text.Trim());
cmd.Parameters.AddWithValue("@Sender", LabelRecevier.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
protected void ButtonSend_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtboxMsg.Text))
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Javascript", "alert('Enter Message!'); ", true);
txtboxMsg.Focus();
}
else if (string.IsNullOrWhiteSpace(LabelRecevier.Text))
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Javascript", "alert('Choose Friend to begain chat!'); ", true);
txtboxMsg.Focus();
}
else
{
try
{
con = new SqlConnection(conString);
cmd = new SqlCommand("Insert into Chat (ChatMsg, Sender, Receiver,DateTime) values(@ChatMsg, @Sender, @Receiver,@DateTime)", con);
cmd.Parameters.AddWithValue("@ChatMsg", txtboxMsg.Text);
cmd.Parameters.AddWithValue("@Sender", LabelSender.Text.ToLower());
cmd.Parameters.AddWithValue("@Receiver", LabelRecevier.Text.ToLower());
cmd.Parameters.AddWithValue("@DateTime", DateTime.Now.ToString("dd/MM/yyyy hh:mm tt"));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtboxMsg.Text = "";
txtboxMsg.Focus();
LoadChatList();
}
catch (Exception ex)
{
FormsAuthentication.SignOut();
Response.Redirect("Login.aspx");
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Javascript", "alert('"+ex+"'); ", true);
}
}
}
protected void Timer1_Tick1(object sender, EventArgs e)
{
DataList1.DataBind();
DataList3.DataBind();
DataList4.DataBind();
LoadChatList();
if(UserTitle.InnerText != LabelRecevier.Text)
{UserTitle.Style.Add("color", "white");}
else {UserTitle.Style.Add("color", "black");}
}
protected string StyleChat(string str)
{
if (string.Equals(Server.HtmlEncode(str), Server.HtmlEncode(LabelSender.Text), StringComparison.OrdinalIgnoreCase))
{
return "SenderClass";
}
return "ReceiverClass";
}
//condation for title conversation;
protected string ExchangeTitle(string title)
{
if (String.IsNullOrWhiteSpace(title))
{
return "Welcome, " + LabelSender.Text.ToUpper();
}
return title;
}
}