SelectedIndexChanged事件无效

时间:2014-08-16 07:02:50

标签: c# asp.net

我试图在触发事件onSelectIndexChanged时将存储过程中的数据放入下拉列表中。但在提出断点后,我知道生成的事件不起作用,即控件甚至没有进入该代码。

 <tr>
        <td><asp:Label ID="Label5" runat="server" Text="Book Category"></asp:Label></td>
        <td>
            <asp:DropDownList ID="ddlBookCategory" runat="server" Height="20px" Width="250px" OnSelectedIndexChanged="ddlBookCategory_SelectedIndexChanged"  >
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td><asp:Label ID="Label4" runat="server" Text="Book Subject"></asp:Label></td>
        <td>
            <asp:DropDownList ID="ddlBookSubject" runat="server" Height="20px" Width="250px" >
            </asp:DropDownList>
        </td>
    </tr>

后端cs文件编码为::

 protected void Page_Load(object sender, EventArgs e)
{


     connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
     lblmsg.Visible = false;

     OleDbConnection con = new OleDbConnection(connectionString);
     OleDbCommand cmd = new OleDbCommand();

     try
     {
         con.Open();
         cmd.CommandType = System.Data.CommandType.StoredProcedure;

         cmd.CommandText = "ShowBookCategory";
         cmd.Connection = con;



         OleDbDataReader reader = cmd.ExecuteReader();


         if (reader.HasRows)
         {
             while (reader.Read())
             {
                 ddlBookCategory.Items.Add(new ListItem(reader["CategoryName"].ToString()));
             }
         }

     }
     catch (OleDbException ex)
     {
         ex.Data.ToString();
     } 
}


 protected void ddlBookCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    ddlBookCategory.AutoPostBack = true;
    OleDbConnection con = new OleDbConnection(connectionString);
    OleDbCommand cmd2 = new OleDbCommand();
    MbERPLibraryBookSubjectProperty foc = new MbERPLibraryBookSubjectProperty();

    try
    {
        con.Open();
        cmd2.CommandType = System.Data.CommandType.StoredProcedure;
        cmd2.CommandText = "ShowBookSubjectWithCategory";
        cmd2.Connection = con;

        foc.CategoryName = ddlBookCategory.Text.ToString();
        cmd2.Parameters.Add(new OleDbParameter("@CategoryName", foc.CategoryName));
        cmd2.Parameters["@CategoryName"].Direction = System.Data.ParameterDirection.Input;

        OleDbDataReader reader2 = cmd2.ExecuteReader();
        if (reader2.HasRows)
        {
            while (reader2.Read())
            {
                ddlBookSubject.Items.Add(new ListItem(reader2["SubjectName"].ToString()));
            }
        }

    }
    catch (OleDbException ex)
    {
        ex.Data.ToString();
    } 
}

我的存储过程是:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[ShowBookSubjectWithCategory](
    @CategoryName varchar(50)
    )
AS
BEGIN
select * from BookSubjects Where CategoryName = @CategoryName
END

3 个答案:

答案 0 :(得分:1)

如果要动态设置ddlBookCategory.AutoPostBack = true;,请在pageload事件中设置它。

答案 1 :(得分:1)

不要在事件处理程序中将autoPostBack声明为true,而是尝试在下面的DropDownList声明中设置它:

<asp:DropDownList ID="ddlBookCategory" ... autopostback="true" ... >
...
</asp:DropDownList>

答案 2 :(得分:0)

您需要向控件添加AutoPostBack = true,以便控件向服务器发出发生事件的信号。完成后,您可以为代码中的方法设置一个事件处理程序,用于该控件上选定的索引更改事件。