gpt4 book ai didi

c# - 列表框滚动条在没有任何命令的情况下移动

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:03 26 4
gpt4 key购买 nike

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

enter image description here

    <%@ 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();

}
}

最佳答案

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

关于c# - 列表框滚动条在没有任何命令的情况下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18685993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com