sábado, 19 de fevereiro de 2011

Questionário Dinâmico em ASP.NET - Parte V

Na área do aluno temos a seguinte página aluno.aspx após o login:



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="aluno.aspx.vb" Inherits="aluno" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Aluno - Home</title><style type="text/css">
    .table1
        {
            width: 680px;
            border: 1px solid #999999;
            background-color: #FFFFFF;
            font: Verdana;
            font-size: x-small;
        }
        .style2
        {
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <table align="center" class="table1" style="font-family: Verdana; font-size: x-small;">
        <tr>
            <td style="text-align: left">&nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Button ID="Button2" runat="server" Text="Questionário" PostBackUrl="~/questionario_teste.aspx" />
            </td>
        </tr>
        <tr>
            <td style="text-align: center">&nbsp;</td>
        </tr>
        <tr>
            <td style="text-align: center">
                  <asp:Button ID="Button5" runat="server" Text="Home" />
                  &nbsp;
<asp:Button ID="Button6" runat="server" Text="Sair" PostBackUrl="Default.aspx" />
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>


Ao clicar em questionário e selecionar o tema, as questões serão exibidas, basta selecionar a alternativa correta para cada questão e clicar no botão responder.



Será exibida a mensagem “Questionário respondido com sucesso” conforme a imagem abaixo:



Segue o código da página questionario.aspx:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="questionario.aspx.vb" Inherits="questionario" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Aluno - Questionário</title>
<style type="text/css">
.table1
        {
            width: 680px;
            border: 1px solid #999999;
            background-color: #FFFFFF;
            font: Verdana;
            font-size: x-small;
        }
        .style3
        {
            text-align: left;
        }
        </style>
</head>
<body style="text-align: center">
    <form id="form1" runat="server">
    <div>
   
    <table align="center" class="table1" style="font-family: Verdana; font-size: x-small;">
        <tr>
            <td style="text-align: left">&nbsp;</td>
        </tr>
        <tr>
            <td style="text-align: center">
                <asp:Button ID="Button5" runat="server" PostBackUrl="~/aluno.aspx"
                    Text="Home" />
            </td>
        </tr>
        <tr>
            <td style="text-align: center">&nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                Selecione o tema:                
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="AccessDataSource1" DataTextField="tema" DataValueField="id" AutoPostBack="True">
                </asp:DropDownList>
                <asp:AccessDataSource ID="AccessDataSource1" runat="server"
                    DataFile="~/projeto.mdb" SelectCommand="SELECT * FROM [tema_questao]">
                </asp:AccessDataSource>
            </td>
        </tr>
        <tr>
            <td class="style3">&nbsp;</td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Repeater ID="Repeater1" runat="server" DataSourceID="AccessDataSource2">
                    <ItemTemplate>
                    <table>
                    <tr>
            <td class="style3" colspan="2"></td>
        </tr>
        <tr>
            <td colspan="2"></td>
        </tr>
  <tr>
            <td colspan="2">
                <asp:Label ID="lbl_numquestao" runat="server" Text='<%#Eval("num_questao")%>'>
                </asp:Label> -
                <asp:Label ID="lbl_questao" runat="server" Text='<%#Eval("questao")%>'></asp:Label>
                </td>
        </tr>
        <tr>
            <td rowspan="4">
                <asp:RadioButtonList ID="rbl_resposta" runat="server" TextAlign="Left" Height="100%">
                    <asp:ListItem>A</asp:ListItem>
                    <asp:ListItem>B</asp:ListItem>
                    <asp:ListItem>C</asp:ListItem>
                    <asp:ListItem>D</asp:ListItem>
                </asp:RadioButtonList>
            </td>
            <td>
                <asp:Label ID="Label4" runat="server" Text='<%#Eval("alternativa1")%>'></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label5" runat="server" Text='<%#Eval("alternativa2")%>'></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label6" runat="server" Text='<%#Eval("alternativa3")%>'></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label7" runat="server" Text='<%#Eval("alternativa4")%>'></asp:Label>
            </td>
        </tr>
                    </table>
                </ItemTemplate>
                </asp:Repeater>
                <asp:AccessDataSource ID="AccessDataSource2" runat="server"
                    DataFile="~/projeto.mdb"
                    SelectCommand="SELECT [id_tema], [num_questao], [questao], [alternativa1], [alternativa2], [alternativa3], [alternativa4] FROM [questoes] WHERE ([id_tema] = ?) order by num_questao">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="DropDownList1" Name="id_tema"
                            PropertyName="SelectedValue" Type="String" />
                    </SelectParameters>
                </asp:AccessDataSource>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Button ID="Button6" runat="server" Text="Responder" />
            </td>
        </tr>
        <tr>
            <td class="style3">&nbsp;</td>
        </tr>
       
    </table>
</div>
    </form>
</body>
</html>

Na página questionario.aspx.vb inserir:

Imports System.Data.OleDb
Imports System.Data

Partial Class questionario
    Inherits System.Web.UI.Page

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click

        Dim conexao As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("projeto.mdb"))
        Dim cmd As New OleDbCommand

        Session("tema") = DropDownList1.SelectedValue

        conexao.Open()
        cmd.Connection = conexao

        Dim item As RepeaterItem
        For Each item In Repeater1.Items
            Dim nquest As String = CType(item.FindControl("lbl_numquestao"), Label).Text
            Dim resp As String = CType(item.FindControl("rbl_resposta"), RadioButtonList).SelectedValue
            cmd.CommandText = "Insert into questionario_respondido(login,id_tema,questao,resposta)values('" & Session("login") & "', '" & Session("tema") & "', '" & nquest & "', '" & resp & "')"

            cmd.ExecuteNonQuery()
        Next item

        Dim texto As String
        texto = "Questionário respondido com sucesso!"
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "Error", "alert('" & texto & "'); window.location.href = 'aluno.aspx';", True)

    End Sub
End Class

Espero que tenham gostado e comentem!
Finalmente a última parte deste post, obrigada a todos que acompanharam pacientemente!