[英]Asp.net Dropdownlist selected index changed AND TextChanged Events not Fire? (C#)
I am new to ASP>NET(C#). 我是ASP> NET(C#)的新手。 But I am using Winform before. 但是我以前使用Winform。
In my Project I have two dropdown list. 在我的项目中,我有两个下拉列表。 if i change the one,.. the second will change auto. 如果我改变一个,..第二将改变自动。
But even event also not firing in ASP.net. 但是即使事件也没有在ASP.net中触发。 Without event how to i manage that?. 没有事件,我该如何管理?
<div id="Div1" class="ui-content ui-body-a" runat="server">
<asp:dropdownlist id="ddlOutlet" runat="server" autopostback="True" onselectedindexchanged="ddlOutlet_SelectedIndexChanged"
ontextchanged="ddlOutlet_TextChanged">
</asp:dropdownlist>
<br />
<asp:dropdownlist id="ddlServedAt" runat="server">
</asp:dropdownlist>
<br />
<asp:button id="btnLogin" runat="server" text="LogIn" />
</div>
C# C#
I already Added items in both dropdown list in page load event. 我已经在页面加载事件的两个下拉列表中添加了项目。 But When I change the ddlOutlet selected index changed Event not firing. 但是当我更改ddlOutlet所选索引时,更改了事件未触发。 So I tried to TextChanged Events Also. 所以我也尝试了TextChanged事件。 But nothing happened. 但是什么也没发生。 What is problem?. 有什么问题吗?
Page Load Event - 页面加载事件-
protected void Page_Load(object sender, EventArgs e)
{
HelpingFunctions hp = new HelpingFunctions();
string id = Request.QueryString["id"];
MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs@2011$;database=mcubic;");
string query = "SELECT c.Outlet_Master_Name,d.Fbserved_Served FROM mcs_user a, mcs_user_outlet b,outlet_master c,fb_served d WHERE a.Mcs_User_Id='" + id + "' and"
+ " a.Mcs_User_Id = b.Mcs_User_Outlet_User_Id and c.Outlet_Master_Id = b.Mcs_User_Outlet_Outlet_Id and c.Outlet_Master_Id=d.Fbserved_outletid";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
ddlOutlet.Items.Add(Reader[0].ToString());
ddlServedAt.Items.Add(Reader[1].ToString());
}
connection.Close();
}
Selected Item - 选定项目-
protected void ddlOutlet_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs@2011$;database=mcubic;");
string query = "select Fbserved_Served from fb_served where Fbserved_outletid = (select Outlet_Master_Id from outlet_master where Outlet_Master_Name ='" + ddlOutlet.SelectedItem.ToString() + "')";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
ddlServedAt.SelectedItem.Value = Reader[0].ToString();
}
connection.Close();
}
I read some same issue problem solutions, but they told to set AutoPostBack="True". 我阅读了一些相同问题的问题解决方案,但他们告诉他们设置AutoPostBack =“ True”。 I checked that also. 我也检查了。
I don't know why ASP.net hard to Event firing also?. 我不知道为什么ASP.net很难触发事件?
UPDATED QUESTION 更新的问题
In my Project,.. I am using jquerymobile(http://jquerymobile.com/) with ASP.Net and mysql. 在我的项目中,..我正在将jquerymobile(http://jquerymobile.com/)与ASP.Net和mysql一起使用。 I have two drop down list controls and ONE button. 我有两个下拉列表控件和一个按钮。
But When I change the ddlOutlet selected index changed Event not firing. 但是当我更改ddlOutlet所选索引时,更改了事件未触发。 After the button Click the Events Are fired Correctly. 在按钮之后,单击“正确触发事件”。 But Before that They not fire. 但在此之前,他们不会开火。 I do't know Why its happen. 我不知道为什么会这样。
I Give my Complete Code Below. 我在下面提供完整的代码。
My Complete ASPX File :- 我完整的ASPX文件:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OutLet.aspx.cs" Inherits="MobileApp.OutLet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta name="viewport" content="width=device-width; height=device-height; initial-scale=1.0; maximum-scale=1.5; user-scalable=no;" />
<link href="Styles/jquery.mobile-1.0b3.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.mobile-1.0b3.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server" data-ajax="false">
<div id="mainheader" class="ui-header-fixed ui-bar-a">
</div>
<div id="Div1" class="ui-content ui-body-a" runat="server">
<%-- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>--%>
<asp:DropDownList ID="ddlOutlet" runat="server"
AutoPostBack="True" onselectedindexchanged="ddlOutlet_SelectedIndexChanged"
ontextchanged="ddlOutlet_TextChanged">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlServedAt" runat="server" AutoPostBack="True">
</asp:DropDownList>
<br />
<asp:Button ID="btnLogin" runat="server" Text="LogIn"
onclick="btnLogin_Click" />
<%-- </ContentTemplate>
</asp:UpdatePanel> --%>
</div>
</form>
</body>
</html>
my Code - 我的代码-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
namespace MobileApp
{
public partial class OutLet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HelpingFunctions hp = new HelpingFunctions();
string id = Request.QueryString["id"];
if (!Page.IsPostBack)
{
MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs@2011$;database=mcubic;");
string query = "SELECT c.Outlet_Master_Name,d.Fbserved_Served FROM mcs_user a, mcs_user_outlet b,outlet_master c,fb_served d WHERE a.Mcs_User_Id='" + id + "' and"
+ " a.Mcs_User_Id = b.Mcs_User_Outlet_User_Id and c.Outlet_Master_Id = b.Mcs_User_Outlet_Outlet_Id and c.Outlet_Master_Id=d.Fbserved_outletid";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
ddlOutlet.Items.Add(Reader[0].ToString());
ddlServedAt.Items.Add(Reader[1].ToString());
}
connection.Close();
}
}
protected void ddlOutlet_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs@2011$;database=mcubic;");
string query = "select Fbserved_Served from fb_served where Fbserved_outletid = (select Outlet_Master_Id from outlet_master where Outlet_Master_Name ='" + ddlOutlet.SelectedItem.ToString() + "')";
MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
ddlServedAt.SelectedValue = Reader[0].ToString();
}
connection.Close();
}
protected void ddlOutlet_TextChanged(object sender, EventArgs e)
{
//MySqlConnection connection = new MySqlConnection("server=192.168.1.100;username=mcubic;password=mcs@2011$;database=mcubic;");
//string query = "select Fbserved_Served from fb_served where Fbserved_outletid = (select Outlet_Master_Id from outlet_master where Outlet_Master_Name ='" + ddlOutlet.SelectedItem.ToString() + "')";
//MySqlCommand command = new MySqlCommand(query, connection);
//connection.Open();
//MySqlDataReader Reader = command.ExecuteReader();
//while (Reader.Read())
//{
// ddlServedAt.SelectedItem.Value = Reader[0].ToString();
//}
//connection.Close();
}
protected void btnLogin_Click(object sender, EventArgs e)
{
}
}
}
Create the drop down list(the one that changes the content of the other one) 创建下拉列表(一个更改另一个列表的内容)
Activity: <asp:DropDownList ID="cmbActivity" runat="server" OnSelectedIndexChanged="cmbActivity_SelectedIndexChanged" AutoPostBack="true" />
Then in your C#, your load event should be like this 然后在您的C#中,您的load事件应如下所示
if (Session["staffId"] == null)
{
Response.Redirect("~/login.aspx");
}
else
{
if (!IsPostBack)
{
cmbActivity.DataSource = a;
cmbActivity.DataTextField = "activityName";
cmbActivity.DataValueField = "activiyId";
}
}
Your selectedIndex_Changed method still remains the same. 您的selectedIndex_Changed方法仍然保持不变。 This should work. 这应该工作。 Have more than one item in the list to be sure. 确保列表中有多个项目。 I hope this helps 我希望这有帮助
try by Adding Page.IsPostBack
in your page_Load
event... 尝试通过在page_Load
事件中添加Page.IsPostBack
...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//your page load code.....
}
}
EDIT - 1 编辑-1
Sample Code of ASPX page in Web Application Web应用程序中ASPX页面的示例代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList id="id1" runat="server" AutoPostBack="true"
onselectedindexchanged="id1_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="id2" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
Sample code of code behind in Web Application Web应用程序中的代码示例代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void id1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Sample code of ASPX page in Website 网站中ASPX页面的示例代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList id="id1" runat="server" AutoPostBack="true"
onselectedindexchanged="id1_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="id2" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
Sample code of Code behind in Website 网站中后面的代码示例代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void id1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
OP在他的问题中已经有这个问题,但是我缺少了autopostback="True"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.