ASP.NET kontrolki walidacyjne

0

Witam mam na jednej stronie dwa formularze. Kiedy dodaję kontrolki walidacyjne z polem wymaganym do formularza to nie mogę wysłać ani jednego formularza nie wypełniając danych z drugiego, chociaż oba formularze są obsługiwane osobno. Jak można temu zaradzić?

0

Pierwszy krok to pokazanie jakiegoś kodu

0

Dodaj.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="Dodaj.aspx.cs" Inherits="APBD2.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style3 {
            width: 144px;
        }
    .auto-style4 {
        width: 187px;
    }
    .auto-style6 {
        width: 217px;
    }
    .auto-style7 {
        width: 222px;
    }
    .auto-style8 {
        width: 282px;
    }
    .auto-style9 {
        width: 282px;
        height: 23px;
    }
    .auto-style10 {
        width: 222px;
        height: 23px;
    }
    .auto-style11 {
        width: 217px;
        height: 23px;
    }
    .auto-style12 {
        height: 23px;
        width: 99px;
    }
    .auto-style13 {
        width: 99px;
    }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="tresc" runat="server">
<table class="auto-style1">
        <tr>
            <td class="auto-style3">
                <asp:Label ID="Label1" runat="server" Text="Podaj Imię Autora:"></asp:Label>
            </td>
            <td>
                <asp:Label ID="Label2" runat="server" Text="Podaj Nazwisko Autora:"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="auto-style3">
                <asp:TextBox ID="imieAutora" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:TextBox ID="nazwiskoAutora" runat="server" Width="166px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="auto-style3">
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Dodaj Autora" />
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="imieAutora" Display="Dynamic" ErrorMessage="Imię Autora nie może być puste"></asp:RequiredFieldValidator>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="nazwiskoAutora" ErrorMessage="Nazwisko Autora nie może być puste"></asp:RequiredFieldValidator>
            </td>
        </tr>
    </table>
    <hr />
<table class="auto-style1">
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label3" runat="server" Text="Podaj Nazwę Wydawnictwa:"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="nazwaWydawnictwa" runat="server" style="margin-left: 0px" Width="121px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Dodaj Wydawnictwo" Width="140px" />
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="nazwaWydawnictwa" ErrorMessage="Nazwa Wydawnictwa nie może być pusta"></asp:RequiredFieldValidator>
        </td>
    </tr>
</table>
<hr />
<table class="auto-style1">
    <tr>
        <td>
            <asp:Label ID="Label4" runat="server" Text="Imię:"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label5" runat="server" Text="Nazwisko"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label6" runat="server" Text="Numer Telefonu"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:TextBox ID="imiePracownika" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="imiePracownika" ErrorMessage="Wymagane"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:TextBox ID="nazwiskoPracownika" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:TextBox ID="telPracownika" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Dodaj Pracownika" />
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="nazwiskoPracownika" ErrorMessage="Nazwisko Pracownika Wymagane"></asp:RequiredFieldValidator>
        </td>
        <td>&nbsp;</td>
    </tr>
</table>
<hr />
<table class="auto-style1">
    <tr>
        <td>
            <asp:Label ID="Label7" runat="server" Text="Imię"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label8" runat="server" Text="Nazwisko"></asp:Label>
        </td>
        <td>
            <asp:Label ID="Label9" runat="server" Text="Adres"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:TextBox ID="imieCzytelnika" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="nazwiskoCzytelnika" ErrorMessage="Imię Wymagane"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:TextBox ID="nazwiskoCzytelnika" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:TextBox ID="adresCzytelnika" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Dodaj Czytelnika" />
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="nazwiskoCzytelnika" ErrorMessage="Nazwisko Wymagane"></asp:RequiredFieldValidator>
        </td>
        <td>&nbsp;</td>
    </tr>
