0

我一直在寻找这个问题的解决方案,并且我已经搜索了网络,但仍然无法解决这个问题。如果我将我的代码放在默认的 asp 模板中,它将起作用,但是当我使用我的模板时,我总是将列表框 Selectedindex 设为 -1。

这是我的 C# 代码:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web.UI.WebControls;

public partial class permissaotop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }  
    public void Droptopicoperfil_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Data Source=Microsoft SQL Server (SqlClient);Server=Server;Initial Catalog=Forum;uid=user;pwd=password;Connect Timeout=10;TrustServerCertificate=True ");
        conn.Open();


        SqlCommand comm = conn.CreateCommand();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = "SP_vertopicoperfil";

        comm.Parameters.AddWithValue("@id_topico", this.Droptopicoperfil.SelectedValue);

        SqlDataReader rdr = comm.ExecuteReader();

        lbpermitidotp.DataSource = rdr;
        lbpermitidotp.DataTextField = "Nome";
        this.lbpermitidotp.DataValueField = "Id";


        lbpermitidotp.DataBind();

        rdr.Close();
        conn.Close();

        conn.Open();

        SqlCommand comm1 = conn.CreateCommand();
        comm1.CommandType = CommandType.StoredProcedure;
        comm1.CommandText = "[SP_naotopicoperfil]";

        comm1.Parameters.AddWithValue("@id_topico", this.Droptopicoperfil.SelectedValue);
        SqlDataReader rdr1 = comm1.ExecuteReader();


        lbproibidotp.DataSource = rdr1;
        lbproibidotp.DataTextField = "Nome";
        this.lbproibidotp.DataValueField = "Id";

        lbproibidotp.DataBind();

        rdr1.Close();
        conn.Close();

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

        SqlConnection conn = new SqlConnection("Data Source=Microsoft SQL Server (SqlClient);Server=NEVETS-LAPTOP\\NEVETS;Initial Catalog=Forum;uid=sa;pwd=sql;Connect Timeout=10;TrustServerCertificate=True ");

        conn.Open();
        SqlCommand comm = conn.CreateCommand();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = "usp_inserirtopicoperfil";

        comm.Parameters.AddWithValue("@id_topico", this.Droptopicoperfil.SelectedValue);             
        comm.Parameters.AddWithValue("@id_perfil", this.lbproibidotp.SelectedValue.ToString());


        SqlDataReader rdr = comm.ExecuteReader();

        if (this.lbproibidotp.SelectedIndex >= 0)
        {
            this.lbproibidotp.Items.RemoveAt(this.lbproibidotp.SelectedIndex);
        }

        lbpermitidotp.DataBind();
        lbproibidotp.DataBind();
        Droptopicoperfil.DataBind();

        rdr.Close();
        conn.Close();

        Droptopicoperfil_SelectedIndexChanged(this, null);
    }
    protected void BtnpermitirClick(object sender, EventArgs e)
    {

        SqlConnection conn = new SqlConnection("Data Source=Microsoft SQL Server (SqlClient);Server=NEVETS-LAPTOP\\NEVETS;Initial Catalog=Forum;uid=sa;pwd=sql;Connect Timeout=10;TrustServerCertificate=True ");
        conn.Open();

        SqlCommand comm = conn.CreateCommand();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = "SP_apagartopicoperfil";

        comm.Parameters.AddWithValue("@id_topico", this.Droptopicoperfil.SelectedValue);

        comm.Parameters.AddWithValue("@id_perfil", lbpermitidotp.SelectedValue.ToString());


        SqlDataReader rdr = comm.ExecuteReader();

        lbpermitidotp.DataSource = rdr;

        if (this.lbpermitidotp.SelectedIndex >= 0)
        {
            this.lbpermitidotp.Items.RemoveAt(this.lbpermitidotp.SelectedIndex);
        }

        lbpermitidotp.DataBind();
        lbproibidotp.DataBind();
        Droptopicoperfil.DataBind();

        rdr.Close();
        conn.Close();

        Droptopicoperfil_SelectedIndexChanged(this, null);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    //testing listbox
        Label3.Text =lbproibidotp.SelectedIndex).ToString();
        //Always gets the result -1

    }
}

ASP:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="permissaotop.aspx.cs" Inherits="permissaotop" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content" Runat="Server">
    <br />
    <asp:Panel ID="pnlperfil" runat="server" GroupingText="Topicoperfil" 
        style="text-align: center; margin-top: 15px;" 
        ScrollBars="None" Height="607px" 
        HorizontalAlign="Center" Wrap="False" EnableTheming="False" 
        Direction="LeftToRight" Font-Size="Small" Width="711px">
        &nbsp;&nbsp;<br />
        <br />
        <table align="left">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Perfil"></asp:Label>
                    <br />
                </td>
                <td>
                    <asp:DropDownList ID="Dropperfil" runat="server" Height="20px" Width="213px" 
                        AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Nome" 
                        DataValueField="Id" >
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:forumConnectionString2 %>" 
                        SelectCommand="SELECT * FROM [Perfil]"></asp:SqlDataSource>
                    <br />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td >
                    <asp:Label ID="Label2" runat="server" Text="topico"></asp:Label>
                    <br />
                </td>
                <td >
                    <asp:DropDownList ID="Droptopicoperfil" runat="server" Height="19px" 
                        style="margin-left: 3px" Width="213px" AutoPostBack="True" 
                        DataSourceID="SqlDataSource2" DataTextField="nome" DataValueField="Id_topico" 
                        >
                          <asp:ListItem Text="-Select-" Value="" />
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:forumConnectionString2 %>" 
                        SelectCommand="SELECT * FROM [Topico]"></asp:SqlDataSource>
                    <br />
                </td>
                <td >
                    &nbsp;</td>
            </tr>
            <tr>
                <td >
                    <asp:ListBox ID="lbpermitidotp" runat="server" Width="113px" 
                        onselectedindexchanged="lbpermitidotp_SelectedIndexChanged" 
                        ViewStateMode="Enabled"></asp:ListBox>
                </td>
                <td >
                    <asp:Button ID="btnproibirtp" runat="server" style="text-align: center" 
                        Text="&gt;&gt;" onclick="BtnpermitirClick" />
                    <asp:Button ID="btnpermitirtp" runat="server" Text="&lt;&lt;" 
                        onclick="BtnnaopermitirClick" />
                    <asp:Label ID="Label3" runat="server" style="background-color: #000000" 
                        Text="Label"></asp:Label>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                </td>
                <td >
                    <asp:ListBox ID="lbproibidotp" runat="server" Width="100px" 
                        ViewStateMode="Enabled"></asp:ListBox>
                    <br />
                    <br />

                    </td>
            </tr>
        </table>
    </asp:Panel>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="AfterBody" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="Footer" Runat="Server">
</asp:Content>

我怎样才能解决这个问题?

4

0 回答 0