0

我是一名新的 ASP.NET 开发人员,我必须创建一个测验引擎应用程序。我正在使用 ListView 在特定测验下插入问题。

我有以下数据库设计:

QuizContent Table: ID, QuizID, QuestionID, AnswerID
Quiz Table: QuizID, Title, Description
Question Table: QuestionID, Question, QuestionOrder, AnswerExplanation
Answers Table: AnswerID, Answer
QuestionImage: ID, QuestionID, URL

我现在想要的是让管理员能够将图像嵌入到这些问题中的任何一个。我该怎么做,因为我在数据库中有两个不同的表。如上面的设计所示,一个用于问题,一个用于图像。

我知道我不应该发布太多的代码或信息,但我必须这样做才能明确:

<asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2" 
            DataKeyNames="QuestionID" InsertItemPosition="LastItem">

            <EditItemTemplate>

                <tr style="">
                    <td>
                        <asp:ImageButton ID="UpdateButton" ImageUrl="Images/icons/update24.png" ToolTip="Update" runat="server" CommandName="Update" />

                        <asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/cancel324.png" ToolTip="Cancel" runat="server" CommandName="Cancel" />
                    </td>
                    <%--<td>
                        <asp:Label ID="QuestionIDLabel1" runat="server" 
                            Text='<%# Eval("QuestionID") %>' />
                    </td>--%>
                    <td>
                        <asp:TextBox ID="QuestionTextBox" runat="server" 
                            Text='<%# Bind("Question") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="QuestionOrderTextBox" runat="server" 
                            Text='<%# Bind("QuestionOrder") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="AnswerExplanationTextBox" runat="server" 
                            Text='<%# Bind("AnswerExplanation") %>' />
                    </td>
                </tr>
            </EditItemTemplate>

            <EmptyDataTemplate>
                <table runat="server" 
                    style="">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>

            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:ImageButton ID="InsertButton" ImageUrl="Images/icons/add24.png" ToolTip="Add" runat="server" CommandName="Insert" />

                        <asp:ImageButton ID="CancelButton" ImageUrl="Images/icons/clear24.png" ToolTip="Cancel"  runat="server" CommandName="Cancel" />
                    </td>
                    <%--<td>
                        &nbsp;</td>--%>
                    <td>
                        <asp:TextBox ID="QuestionTextBox" runat="server" 
                            Text='<%# Bind("Question") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="QuestionOrderTextBox" runat="server" 
                            Text='<%# Bind("QuestionOrder") %>'/>
                    </td>
                    <td>
                        <asp:TextBox ID="AnswerExplanationTextBox" runat="server" 
                            Text='<%# Bind("AnswerExplanation") %>' />
                    </td>
                </tr>

            </InsertItemTemplate>

            <ItemTemplate>
                <tr style="">
                    <td>
                        <asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" ToolTip="delete" runat="server" CommandName="Delete" />

                        <asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit24.png" ToolTip="Edit" runat="server" CommandName="Edit" />

                        <asp:ImageButton ID="SelectButton" ImageUrl="Images/icons/select.png" ToolTip="Select" runat="server" CommandName="Select" />
                            <%--<asp:Button ID="SelectButton" runat="server" CommandName="Select" Text="Select" />--%>
                    </td>
                    <td>
                        <asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
                    </td>
                    <td>
                        <asp:Label ID="QuestionOrderLabel" runat="server" 
                            Text='<%# Eval("QuestionOrder") %>' />
                    </td>
                    <td>
                        <asp:Label ID="AnswerExplanationLabel" runat="server" 
                            Text='<%# Eval("AnswerExplanation") %>' />
                    </td>
                </tr>
            </ItemTemplate>

            <LayoutTemplate>
                <div ><table id="thetable" width="97%" cellpadding="0px" cellspacing="0px" style="margin:0px 0px 0px 0px; border:2px solid #003366; font-size:13px; font-weight:bold;">
                        <thead>
                            <tr style="background-color:#C6D7B5;">
                                <th style="border-bottom:2px solid #003366; ">...</th>
                                <th style="border-bottom:2px solid #003366; ">Question</th>
                                <th style="border-bottom:2px solid #003366; ">Question Order</th>
                                <th style="border-bottom:2px solid #003366; ">Answer Explanation</th>
                            </tr>
                       </thead>
                       <tbody><tr id="itemPlaceholder" runat="server"></tr></tbody>
                    </table></div>     
            </LayoutTemplate>
            <SelectedItemTemplate>
                <tr style="">
                    <td>
                        <asp:ImageButton ID="DeleteButton" ImageUrl="Images/icons/delete24.png" ToolTip="Delete" runat="server" CommandName="Delete" />

                        <asp:ImageButton ID="EditButton" ImageUrl="Images/icons/edit24.png" ToolTip="Edit" runat="server" CommandName="Edit" />
                    </td>
                    <td>
                        <asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
                    </td>
                    <td>
                        <asp:Label ID="QuestionOrderLabel" runat="server" 
                            Text='<%# Eval("QuestionOrder") %>' />
                    </td>
                    <td>
                        <asp:Label ID="AnswerExplanationLabel" runat="server" 
                            Text='<%# Eval("AnswerExplanation") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>
        </div>

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:QuizSysDBConnectionString %>" 
            SelectCommand="SELECT DISTINCT [Question].* FROM [Question] INNER JOIN [QuizContent] ON [QuizContent].[QuizID] = @QuizID"

            DeleteCommand="DELETE FROM [Question] WHERE [QuestionID] = @QuestionID" 
            InsertCommand="INSERT INTO [Question] ([Question], [QuestionOrder], [AnswerExplanation]) VALUES (@Question, @QuestionOrder, @AnswerExplanation)" 


            UpdateCommand="UPDATE [Question] SET [Question] = @Question, [QuestionOrder] = @QuestionOrder, [AnswerExplanation] = @AnswerExplanation WHERE [QuestionID] = @QuestionID">

                <DeleteParameters>
                    <asp:Parameter Name="QuestionID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="Question" Type="String" />
                    <asp:Parameter Name="QuestionOrder" Type="Int32" />
                    <asp:Parameter Name="AnswerExplanation" Type="String" />
                    <asp:ControlParameter ControlID="ListView1" Name="QuizID" PropertyName="SelectedValue" Type="Int32" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Question" Type="String" />
                    <asp:Parameter Name="QuestionOrder" Type="Int32" />
                    <asp:Parameter Name="AnswerExplanation" Type="String" />
                </UpdateParameters>

            <SelectParameters>
                <asp:ControlParameter ControlID="ListView1" Name="QuizID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

那么该怎么做呢?

4

2 回答 2

0

使用<asp:FileUpload>控制。有关控制的帮助,请查看此站点。

asp.net 教程

于 2012-07-24T08:30:49.010 回答
0

你需要添加:

<asp:FileUpload ID="PictureIDUploader" runat="server" />

并在代码后面的页面中添加此

 protected void ListView2_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    try
    {
        switch (e.CommandName)
        {

            case "Insert":
string Imagepath = "~/UploadedImages/";
                FileUpload fu2 = (FileUpload)ListView2.InsertItem.FindControl("PictureIDUploader");
                if (fu2.HasFile)
                {
                    string exten = Path.GetExtension(Server.MapPath(fu2.FileName));
                    string Filename2 = DateTime.Now.ToOADate().ToString() + exten;
                    Session["TheImage"] = Filename2;
                    string FileExtension = Path.GetExtension(Filename2).ToString();
                    string filepath = Path.Combine(Server.MapPath(Imagepath)) + Session["TheImage"].ToString();
                    fu2.SaveAs(filepath);
                }
                else
                {
                    Session["TheImage"] = "NoImage.png";
                }
                //Hiding the insert template
                break;
        }
    }
    catch (Exception ex)
    {
        ErrorLabel.Text = ex.Message;
    }
}

现在您所需要的就是将其添加到您的数据源中:

<asp:SessionParameter Name="TheImage" Type="String" SessionField="TheImage" />

您不需要另一个用于图像“QuestionImage”的表,您只需要在“问题”表上添加一个新列“TheImage”并将插入查询修改为:

InsertCommand="INSERT INTO [Question] ([Question], [QuestionOrder], [AnswerExplanation],TheImage) VALUES (@Question, @QuestionOrder, @AnswerExplanation,@TheImage)"

这就对了 :)

于 2012-07-24T08:52:42.593 回答