0

我有一个 asp.net ListView,当我单击选择链接按钮以选择行并将数据的 id 传递给详细信息视图时,我想要。它传递除最后一条记录之外的所有记录的 id。有什么理由吗?

    <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();
}
4

1 回答 1

0

试试这个来获取选定的行值

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

希望能帮助到你。

于 2012-06-14T10:04:29.957 回答