Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace aspnet_administartion
- {
- public partial class _Default : Page
- {
- DataClasses1DataContext db = new DataClasses1DataContext();
- bool flagEmptyRow = false;//vlajka pro zobrazeni gridview - když je prázdne
- protected void Page_Load(object sender, EventArgs e)
- {
- //vyresetovaní flajky
- flagEmptyRow = false;
- }
- protected void PageRender(object sender, EventArgs e)
- {
- if (flagEmptyRow)
- {
- // skrytí prazndeho řádku
- GridView1.Rows[0].Visible = false;
- }
- }
- /// <summary>
- /// nastaveni filtru
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void LinqSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
- {
- //switch pro nastaveni filtru
- switch (SetFilter.Value)
- {
- case "name":
- e.Result = db.Restaurants.Where(p => p.Name.ToLower().Contains(searchString.Text.ToLower()));
- break;
- case "phone":
- e.Result = db.Restaurants.Where(p => p.Phone.ToLower().Contains(searchString.Text.ToLower()));
- break;
- case "mail":
- e.Result = db.Restaurants.Where(p => p.Email.ToLower().Contains(searchString.Text.ToLower()));
- break;
- default:
- if (db.Restaurants.Count() == 0)//pokud je gridview prazdny a neni nastavena filtrace vytvori falseny řádek
- {
- e.Result = new Restaurant();
- flagEmptyRow = true;//nastaveni vlajky pro schovani falesneho radku
- }
- else
- e.Result = db.Restaurants;
- break;
- }
- }
- protected void LinqSource_Selected(object sender, LinqDataSourceStatusEventArgs e)
- {
- }
- /// <summary>
- /// zruseni pridani nove restaurace
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void CancelNew_Click(object sender, EventArgs e)
- {
- GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
- //vyčištění polí
- ((TxtBox)row.FindControl("NewName")).Value = "";
- ((TxtBox)row.FindControl("NewPhone")).Value = "";
- ((TxtBox)row.FindControl("NewEmail")).Value = "";
- }
- /// <summary>
- /// ulozeni nove restaurace
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void SaveNew_Click(object sender, EventArgs e)
- {
- // This is the crux -
- GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
- Restaurant r = new Restaurant();
- //nalezeni ovladacich prvků
- TxtBox txtName = (TxtBox)row.FindControl("NewName");
- TxtBox txtPhone = (TxtBox)row.FindControl("NewPhone");
- TxtBox txtMail = (TxtBox)row.FindControl("NewEmail");
- //serverova validace
- if (db.Restaurants.Any(p => p.Name == txtName.Value))
- {
- errorBox.Controls.Add(new LiteralControl("Jméno musí být 3-50 znaků dlouhé"));
- return;
- }
- //serverova validace
- if (Regex.IsMatch(txtName.Value, @".{3,50}"))
- {
- errorBox.Controls.Add(new LiteralControl("header musí být 3-30 znaků dlouhý"));
- return;
- }
- r.Name = txtName.Value;
- //serverova validace
- if (Regex.IsMatch(txtPhone.Value, @"^[0-9]{3} [0-9]{3} [0-9]{3}$"))
- {
- errorBox.Controls.Add(new LiteralControl("Zadejte telefon ve formátu 123 456 789"));
- return;
- }
- r.Phone = txtPhone.Value;
- //serverova validace
- if (Regex.IsMatch(txtMail.Value, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"))
- {
- errorBox.Controls.Add(new LiteralControl("Zadejte validní email"));
- return;
- }
- r.Email = txtMail.Value;
- db.Restaurants.InsertOnSubmit(r);
- db.SubmitChanges();
- GridView1.DataBind();
- }
- /// <summary>
- /// zacatek editace
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Edit_Click(object sender, EventArgs e)
- {
- //spusteni editačního modu
- GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
- GridView1.EditIndex = row.RowIndex;
- }
- /// <summary>
- /// konec editace
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void CancelEdit_Click(object sender, EventArgs e)
- {
- // neni receno jestli se maji vynulovat data - tak je nechávám být a vypinam validacni mod
- GridView1.EditIndex = -1;
- }
- /// <summary>
- /// ukladani članků
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void SaveArticle_Click(object sender, EventArgs e)
- {
- GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
- Label lblname = (Label)row.FindControl("lblName");
- Button btnNew = (Button)row.FindControl("SetNewArticle");
- ArticlePanel arrPanel = (ArticlePanel)row.FindControl("articlePanel");
- arrPanel.RestauraceId = lblname.Text; //ziskani restaurant ID
- arrPanel.NewArticle = !btnNew.Visible; // bool pro zjisteni - jestli se jedna o novy clanek nebo o upravu
- if (arrPanel.SaveEvent())//pokud probehlo ulozeni - vracim gridview do pivodni podoby
- {
- SetVisibile(row, "EditingPanel", false);
- SetVisibile(row, "ClosePanel", false);
- SetVisibile(row, "Edit", true);
- SetVisibile(row, "SetNewArticle", false);
- SetVisibile(row, "ShowFrom", true);
- }
- }
- /// <summary>
- /// zobrazeni formulare članku
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void ShowFrom_Click(object sender, EventArgs e)
- {
- GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
- Label lblname = (Label)row.FindControl("lblName");
- ArticlePanel arrPanel = (ArticlePanel)row.FindControl("articlePanel");
- arrPanel.RestauraceId = lblname.Text;
- arrPanel.FillDropDownList();
- SetVisibile(row, "EditingPanel", true);
- SetVisibile(row, "ClosePanel", true);
- SetVisibile(row, "Edit", false);
- SetVisibile(row, "SetNewArticle", true);
- SetVisibile(row, "ShowFrom", false);
- }
- /// <summary>
- /// ukocení spravy članku
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void ClosePanel_Click(object sender, EventArgs e)
- {
- GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
- ArticlePanel arrPanel = (ArticlePanel)row.FindControl("articlePanel");
- arrPanel.NewArticle = false;
- arrPanel.ClearTxt();
- //zaviram celou editaci - nevim zda-li jsem spravne pochopil
- SetVisibile(row, "EditingPanel", false);
- SetVisibile(row, "ClosePanel", false);
- SetVisibile(row, "Edit", true);
- SetVisibile(row, "SetNewArticle", false);
- SetVisibile(row, "ShowFrom", true);
- }
- /// <summary>
- /// nastaveni modu novy članek
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void SetNewArticle_Click(object sender, EventArgs e)
- {
- GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
- ArticlePanel arrPanel = (ArticlePanel)row.FindControl("articlePanel");
- arrPanel.NewArticle = true;// tento parametr nastavi pohled na pridavani novych članků
- SetVisibile(row, "SetNewArticle", false);
- }
- /// <summary>
- /// funkce pro shovavani a zobrazovani ovladacivh prvku v zaznamu (Row)
- /// </summary>
- /// <param name="row"> radek gridu</param>
- /// <param name="nameControl">nazev prvku</param>
- /// <param name="visible">jaky stav ma byt nastaven</param>
- protected void SetVisibile(GridViewRow row, string nameControl, bool visible)
- {
- Control control = row.FindControl(nameControl);
- control.Visible = visible;
- }
- /// <summary>
- /// udalost nastaveni filtru
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Filter_Click(object sender, EventArgs e)
- {
- SetFilter.Value = Type.SelectedValue;
- GridView1.DataBind();
- }
- /// <summary>
- /// udalost zrušení filtru
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void ShowAll_Click(object sender, EventArgs e)
- {
- SetFilter.Value = "";
- GridView1.DataBind();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement