2012-07-24 26 views
0

我是一名新的ASP.NET开发人员,我必须创建一个测验引擎应用程序。我正在使用ListView在特定测验下插入问题。如何在此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> 

那么怎么办呢?

回答

0

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

asp.net-tutorials

+0

我知道,但它适合在ListView内? – 2012-07-24 08:33:45

+2

配合,只需将FileUpload控件插入到ListView的ItemTemplate中即可。就这样。 – Abhijeetchindhe 2012-07-24 08:37:28

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)" 

这是它:)

+0

感谢您的帮助。那么SqlDataSource中的查询呢? – 2012-07-24 09:16:54

+0

我修改答案;) – HAJJAJ 2012-07-24 09:26:08

+0

有些问题没有图像。除此之外,我可以用两个或三个问题的图像。所以我认为如果我在另一张桌子上有它会很好。 – 2012-07-24 09:34:55

相关问题