简体   繁体   English

ListBoxItemSelected事件无法触发C#Asp.net?

[英]ListBoxItemSelected event not firing C# Asp.net?

I have a listboxitems i want that when i select value then page redirect to other page.I use onSelectedIndex property event function but it's not working.I search on google alot i find many solutions like autopostback property='true'. 我有一个listboxitems我希望当我选择值时将页面重定向到其他页面。我使用onSelectedIndex属性事件函数,但它不起作用。我在google上搜索很多,我发现许多解决方案,例如autopostback property ='true'。 I apply all solutions but still not working.My listbox values coming from database and it load fine but page not redirecting to other page. 我应用了所有解决方案,但仍然无法正常工作。我的列表框值来自数据库,并且可以很好地加载但页面无法重定向到其他页面。 Here is my code aspx code: 这是我的代码aspx代码:

<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<div class="select-db">
<form runat="server"></form>
<label style="position:relative; font-weight:bold;bottom:230px; left:450px; color:White;">Select Database</label>
<asp:ListBox ID="ListBox1" runat="server" Height="202px" Width="262px" 
style="margin-left: 247px; margin-top: 68px" 
onselectedindexchanged="ListBox_Changed" AutoPostBack="true" EnableViewState="True" >
</asp:ListBox>
</div>
</asp:Content>

and here is my aspx.cs code: 这是我的aspx.cs代码:

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 ListBox_Changed(object sender, EventArgs e)
{
 Session["value"] = ListBox1.SelectedValue; 
 Response.Redirect("CreateTable.aspx");
}
 public override void VerifyRenderingInServerForm(Control control)
{
 //base.VerifyRenderingInServerForm(control);
}
}

I also use 'breakpoint' to check that event is firing or not but event is not firing.I don't know why????? 我也使用'breakpoint'来检查事件是否触发,但事件没有触发。我不知道为什么?

All asp.net controls have to be inside a <form> that has a runat="server" attribute. 所有的asp.net控件都必须在具有runat="server"属性的<form>内。 Move the closing tag of your form to the end of the content and it should work. 将表单的结束标记移动到内容的末尾,它应该可以工作。 Like this: 像这样:

<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
    <div class="select-db">
        <form runat="server">
            <label style="position:relative; font-weight:bold;bottom:230px; left:450px; color:White;">Select Database</label>
            <asp:ListBox ID="ListBox1" runat="server" Height="202px" Width="262px" style="margin-left: 247px; margin-top: 68px" OnSelectedIndexChanged="ListBox_Changed" AutoPostBack="true" EnableViewState="True" >
            </asp:ListBox>
        </form>
    </div>
</asp:Content>

Also remember you can only have one <form> with runat="server" . 还要记住,您只能将一个<form>runat="server"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM