列表框selectedindexchange事件未触发? ASP.NET C# [英] Listbox selectedindexchange event not firing ? ASP.NET C#

查看:244
本文介绍了列表框selectedindexchange事件未触发? ASP.NET C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我有一个列表框,它从数据库中重新显示值,当我从lisbox中选择值时,我希望该页面重定向到其他,但我无法做到这一点,我想我的代码是完美但不工作.Autopostback属性是'真'但它没有触发事件。



我尝试过:



这是我的aspx代码:

 <   asp:ListBox     ID   =  ListBox1    runat   =  server   高度  =  202px   宽度  =  262px < span class =code-attribute>   

< span class =code-attribute> 样式 = margin-left:247px; margin-top:68px

onselectedindexchanged = ListBox2 AutoPostBack = true EnableViewState = True >
< / asp:ListBox >



这里是我的aspx.cs代码

 受保护  void  Page_Load(< span class =code-keyword> object  sender,EventArgs e)
{
if (!Page.IsPostBack)
{
getdata();
}
}
public void getdata()
{
string user = Session [ 名称]的ToString();

SqlConnection cnn = new SqlConnection( 数据源= HAMEED_KHAN \\SQLEXPRESS;初始目录= db_compiler;集成安全性=真);
SqlCommand cmd2 = new SqlCommand( SELECT来自tbl_user的User_ID WHERE User_Name =' + user + ',cnn);
cnn.Open();
string id = cmd2.ExecuteScalar()。ToString();
int ID = Int32 .Parse(id);
SqlCommand cmd = new SqlCommand( USE db_compiler SELECT Database_Name FROM Create_db WHERE User_ID =' + ID + ',cnn );
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
da.Fill(dt);
if (dt.Rows.Count > 0
{
foreach (DataRow row in dt .Rows)
{
string value = row [ Database_Name] + ;

ListBox1.Items.Add( value );
}

}
}
public void ListBox2( object sender,EventArgs e)
{
Session [ value] = ListBox1.SelectedValue;

Response.Redirect( CreateTable.aspx);
}
}



从数据库加载到列表框中的值很好,但页面没有重定向到其他。我使用断点来确认是事件触发或不,但事件没有解雇。谢谢

解决方案

请确保页面标记中的以下属性设置为true

 AutoEventWireup = 真


Hi.
I have a listbox which reterive values from database and when i select value from lisbox then i want that page redirect to other,but I'm unable to do this,I think my code is perfect but not working.Autopostback property is 'true' but it's not firing the event .

What I have tried:

Here is my aspx code :

 <asp:ListBox ID="ListBox1" runat="server" Height="202px" Width="262px" 

        style="margin-left: 247px; margin-top: 68px" 

        onselectedindexchanged="ListBox2" AutoPostBack="true" EnableViewState="True">
</asp:ListBox>


and here is my aspx.cs code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getdata();
        }
    }
    public void getdata()
    {
        string user = Session["name"].ToString();

        SqlConnection cnn = new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");
        SqlCommand cmd2 = new SqlCommand("SELECT User_ID from tbl_user WHERE User_Name='" + user + "'", cnn);
        cnn.Open();
        string id = cmd2.ExecuteScalar().ToString();
        int ID = Int32.Parse(id);
        SqlCommand cmd = new SqlCommand("USE db_compiler SELECT Database_Name FROM  Create_db WHERE User_ID='" + ID + "'", cnn);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        cmd.ExecuteNonQuery();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                string value = row["Database_Name"] + "";

                ListBox1.Items.Add(value);
            }

        }
    }
    public void ListBox2(object sender, EventArgs e)
    {
        Session["value"] = ListBox1.SelectedValue;

        Response.Redirect("CreateTable.aspx");
    }
}


Values loaded from database into listbox well,but page not redirect to other.I use break point to confirm that is event firing or not,But event not firing.Thanks

解决方案

Please make sure you have following attribute in page markup set to true

AutoEventWireup="true"


这篇关于列表框selectedindexchange事件未触发? ASP.NET C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