2

リストボックスを含む ASP.NET プラクティス Web サイトを開発しています。アイテムを選択すると、スクロールバーが突然動きます。どうしてこれなの?これは一般的ですか?それとも、私は何か間違ったことをしていますか?........ 提案をいただければ幸いです。問題のスクリーンショットを投稿しています。

ここに画像の説明を入力

    <%@ 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