All pastes #182872 Raw Edit

Unnamed

public text v1 · immutable
#182872 ·published 2006-09-26 12:46 UTC
rendered paste body
// aspx page

<body>
    <form id="form1" runat="server">
    <div>
         <asp:FormView ID="FormView1" DefaultMode="Insert" runat="server" 
         DataKeyNames="ID" 
         OnItemInserted="FormView1_ItemInserted" 
         OnItemUpdated="FormView1_ItemUpdated" 
         OnItemInserting="FormView1_ItemInserting" 
         OnItemUpdating="FormView1_ItemUpdating" 
         Width="320px" 
         >
            <EditItemTemplate>
               <table>
                    <tr>
                        <td>
                            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                            <a href="javascript:void(0);" onclick="self.close();">Anuleaza</a>
                        </td>
                    </tr>
                    <tr>
                        <td>Nume:</td>
                        <td>
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' MaxLength="255"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvNameTextBox" runat="server" ControlToValidate="NameTextBox" ErrorMessage="'Nume' este obligatoriu">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>Arhivat:</td>
                        <td>
                            <asp:CheckBox ID="ArchivedCheckBox" runat="server" Checked='<%# Bind("Archived") %>' />
                        </td>
                    </tr>
                </table>
            </EditItemTemplate>
            
            <InsertItemTemplate>
                <table>
                    <tr>
                        <td>
                            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Salveaza"></asp:LinkButton>
                            <a href="javascript:void(0);" onclick="self.close();">Anuleaza</a>
                        </td>
                    </tr>
                    <tr>
                        <td>Nume:</td>
                        <td>
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' MaxLength="255"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvNameTextBox" runat="server" ControlToValidate="NameTextBox" ErrorMessage="'Nume' este obligatoriu">*</asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>Arhivat:</td>
                        <td>
                            <asp:CheckBox ID="ArchivedCheckBox" runat="server" Checked='<%# Bind("Archived") %>' />
                        </td>
                    </tr>
                </table>
            </InsertItemTemplate>
        </asp:FormView>
        
        
        
    </div>
    <asp:ValidationSummary ID="vs1" runat="server" ShowMessageBox="true" ShowSummary="false" HeaderText="Verificati datele introduse:" />
    </form>
</body>







// aspx.cs page:

public partial class CarBodyDetails : BasePage
	{
		private int _id = 0;
		public int Id
		{
			get { return _id; }
			set { _id = value; }
		}

		private const string CarBodyDuplicateErrorMessage = "Aceasta caroserie exista in baza de date.";

		protected void Page_Load(object sender, EventArgs e)
		{
			SetFormViewMode();
			BindFormView(this.Id);
		}

		protected void SetFormViewMode()
		{
			this.FormView1.DefaultMode = FormViewMode.Insert;

			int carBodyID = 0;
			if (Request.QueryString["id"] != null)
			{
				try { carBodyID = Int32.Parse(Request.QueryString["id"]); }
				catch { }
			}
			if (carBodyID > 0)
			{
				this.FormView1.DefaultMode = FormViewMode.Edit;
				this.Id = carBodyID;
				
			}
		}
		private void BindFormView(int id)
		{
			if (this.FormView1.CurrentMode == FormViewMode.Edit)
			{
				CarBody carBody = new CarBody(id);

				List<CarBody> list = new List<CarBody>(1); 
				list.Add(carBody); 
				this.FormView1.DataSource = list;
				this.FormView1.DataBind();

				/*
				((TextBox)FormView1.FindControl("NameTextBox")).Text = carBody.Name;
				((CheckBox)this.FormView1.FindControl("ArchivedCheckBox")).Checked = carBody.Archived;			
				*/
			}
		}



		protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
		{
			if (Page.IsValid)
			{
				string name = e.Values["Name"].ToString();
				if (CarBody.Exists(name))
				{
					// cancel insert operation
					e.Cancel = true;
					// display error message
					this.ShowMessage(CarBodyDuplicateErrorMessage);
				}
				else
				{
					// insert item
				}
			}
		}

		protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
		{
			if (Page.IsValid)
			{
				string name = ((TextBox)FormView1.FindControl("NameTextBox")).Text;
				// int carBodyId = int.Parse(Request.QueryString["id"]);
				if (CarBody.Exists(name, Id))
				{
					// cancel update operation
					e.Cancel = true;
					this.ShowMessage(CarBodyDuplicateErrorMessage);
				}
				else
				{
					// update item
					bool archived = ((CheckBox)this.FormView1.FindControl("ArchivedCheckBox")).Checked;
					CarBody .Update(Id, name, archived);
					this.RefreshParentWindow();
					this.ClosePopup();
				}
			}
		}

		protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
		{
			this.RefreshParentWindow();
			this.ClosePopup();
		}

		protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
		{
			this.RefreshParentWindow();
			this.ClosePopup();
		}

	
	}