2012-06-14 98 views
0

我有一個asp.net ListView,我希望當我點擊選擇鏈接按鈕來選擇行並將數據的id傳遞給detailsview。它傳遞除最後一個記錄以外的所有記錄的id。任何原因爲什麼?Listview無法選擇最後一行

<asp:ListView ID="ListView1" runat="server" DataKeyNames="AnswerID" OnSelectedIndexChanging="ListView1_SelectedIndexChanging"> 
    <EmptyDataTemplate> 
     <table runat="server" style=""> 
      <tr> 
       <td> 
        No data was returned. 
       </td> 
      </tr> 
     </table> 
    </EmptyDataTemplate> 
    <ItemTemplate> 
     <tr style=""> 
      <td> 
       <asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" /> 
      </td> 
      <td> 
       <asp:Label ID="Label1" runat="server" Text='<%# Eval("AnswerID") %>'></asp:Label> 
      </td> 
      <td> 
       <asp:Label ID="Question" runat="server" Text='<%# Eval("QuestionText") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="UserAnswer" runat="server" Text='<%# Eval("UserAnswerTxt") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="CorrectAns" runat="server" Text='<%# Eval("CorrectAnswerTxt") %>' /> 
      </td> 
      <td> 
       <asp:Image ID="Image1" ImageUrl='<%# Eval("ImagePath") %>' Width="48" Height="48" 
        runat="server" /> 
      </td> 
     </tr> 
    </ItemTemplate> 
    <LayoutTemplate> 
     <table runat="server"> 
      <tr runat="server"> 
       <td runat="server"> 
        <table id="itemPlaceholderContainer" runat="server" border="0" style=""> 
         <tr runat="server" style=""> 
          <th runat="server"> 
          </th> 
          <th runat="server"> 
           AnswerID 
          </th> 
          <th runat="server"> 
           QuestionText 
          </th> 
          <th runat="server"> 
           UserAnswerTxt 
          </th> 
          <th runat="server"> 
           CorrectAnswerTxt 
          </th> 
          <th runat="server"> 

          </th> 
          <th runat="server"> 

          </th> 
         </tr> 
         <tr id="itemPlaceholder" runat="server"> 
         </tr> 
        </table> 
       </td> 
      </tr> 
      <tr runat="server"> 
       <td runat="server" style=""> 
       </td> 
      </tr> 
     </table> 
    </LayoutTemplate> 
    <SelectedItemTemplate> 
     <tr style=""> 
      <td> 
       <asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" /> 
      </td> 
      <td> 
       <asp:Label ID="Label1" runat="server" Text='<%# Eval("AnswerID") %>'></asp:Label> 
      </td> 
      <td> 
       <asp:Label ID="Question" runat="server" Text='<%# Eval("QuestionText") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="UserAnswer" runat="server" Text='<%# Eval("UserAnswerTxt") %>' /> 
      </td> 
      <td> 
       <asp:Label ID="CorrectAns" runat="server" Text='<%# Eval("CorrectAnswerTxt") %>' /> 
      </td> 
      <td> 
       <asp:Image ID="Image1" ImageUrl='<%# Eval("ImagePath") %>' Width="48" Height="48" 
        runat="server" /> 
      </td> 
     </tr> 
    </SelectedItemTemplate> 
</asp:ListView> 
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" 
    DataSourceID="SqlDataSource1"> 
    <Fields> 
     <asp:BoundField DataField="AnswerID" HeaderText="AnswerID" SortExpression="AnswerID" /> 
     <asp:BoundField DataField="AnswerExplanation" HeaderText="AnswerExplanation" SortExpression="AnswerExplanation" /> 
     <asp:BoundField DataField="SuggestionText" HeaderText="SuggestionText" SortExpression="SuggestionText" /> 
     <asp:BoundField DataField="LessonID" HeaderText="LessonID" SortExpression="LessonID" /> 
    </Fields> 
</asp:DetailsView> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT [ErDiagID], [AnswerID], [LessonID], [AnswerExplanation], [SuggestionText] FROM [ErrorDiagnosis] WHERE ([AnswerID] = @AnswerID)"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="ListView1" Name="AnswerID" PropertyName="SelectedValue" 
      Type="Int32" /> 
    </SelectParameters> 
</asp:SqlDataSource> 
protected void Page_Load(object sender, EventArgs e) 
{ 
    Panel p = (Panel)Master.FindControl("Panel1"); 

    if (p != null) 
    { 
     p.Visible = false; 
    } 

    ArrayList al = (ArrayList)Session["AnswerList"]; 

    if (!Page.IsPostBack) 
    { 
     ListView1.DataBind(); 

    } 

    if (al == null) 
    { 
     Response.Redirect("~/Default.aspx"); 
    } 

    SqlConnection connection = null; 
    try 
    { 
     ListView1.DataSource = al; 
     //ListView1.SelectedIndex = ListView1.Items.Count - 1; 
     ListView1.DataBind(); 
     //ListView1.SelectedIndex = ListView1.Items.Count - 1; 
     ListView1.SelectedIndex = -1; 

     if (IsPostBack == false) 
     { 
      double questions = al.Count; 
      double correct = 0.0; 

      for (int i = 0; i < al.Count; i++) 
      { 
       Answer a = (Answer)al[i]; 

       if (a.Result == Answer.ResultValue.Correct) 
       { 
        correct++; 
       } 

      } 

      //foreach (ListViewItem item in resultGrid.Items) 
      //{ 
      // Label cor = (Label)item.FindControl("CorrectAns"); 
      // Label useran = (Label)item.FindControl("UserAnswer"); 

      // if (cor.Text != useran.Text) 
      // { 
      //  useran.Font.Bold = true; 
      // } 
      //} 

      double score = (correct/questions) * 100; 

      Scorelbl.Text = string.Format("{0:0.##}", score) + "%"; 
      if (score >= 80 && score < 100) 
      { 
       resultext.Text = "Μπράβο " + Profile.UserName + "!!!"; 

      } 

      connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
      connection.Open(); 

      SqlDataSource userQuizDataSource = new SqlDataSource(); 
      userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); 
      userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [UserName]) VALUES (@QuizID, GETDATE(), @Score, @UserName)"; 
      userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString()); 
      userQuizDataSource.InsertParameters.Add("Score", score.ToString()); 
      userQuizDataSource.InsertParameters.Add("UserName", User.Identity.Name); 

      int rowsAffected = userQuizDataSource.Insert(); 
      connection.Close(); 

      if (rowsAffected == 0) 
      { 

       errorLabel.Text = ""; 
      } 

     } 
    } 
    catch (Exception exp) 
    { 
     //throw new Exception(exp.ToString(), exp); 
     Response.Redirect("~/Default.aspx"); 
    } 

} 
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e) 
{ 
    ListView1.SelectedIndex = e.NewSelectedIndex; 
    string id = ListView1.SelectedDataKey.Value.ToString(); 
    SqlDataSource1.SelectParameters["AnswerID"].DefaultValue = id; 
    ListView1.DataBind(); 
} 

回答

0

嘗試使用此方法獲得所選行看重

ListView1.SelectedIndex = e.NewSelectedIndex; 
string id=ListView1.SelectedDataKey.Value.ToString(); 
//Then pass the id's value 
ListView1.DataBind(); 

希望它能幫助。

+0

哪個事件?在相同的? – Ophelia

+0

@ user1455888我編輯了我的答案。事件是一樣的。 – freebird

+0

我試圖通過它,但它不起作用。 – Ophelia