</table>
<hr />
<table class="auto-style1">
    <tr>
        <td class="auto-style9">
            <asp:Label ID="Label10" runat="server" Text="Tytuł"></asp:Label>
        </td>
        <td class="auto-style10">
            <asp:Label ID="Label11" runat="server" Text="Autor"></asp:Label>
        </td>
        <td class="auto-style11">
            <asp:Label ID="Label12" runat="server" Text="Wydawnictwo"></asp:Label>
        </td>
        <td class="auto-style12">
            <asp:Label ID="Label13" runat="server" Text="Rok Wydania"></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="auto-style8">
            <asp:TextBox ID="tytulKsiazki" runat="server"></asp:TextBox>
        </td>
        <td class="auto-style7">
            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="AutorLista" DataTextField="nazwisko" DataValueField="idautora" Height="22px" Width="129px">
            </asp:DropDownList>
            <asp:SqlDataSource ID="AutorLista" runat="server" ConnectionString="<%$ ConnectionStrings:BibliotekaConnectionString %>" SelectCommand=" select idautora, cast(imie as nvarchar)+' '+cast(nazwisko as nvarchar) as nazwisko FROM [Autor]"></asp:SqlDataSource>
        </td>
        <td class="auto-style6">
            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="WydawnictwoLista" DataTextField="Nazwa" DataValueField="IdWydawnictwa" Width="124px">
            </asp:DropDownList>
            <asp:SqlDataSource ID="WydawnictwoLista" runat="server" ConnectionString="<%$ ConnectionStrings:BibliotekaConnectionString %>" SelectCommand="SELECT [Nazwa], [IdWydawnictwa] FROM [Wydawnictwo]"></asp:SqlDataSource>
        </td>
        <td class="auto-style13">
            <asp:TextBox ID="rokWydania" runat="server"></asp:TextBox>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style8">
            <asp:Label ID="Label14" runat="server" Text="Dziedzina:"></asp:Label>
        </td>
        <td class="auto-style7">
            <asp:TextBox ID="dziedzina" runat="server"></asp:TextBox>
        </td>
        <td class="auto-style6">&nbsp;</td>
        <td class="auto-style13">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style8">
            <asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="Dodaj Książkę" Height="26px" />
        </td>
        <td class="auto-style7">&nbsp;</td>
        <td class="auto-style6">&nbsp;</td>
        <td class="auto-style13">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
</table>
    <hr />
    <table class="auto-style1">
        <tr>
            <td>
                <asp:Label ID="Label15" runat="server" Text="Tytuł Książki"></asp:Label>
            </td>
            <td>
                <asp:Label ID="Label16" runat="server" Text="Dostępna w magazynie?"></asp:Label>
            </td>
            <td>
                <asp:Label ID="Label17" runat="server" Text="Czy wypożyczona?"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="KsiazkaLista" DataTextField="Tytul" DataValueField="IdKsiazki">
                </asp:DropDownList>
                <asp:SqlDataSource ID="KsiazkaLista" runat="server" ConnectionString="<%$ ConnectionStrings:BibliotekaConnectionString %>" SelectCommand="SELECT [IdKsiazki], [Tytul] FROM [Ksiazka]"></asp:SqlDataSource>
            </td>
            <td>
                <asp:CheckBox ID="dostepMagazyn" runat="server" />
            </td>
            <td>
                <asp:CheckBox ID="wypozyczona" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="Swórz Egzemplarz" />
            </td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>
    <hr />
    <br />
    <asp:Label ID="blad" runat="server"></asp:Label>
</asp:Content>

Dodaj.aspx.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace APBD2
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            try
            {
                using (SqlConnection con = new SqlConnection(Properties.Settings.Default.dbBiblioteka))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    con.Open();
                    cmd.CommandText = "Dodaj_autora" ;
                    
                    cmd.Parameters.Add("@nazwisko", System.Data.SqlDbType.NVarChar).Value = imieAutora.Text;
                    cmd.Parameters.Add("@imie", System.Data.SqlDbType.NVarChar).Value = nazwiskoAutora.Text;
                   // cmd.Parameters.AddWithValue
                    
                    cmd.ExecuteNonQuery();
                    blad.Text = "" + "Dodano autora: " + imieAutora.Text + " " + nazwiskoAutora.Text;
                    con.Close();
                    
                }
            }
            catch (Exception exc)
            {
                blad.Text = "" +exc;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Properties.Settings.Default.dbBiblioteka))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    con.Open();
                    cmd.CommandText = "Dodaj_wydawnictwo";

                    cmd.Parameters.Add("@nazwa", System.Data.SqlDbType.VarChar).Value = nazwaWydawnictwa.Text;
                    // cmd.Parameters.AddWithValue

                    cmd.ExecuteNonQuery();
                    blad.Text = "" + "Dodano wydawnictwo: " + nazwaWydawnictwa.Text;
                    con.Close();

                }
            }
            catch (Exception exc)
            {
                blad.Text = "" + exc;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Properties.Settings.Default.dbBiblioteka))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    con.Open();
                    cmd.CommandText = "Dodaj_pracownika";

                    cmd.Parameters.Add("@Nazwisko", System.Data.SqlDbType.NVarChar).Value = imiePracownika.Text;
                    cmd.Parameters.Add("@Imie", System.Data.SqlDbType.NVarChar).Value = nazwiskoPracownika.Text;
                    cmd.Parameters.Add("@Nnumer", System.Data.SqlDbType.NVarChar).Value = telPracownika.Text;
                    // cmd.Parameters.AddWithValue

                    cmd.ExecuteNonQuery();
                    blad.Text = "" + "Dodano pracownika: " + imiePracownika.Text + " " + nazwiskoPracownika.Text;
                    con.Close();

                }
            }
            catch (Exception exc)
            {
                blad.Text = "" + exc;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Properties.Settings.Default.dbBiblioteka))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "Dodaj_czytelnika";
                    cmd.Parameters.Add("@Nazwisko", System.Data.SqlDbType.NVarChar).Value = nazwiskoCzytelnika.Text;
                    cmd.Parameters.Add("@Imie", System.Data.SqlDbType.VarChar).Value = imieCzytelnika.Text;
                    cmd.Parameters.Add("@Adres", System.Data.SqlDbType.VarChar).Value = adresCzytelnika.Text;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    blad.Text = "Dodano czytelnika: " + imieCzytelnika.Text + " " + nazwiskoCzytelnika.Text;
                    con.Close();
                }
            }
            catch (Exception exc)
            {
                blad.Text = "" + exc;
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Properties.Settings.Default.dbBiblioteka))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "Dodaj_Ksiazke";
                    cmd.Parameters.Add("@Tytul", System.Data.SqlDbType.NVarChar).Value = tytulKsiazki.Text;
                    cmd.Parameters.Add("@Autor", System.Data.SqlDbType.Int).Value = DropDownList1.Text;
                    cmd.Parameters.Add("@Wydawnictwo", System.Data.SqlDbType.Int).Value = DropDownList2.Text;
                    cmd.Parameters.Add("@Rok", System.Data.SqlDbType.Int).Value = rokWydania.Text;
                    cmd.Parameters.Add("@Dziedzina", System.Data.SqlDbType.VarChar).Value = dziedzina.Text;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    blad.Text = "Dodano Książkę: " + tytulKsiazki.Text;
                    con.Close();
                }
            }
            catch (Exception exc)
            {
                blad.Text = "" + exc;
            }
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(Properties.Settings.Default.dbBiblioteka))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "Stworz_egzemplarz";
                    cmd.Parameters.Add("@Ksiazka", System.Data.SqlDbType.Int).Value = DropDownList3.Text;
                    cmd.Parameters.Add("@Dostepnosc", System.Data.SqlDbType.Bit).Value = dostepMagazyn.Checked;
                    cmd.Parameters.Add("@Wypozyczenie", System.Data.SqlDbType.Bit).Value = wypozyczona.Checked;
                    
                    con.Open();
                    cmd.ExecuteNonQuery();
                    blad.Text = "Dodano nowy Egzemplarz";
                    con.Close();
                }
            }
            catch (Exception exc)
            {
                blad.Text = "" + exc;
            }
        }
    }
}
0

Poczytaj o ValidationGroup.

1 użytkowników online, w tym zalogowanych: 0, gości: 1