Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Answears.aspx.cs" Inherits="HTML_LAB5_6.Answears" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>Answears</h1>
- <a href="Questions.aspx">Questions</a><br />
- <br />
- <a href="Users.aspx">Users</a><br />
- <br />
- <a href="ChatLogin.aspx">ChatLogin</a><br />
- <br />
- <a href="Chat.aspx">Chat</a><br />
- <br />
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1">
- <AlternatingItemTemplate>
- <tr style="">
- <td>
- <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
- </td>
- <td>
- <asp:Label ID="IdPytaniaLabel" runat="server" Text='<%# Eval("IdPytania") %>' />
- </td>
- <td>
- <asp:Label ID="OdpowiedzLabel" runat="server" Text='<%# Eval("Odpowiedz") %>' />
- </td>
- </tr>
- </AlternatingItemTemplate>
- <EditItemTemplate>
- <tr style="">
- <td>
- <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
- <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
- </td>
- <td>
- <asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' />
- </td>
- <td>
- <asp:TextBox ID="IdPytaniaTextBox" runat="server" Text='<%# Bind("IdPytania") %>' />
- </td>
- <td>
- <asp:TextBox ID="OdpowiedzTextBox" runat="server" Text='<%# Bind("Odpowiedz") %>' />
- </td>
- </tr>
- </EditItemTemplate>
- <EmptyDataTemplate>
- <table runat="server" style="">
- <tr>
- <td>No data was returned.</td>
- </tr>
- </table>
- </EmptyDataTemplate>
- <InsertItemTemplate>
- <tr style="">
- <td>
- <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
- <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
- </td>
- <td>
- <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' />
- </td>
- <td>
- <asp:TextBox ID="IdPytaniaTextBox" runat="server" Text='<%# Bind("IdPytania") %>' />
- </td>
- <td>
- <asp:TextBox ID="OdpowiedzTextBox" runat="server" Text='<%# Bind("Odpowiedz") %>' />
- </td>
- </tr>
- </InsertItemTemplate>
- <ItemTemplate>
- <tr style="">
- <td>
- <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
- </td>
- <td>
- <asp:Label ID="IdPytaniaLabel" runat="server" Text='<%# Eval("IdPytania") %>' />
- </td>
- <td>
- <asp:Label ID="OdpowiedzLabel" runat="server" Text='<%# Eval("Odpowiedz") %>' />
- </td>
- </tr>
- </ItemTemplate>
- <LayoutTemplate>
- <table runat="server">
- <tr runat="server">
- <td runat="server">
- <table id="itemPlaceholderContainer" runat="server" border="0" style="">
- <tr runat="server" style="">
- <th runat="server">Id</th>
- <th runat="server">IdPytania</th>
- <th runat="server">Odpowiedz</th>
- </tr>
- <tr id="itemPlaceholder" runat="server">
- </tr>
- </table>
- </td>
- </tr>
- <tr runat="server">
- <td runat="server" style=""></td>
- </tr>
- </table>
- </LayoutTemplate>
- <SelectedItemTemplate>
- <tr style="">
- <td>
- <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
- </td>
- <td>
- <asp:Label ID="IdPytaniaLabel" runat="server" Text='<%# Eval("IdPytania") %>' />
- </td>
- <td>
- <asp:Label ID="OdpowiedzLabel" runat="server" Text='<%# Eval("Odpowiedz") %>' />
- </td>
- </tr>
- </SelectedItemTemplate>
- </asp:ListView>
- <asp:DataPager ID="ProductListPagerSimple" runat="server"
- PagedControlID="ListView1" PageSize="5">
- <Fields>
- <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
- ShowLastPageButton="True" />
- </Fields>
- </asp:DataPager>
- <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Id], [IdPytania], [Odpowiedz] FROM [Answears] WHERE ([IdPytania] = @IdPytania)">
- <SelectParameters>
- <asp:QueryStringParameter Name="IdPytania" QueryStringField="QuestionsID" Type="Int32" />
- </SelectParameters>
- </asp:SqlDataSource>
- <br />
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" Text="Dodaj odpowiedz" OnClick="Button1_Click" />
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace HTML_LAB5_6
- {
- public partial class Answears : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Label1.Text = (String)Session["QuestionID"];
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
- con.Open();
- try
- {
- string sel = "Select count(*) from Answears";
- SqlCommand scm = new SqlCommand(sel, con);
- int id = Convert.ToInt32(scm.ExecuteScalar().ToString());
- sel = "insert into Answears (Id,IdPytania,Odpowiedz) values (@id,@idPytania,@pytanie)";
- scm = new SqlCommand(sel, con);
- scm.Parameters.AddWithValue("@id", id);
- scm.Parameters.AddWithValue("@idPytania", Request.QueryString["QuestionsID"]);
- scm.Parameters.AddWithValue("@pytanie", TextBox1.Text);
- scm.ExecuteNonQuery();
- con.Close();
- Response.Redirect("Answears.aspx?QuestionsID=" + Request.QueryString["QuestionsID"]);
- }
- catch (Exception ex)
- {
- Response.Write("Błąd: " + ex.ToString());
- con.Close();
- }
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Chat.aspx.cs" Inherits="HTML_LAB5_6.Chat" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>Chat</h1>
- <a href="Questions.aspx">Questions</a><br />
- <br />
- <a href="Answears.aspx">Answears</a><br />
- <br />
- <a href="Users.aspx">Users</a><br />
- <br />
- <a href="ChatLogin.aspx">ChatLogin</a><br />
- <br />
- <h3>
- <asp:Label ID="Label1" runat="server" Text=""></asp:Label></h3>
- <br />
- <br />
- <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="MyListView_ItemCommand">
- <AlternatingItemTemplate>
- <li style="background-color: #FFF8DC;">User:
- <asp:Label ID="UserLabel" runat="server" Text='<%# Eval("Userr") %>' />
- <br />
- Message:
- <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' />
- <br />
- <asp:Button ID="ButtonRemove" CommandName="Remove" Text="Usun" CommandArgument='<%# Eval("IdMsg") %>' Enabled='<%# Eval("Userr").ToString() == (String)Session["ChatUser"] %>' runat="server" />
- </li>
- </AlternatingItemTemplate>
- <EditItemTemplate>
- <li style="background-color: #008A8C; color: #FFFFFF;">User:
- <asp:TextBox ID="UserTextBox" runat="server" Text='<%# Bind("Userr") %>' />
- <br />
- Message:
- <asp:TextBox ID="MessageTextBox" runat="server" Text='<%# Bind("Message") %>' />
- <br />
- <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
- <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
- </li>
- </EditItemTemplate>
- <EmptyDataTemplate>
- No data was returned.
- </EmptyDataTemplate>
- <InsertItemTemplate>
- <li style="">User:
- <asp:TextBox ID="UserTextBox" runat="server" Text='<%# Bind("Userr") %>' />
- <br />
- Message:
- <asp:TextBox ID="MessageTextBox" runat="server" Text='<%# Bind("Message") %>' />
- <br />
- <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
- <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
- </li>
- </InsertItemTemplate>
- <ItemSeparatorTemplate>
- <br />
- </ItemSeparatorTemplate>
- <ItemTemplate>
- <li style="background-color: #DCDCDC; color: #000000;">User:
- <asp:Label ID="UserLabel" runat="server" Text='<%# Eval("Userr") %>' />
- <br />
- Message:
- <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' />
- <br />
- <asp:Button ID="ButtonRemove" CommandName="Remove" Text="Usun" CommandArgument='<%# Eval("IdMsg") %>' Enabled='<%# Eval("Userr").ToString() == (String)Session["ChatUser"] %>' runat="server" />
- </li>
- </ItemTemplate>
- <LayoutTemplate>
- <ul id="itemPlaceholderContainer" runat="server" style="font-family: Verdana, Arial, Helvetica, sans-serif;">
- <li runat="server" id="itemPlaceholder" />
- </ul>
- <div style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;">
- </div>
- </LayoutTemplate>
- <SelectedItemTemplate>
- <li style="background-color: #008A8C; font-weight: bold; color: #FFFFFF;">User:
- <asp:Label ID="UserLabel" runat="server" Text='<%# Eval("Userr") %>' />
- <br />
- Message:
- <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' />
- <br />
- </li>
- </SelectedItemTemplate>
- </asp:ListView>
- <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT TOP 5 [IdMsg], [Userr], [Message] FROM [ChatMsg] ORDER BY [IdMsg] DESC"></asp:SqlDataSource>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" Text="Wyslij" OnClick="Button1_Click" />
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace HTML_LAB5_6
- {
- public partial class Chat : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Label1.Text = "Witaj "+(String)Session["ChatUser"]+" na chacie!";
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- String msg = TextBox1.Text;
- SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
- con.Open();
- try
- {
- string sel = "SELECT top 1 IdMsg from ChatMsg ORDER BY IdMsg DESC";
- SqlCommand scm = new SqlCommand(sel, con);
- int id = Convert.ToInt32(scm.ExecuteScalar().ToString());
- id++;
- sel = "insert into ChatMsg (IdMsg,Userr,Message) values (@id,@user,@message)";
- scm = new SqlCommand(sel, con);
- scm.Parameters.AddWithValue("@id", id);
- scm.Parameters.AddWithValue("@user", (String)Session["ChatUser"]);
- scm.Parameters.AddWithValue("@message", TextBox1.Text);
- scm.ExecuteNonQuery();
- con.Close();
- Response.Redirect("Chat.aspx");
- }
- catch (Exception ex)
- {
- Response.Write("Błąd: " + ex.ToString());
- con.Close();
- }
- }
- protected void MyListView_ItemCommand(object sender, CommandEventArgs e)
- {
- int id;
- //Response.Write("<script>alert('Dziala')</script>");
- if (!int.TryParse((string)e.CommandArgument, out id))
- {
- Response.Write(id);
- // log.Write("possible sql injection");
- return;
- }
- SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
- con.Open();
- try
- {
- string sel = "delete from ChatMsg where IdMsg = '"+id+"'";
- SqlCommand scm = new SqlCommand(sel, con);
- scm.ExecuteNonQuery();
- con.Close();
- Response.Redirect("Chat.aspx");
- }
- catch (Exception ex)
- {
- Response.Write("Błąd: " + ex.ToString());
- con.Close();
- }
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChatLogin.aspx.cs" Inherits="HTML_LAB5_6.ChatLogin" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>ChatLogin</h1>
- <a href="Questions.aspx">Questions</a><br />
- <br />
- <a href="Answears.aspx">Answears</a><br />
- <br />
- <a href="Users.aspx">Users</a><br />
- <br />
- <asp:Label ID="Label1" runat="server" Text="Wpisz nazwe uzytkownika:"></asp:Label><br />
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
- <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
- <br />
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace HTML_LAB5_6
- {
- public partial class ChatLogin : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
- con.Open();
- try
- {
- string sel = "Select count(*) from ChatUsers where Userr = '"+TextBox1.Text+"'";
- SqlCommand scm = new SqlCommand(sel, con);
- int user = Convert.ToInt32(scm.ExecuteScalar().ToString());
- if (user == 0)
- {
- sel = "insert into ChatUsers (Userr) values (@user)";
- scm = new SqlCommand(sel, con);
- scm.Parameters.AddWithValue("@user", TextBox1.Text);
- scm.ExecuteNonQuery();
- Session["ChatUser"]= TextBox1.Text;
- }else
- {
- Session["ChatUser"] = TextBox1.Text;
- }
- con.Close();
- Response.Redirect("Chat.aspx");
- }
- catch (Exception ex)
- {
- Response.Write("Błąd: " + ex.ToString());
- con.Close();
- }
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Questions.aspx.cs" Inherits="HTML_LAB5_6.Questions" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>Questions</h1>
- <a href="Answears.aspx">Answears</a><br />
- <br />
- <a href="Users.aspx">Users</a><br />
- <br />
- <a href="ChatLogin.aspx">ChatLogin</a><br />
- <br />
- <a href="Chat.aspx">Chat</a><br />
- <br />
- <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="IdPytania" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" PageSize="5" Width="558px" >
- <Columns>
- <asp:CommandField ShowSelectButton="True"/>
- <asp:BoundField DataField="IdPytania" HeaderText="IdPytania" ReadOnly="True" SortExpression="IdPytania" />
- <asp:BoundField DataField="Pytanie" HeaderText="Pytanie" SortExpression="Pytanie" />
- </Columns>
- </asp:GridView>
- <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Questions]"></asp:SqlDataSource>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" Text="Dodaj pytanie" OnClick="Button1_Click" />
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace HTML_LAB5_6
- {
- public partial class Questions : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
- con.Open();
- try
- {
- string sel = "Select count(*) from Questions";
- SqlCommand scm = new SqlCommand(sel, con);
- int id = Convert.ToInt32(scm.ExecuteScalar().ToString());
- sel = "insert into Questions (IdPytania,Pytanie) values (@id,@pytanie)";
- scm = new SqlCommand(sel, con);
- scm.Parameters.AddWithValue("@id", id);
- scm.Parameters.AddWithValue("@pytanie", TextBox1.Text);
- scm.ExecuteNonQuery();
- con.Close();
- Response.Redirect("Questions.aspx");
- }
- catch (Exception ex)
- {
- Response.Write("Błąd: " + ex.ToString());
- con.Close();
- }
- }
- protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
- {
- string id = GridView1.SelectedRow.Cells[1].Text;
- string pytanie = GridView1.SelectedRow.Cells[2].Text;
- Session["QuestionID"] = pytanie;
- Response.Redirect("~/Answears.aspx?QuestionsID="+id);
- }
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='aquamarine';";
- e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
- e.Row.ToolTip = "Click last column for selecting this row.";
- }
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Users.aspx.cs" Inherits="HTML_LAB5_6.Users" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>Users</h1>
- <a href="Questions.aspx">Questions</a><br />
- <br />
- <a href="Answears.aspx">Answears</a><br />
- <br />
- <a href="ChatLogin.aspx">ChatLogin</a><br />
- <br />
- <a href="Chat.aspx">Chat</a><br />
- <br />
- <asp:ScriptManager ID="MainScriptManager" runat="server" />
- <asp:UpdatePanel ID="pnlHelloWorld" runat="server">
- <ContentTemplate>
- <div>
- <asp:Repeater ID="Repeater1" runat="server" >
- <HeaderTemplate>
- <table>
- <tr>
- <th>
- <asp:LinkButton ID="LinkButton1" runat="server" Text='Id' OnCommand="Sort_Id"
- CommandName="Id" CommandArgument="Id" CausesValidation="false"></asp:LinkButton>
- </th>
- <th>
- <asp:LinkButton ID="SortButtonImie" runat="server" Text='Imie' OnCommand="Sort_Id"
- CommandName="Imie" CommandArgument="Imie" CausesValidation="false"></asp:LinkButton>
- </th>
- <th>
- <asp:LinkButton ID="LinkButton3" runat="server" Text='Nazwisko' OnCommand="Sort_Id"
- CommandName="Nazwisko" CommandArgument="Nazwisko" CausesValidation="false"></asp:LinkButton>
- </th>
- <th>
- <asp:LinkButton ID="LinkButton4" runat="server" Text='Data Urodzenia' OnCommand="Sort_Id"
- CommandName="DataUrodzenia" CommandArgument="DataUrodzenia" CausesValidation="false"></asp:LinkButton>
- </th>
- <th>
- <asp:LinkButton ID="LinkButton5" runat="server" Text='Administrator' OnCommand="Sort_Id"
- CommandName="Administrator" CommandArgument="Administrator" CausesValidation="false"></asp:LinkButton>
- </th>
- </tr>
- </HeaderTemplate>
- <ItemTemplate>
- <tr>
- <td>
- <asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("Id") %>' OnCommand="LinkButton2_Command"
- CommandName="Edit" CommandArgument='<%# Eval("Id") %>' CausesValidation="false"></asp:LinkButton>
- </td>
- <td>
- <asp:Label ID="ImieR" runat="server" Text='<%# Eval("Imie") %>' />
- </td>
- <td>
- <asp:Label ID="NazwiskoR" runat="server" Text='<%# Eval("Nazwisko") %>' />
- </td>
- <td>
- <asp:Label ID="DataUrodzeniaR" runat="server" Text='<%# Eval("DataUrodzenia") %>' />
- </td>
- <td>
- <asp:Label ID="CommentTextBoxR" runat="server" Text='<%# Eval("Administrator") %>' />
- </td>
- </tr>
- </ItemTemplate>
- <FooterTemplate>
- </table>
- </FooterTemplate>
- </asp:Repeater>
- </div>
- <div style="margin-top: 20px;">
- <table style="width: 600px;">
- <tr>
- <td>
- <asp:LinkButton ID="lbFirst" runat="server"
- OnClick="lbFirst_Click">First</asp:LinkButton>
- </td>
- <td>
- <asp:LinkButton ID="lbPrevious" runat="server"
- OnClick="lbPrevious_Click">Previous</asp:LinkButton>
- </td>
- <td>
- <asp:DataList ID="rptPaging" runat="server"
- OnItemCommand="rptPaging_ItemCommand"
- OnItemDataBound="rptPaging_ItemDataBound"
- RepeatDirection="Horizontal">
- <ItemTemplate>
- <asp:LinkButton ID="lbPaging" runat="server"
- CommandArgument='<%# Eval("PageIndex") %>'
- CommandName="newPage"
- Text='<%# Eval("PageText") %> ' Width="20px">
- </asp:LinkButton>
- </ItemTemplate>
- </asp:DataList>
- </td>
- <td>
- <asp:LinkButton ID="lbNext" runat="server"
- OnClick="lbNext_Click">Next</asp:LinkButton>
- </td>
- <td>
- <asp:LinkButton ID="lbLast" runat="server"
- OnClick="lbLast_Click">Last</asp:LinkButton>
- </td>
- <td>
- <asp:Label ID="lblpage" runat="server" Text=""></asp:Label>
- </td>
- </tr>
- </table>
- </div>
- </ContentTemplate>
- </asp:UpdatePanel>
- <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Id], [Imie], [Nazwisko], [DataUrodzenia], [Administrator] FROM [Users]"></asp:SqlDataSource>
- <br />
- <br />
- <asp:Label ID="Label1" runat="server" Text="Imie"></asp:Label>
- <asp:TextBox ID="Imie" runat="server"></asp:TextBox>
- <br />
- <asp:Label ID="Label2" runat="server" Text="Nazwisko"></asp:Label>
- <asp:TextBox ID="Nazwisko" runat="server"></asp:TextBox>
- <br />
- <asp:Label ID="Label3" runat="server" Text="DataUrodzenia"></asp:Label>
- <asp:TextBox ID="DataUrodzenia" runat="server"></asp:TextBox>
- <br />
- <asp:Label ID="Label4" runat="server" Text="Administrator"></asp:Label>
- <asp:RadioButton ID="RadioButton1" runat="server" />
- <br />
- <asp:Button ID="Button1" runat="server" Text="Dodaj uzytkownika" OnClick="Button1_Click" />
- <br />
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace HTML_LAB5_6
- {
- public partial class Users : System.Web.UI.Page
- {
- readonly PagedDataSource _pgsource = new PagedDataSource();
- int _firstIndex, _lastIndex;
- private int _pageSize = 5;
- private int CurrentPage
- {
- get
- {
- if (ViewState["CurrentPage"] == null)
- {
- return 0;
- }
- return ((int)ViewState["CurrentPage"]);
- }
- set
- {
- ViewState["CurrentPage"] = value;
- }
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Page.IsPostBack) return;
- BindDataIntoRepeater();
- }
- // Get data from database/repository
- static DataTable GetDataFromDb()
- {
- var con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
- con.Open();
- var da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Id", con);
- var dt = new DataTable();
- da.Fill(dt);
- con.Close();
- return dt;
- }
- // Bind PagedDataSource into Repeater
- private void BindDataIntoRepeater()
- {
- var dt = GetDataFromDb();
- _pgsource.DataSource = dt.DefaultView;
- _pgsource.AllowPaging = true;
- // Number of items to be displayed in the Repeater
- _pgsource.PageSize = _pageSize;
- _pgsource.CurrentPageIndex = CurrentPage;
- // Keep the Total pages in View State
- ViewState["TotalPages"] = _pgsource.PageCount;
- // Example: "Page 1 of 10"
- lblpage.Text = "Page " + (CurrentPage + 1) + " of " + _pgsource.PageCount;
- // Enable First, Last, Previous, Next buttons
- lbPrevious.Enabled = !_pgsource.IsFirstPage;
- lbNext.Enabled = !_pgsource.IsLastPage;
- lbFirst.Enabled = !_pgsource.IsFirstPage;
- lbLast.Enabled = !_pgsource.IsLastPage;
- // Bind data into repeater
- Repeater1.DataSource = _pgsource;
- Repeater1.DataBind();
- // Call the function to do paging
- HandlePaging();
- }
- private void HandlePaging()
- {
- var dt = new DataTable();
- dt.Columns.Add("PageIndex"); //Start from 0
- dt.Columns.Add("PageText"); //Start from 1
- _firstIndex = CurrentPage - 5;
- if (CurrentPage > 5)
- _lastIndex = CurrentPage + 5;
- else
- _lastIndex = 10;
- // Check last page is greater than total page then reduced it
- // to total no. of page is last index
- if (_lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
- {
- _lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
- _firstIndex = _lastIndex - 10;
- }
- if (_firstIndex < 0)
- _firstIndex = 0;
- // Now creating page number based on above first and last page index
- for (var i = _firstIndex; i < _lastIndex; i++)
- {
- var dr = dt.NewRow();
- dr[0] = i;
- dr[1] = i + 1;
- dt.Rows.Add(dr);
- }
- rptPaging.DataSource = dt;
- rptPaging.DataBind();
- }
- protected void lbFirst_Click(object sender, EventArgs e)
- {
- CurrentPage = 0;
- BindDataIntoRepeater();
- }
- protected void lbLast_Click(object sender, EventArgs e)
- {
- CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
- BindDataIntoRepeater();
- }
- protected void lbPrevious_Click(object sender, EventArgs e)
- {
- CurrentPage -= 1;
- BindDataIntoRepeater();
- }
- protected void lbNext_Click(object sender, EventArgs e)
- {
- CurrentPage += 1;
- BindDataIntoRepeater();
- }
- protected void rptPaging_ItemCommand(object source, DataListCommandEventArgs e)
- {
- if (!e.CommandName.Equals("newPage")) return;
- CurrentPage = Convert.ToInt32(e.CommandArgument.ToString());
- BindDataIntoRepeater();
- }
- protected void rptPaging_ItemDataBound(object sender, DataListItemEventArgs e)
- {
- var lnkPage = (LinkButton)e.Item.FindControl("lbPaging");
- if (lnkPage.CommandArgument != CurrentPage.ToString()) return;
- lnkPage.Enabled = false;
- lnkPage.BackColor = Color.FromName("#00FF00");
- }
- protected void LinkButton2_Command(object sender, CommandEventArgs e)
- {
- Response.Redirect("Questions.aspx");
- }
- protected void Sort_Id(object sender, CommandEventArgs e)
- {
- var con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
- con.Open();
- var da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Id", con);
- var dt = new DataTable();
- switch (e.CommandName)
- {
- case "Id":
- if ((String)Session["Sort"] == "DESC")
- {
- da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Id desc", con);
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Id] DESC";
- Session["Sort"] = "ASC";
- }
- else
- {
- da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Id", con);
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Id]";
- Session["Sort"] = "DESC";
- }
- //------------------------------------------------------------------------------//
- da.Fill(dt);
- con.Close();
- _pgsource.DataSource = dt.DefaultView;
- _pgsource.AllowPaging = true;
- // Number of items to be displayed in the Repeater
- _pgsource.PageSize = _pageSize;
- _pgsource.CurrentPageIndex = CurrentPage;
- // Keep the Total pages in View State
- ViewState["TotalPages"] = _pgsource.PageCount;
- // Example: "Page 1 of 10"
- lblpage.Text = "Page " + (CurrentPage + 1) + " of " + _pgsource.PageCount;
- // Enable First, Last, Previous, Next buttons
- lbPrevious.Enabled = !_pgsource.IsFirstPage;
- lbNext.Enabled = !_pgsource.IsLastPage;
- lbFirst.Enabled = !_pgsource.IsFirstPage;
- lbLast.Enabled = !_pgsource.IsLastPage;
- // Bind data into repeater
- Repeater1.DataSource = _pgsource;
- Repeater1.DataBind();
- // Call the function to do paging
- HandlePaging();
- //------------------------------------------------------------------------------//
- //Repeater1.DataBind();
- break;
- case "Imie":
- if ((String)Session["Sort"] == "DESC")
- {
- da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Imie desc", con);
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Imie] DESC";
- Session["Sort"] = "ASC";
- }
- else
- {
- da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Imie", con);
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Imie]";
- Session["Sort"] = "DESC";
- }
- //------------------------------------------------------------------------------//
- da.Fill(dt);
- con.Close();
- _pgsource.DataSource = dt.DefaultView;
- _pgsource.AllowPaging = true;
- // Number of items to be displayed in the Repeater
- _pgsource.PageSize = _pageSize;
- _pgsource.CurrentPageIndex = CurrentPage;
- // Keep the Total pages in View State
- ViewState["TotalPages"] = _pgsource.PageCount;
- // Example: "Page 1 of 10"
- lblpage.Text = "Page " + (CurrentPage + 1) + " of " + _pgsource.PageCount;
- // Enable First, Last, Previous, Next buttons
- lbPrevious.Enabled = !_pgsource.IsFirstPage;
- lbNext.Enabled = !_pgsource.IsLastPage;
- lbFirst.Enabled = !_pgsource.IsFirstPage;
- lbLast.Enabled = !_pgsource.IsLastPage;
- // Bind data into repeater
- Repeater1.DataSource = _pgsource;
- Repeater1.DataBind();
- // Call the function to do paging
- HandlePaging();
- //------------------------------------------------------------------------------//
- //Repeater1.DataBind();
- break;
- case "Nazwisko":
- if ((String)Session["Sort"] == "DESC")
- {
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Nazwisko] DESC";
- Session["Sort"] = "ASC";
- }
- else
- {
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Nazwisko]";
- Session["Sort"] = "DESC";
- }
- Repeater1.DataBind();
- break;
- case "DataUrodzenia":
- if ((String)Session["Sort"] == "DESC")
- {
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [DataUrodzenia] DESC";
- Session["Sort"] = "ASC";
- }
- else
- {
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [DataUrodzenia]";
- Session["Sort"] = "DESC";
- }
- Repeater1.DataBind();
- break;
- case "Administrator":
- if ((String)Session["Sort"] == "DESC")
- {
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Administrator] DESC";
- Session["Sort"] = "ASC";
- }
- else
- {
- //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Administrator]";
- Session["Sort"] = "DESC";
- }
- Repeater1.DataBind();
- break;
- default:
- // The command name is not recognized. Display an error message.
- //Message.Text = "Command name not recogized.";
- break;
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
- con.Open();
- try
- {
- string sel = "Select count(*) from Users";
- SqlCommand scm = new SqlCommand(sel, con);
- int id = Convert.ToInt32(scm.ExecuteScalar().ToString());
- sel = "insert into Users (Id,Imie,Nazwisko,DataUrodzenia,Administrator) values (@id,@imie,@nazwisko,@dataUrodzenia,@administrator)";
- scm = new SqlCommand(sel, con);
- scm.Parameters.AddWithValue("@id", id);
- scm.Parameters.AddWithValue("@imie", Imie.Text);
- scm.Parameters.AddWithValue("@nazwisko", Nazwisko.Text);
- scm.Parameters.AddWithValue("@dataUrodzenia", DataUrodzenia.Text);
- scm.Parameters.AddWithValue("@administrator", RadioButton1.Checked);
- scm.ExecuteNonQuery();
- con.Close();
- Response.Redirect("Users.aspx");
- }
- catch (Exception ex)
- {
- Response.Write("Błąd: " + ex.ToString());
- con.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement