2

我正在开发一个带有列表框的 ASP.NET 练习网站。当我选择一个项目时,滚动条突然移动。为什么是这样?这很常见吗?还是我做错了什么?............建议赞赏我发布了问题的截图。

在此处输入图像描述

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Refresh" />
            <br />
            <br />
            <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="120px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" Width="202px"></asp:ListBox>
            <br />
            <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox2_TextChanged"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Insert" />
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Update" />
            <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Delete" />

        </div>
        </form>
    </body>
    </html>




using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{   

    SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void refresh()
    {

        ListBox1.Items.Clear();

        cnn.Open();
        SqlCommand cmd = new SqlCommand("SELECT FirstName,LastName,EmployeeID FROM Employees", cnn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                ListBox1.Items.Add(new ListItem(dr.GetString(0) + " " + dr.GetString(1),dr.GetInt32(2).ToString()));
            }
        }

        cnn.Close();

    }

    protected void Button1_Click(object sender, EventArgs e) //refresh
    {
        refresh();
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        cnn.Open();
        SqlCommand cmd = new SqlCommand("SELECT FirstName,LastName FROM Employees WHERE EmployeeID = @numara", cnn);
        cmd.Parameters.AddWithValue("@numara", ListBox1.SelectedValue);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                TextBox1.Text = dr.GetString(0);
                TextBox2.Text = dr.GetString(1);
            }
        }

        cnn.Close();

    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e) //insert
    {
        cnn.Open();
        SqlCommand cmd = new SqlCommand("INSERT INTO Employees (FirstName, LastName) VALUES (@CalisanAdi, @CalisanSoyadi)", cnn);
        cmd.Parameters.AddWithValue("@CalisanAdi", TextBox1.Text);
        cmd.Parameters.AddWithValue("@CalisanSoyadi", TextBox2.Text);
        cmd.ExecuteNonQuery();
        cnn.Close();

        refresh();

    }

    protected void Button3_Click(object sender, EventArgs e) //update
    {
        cnn.Open();
        SqlCommand cmd = new SqlCommand("UPDATE Employees SET FirstName = @CalisanAdi , LastName = @CalisanSoyadi WHERE EmployeeID = @numara", cnn);
        cmd.Parameters.AddWithValue("@numara", ListBox1.SelectedValue);
        cmd.Parameters.AddWithValue("@CalisanAdi", TextBox1.Text);
        cmd.Parameters.AddWithValue("@CalisanSoyadi", TextBox2.Text);
        cmd.ExecuteNonQuery();
        cnn.Close();

        refresh();

    }

    protected void Button4_Click(object sender, EventArgs e) //delete
    {
        cnn.Open();
        SqlCommand cmd = new SqlCommand("DELETE FROM Employees WHERE EmployeeID = @numara", cnn);
        cmd.Parameters.AddWithValue("@numara", ListBox1.SelectedValue);
        cmd.ExecuteNonQuery();
        cnn.Close();

        refresh();

    }
}
4

1 回答 1

1

我认为这是因为当您在列表中选择一个项目时页面被回发,这是 ASP.net 的预期行为。页面正在刷新,您之前选择的项目正在列表框中被选中。

于 2013-09-08T17:13:03.733 回答