我有一个ASP DropDownList,当从中选择一个项目时它会消失。应该发生的是另一个DropDownList应该出现,但它也没有显示。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title>KYFB Black Book</title>
<link rel="stylesheet" href="blackbook.css" />
</head>
<body>
<img class="logo" src="images/logo.jpg"/>
<p id="home_title" runat="server">Black Book</p>
<form id="sign_in_form" runat="server">
<div>
<input id="username_field" runat="server" type="text" placeholder="Username" name="username" /> <br />
<input id="password_field" runat="server" type="password" placeholder="Password" name="password" /> <br /> <br />
<input id="submit_button" runat="server" type="submit" value="Sign In" onclick="ajaxFunction();" />
</div>
</form>
<form id="selection_form" runat="server">
<div id="counties_div" style="display: block; width: 15%; margin: auto;" runat="server">
<p id="county_list_title" style="color:white;" runat="server">Select a county</p>
<asp:DropDownList ID="county_list" AutoPostBack="True" OnSelectedIndexChanged="countyChange" runat="server"></asp:DropDownList>
<br />
<p id="committee_list_title" style="color:white;" runat="server">Select a committee</p>
<asp:DropDownList ID="committee_list" AutoPostBack="true" OnSelectedIndexChanged="committeeChange" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
编辑:在被要求提供更多代码之后,这里是所有的C#。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
selection_form.Visible = false;
counties_div.Visible = false;
submit_button.ServerClick += new EventHandler(this.submitPressed);
}
public void displayAlert(String message)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message + "');", true);
}
public void submitPressed(Object sender, EventArgs e)
{
if (username_field.Value == "")
{
this.displayAlert("Enter your username");
}
else if (password_field.Value == "")
{
this.displayAlert("Enter your password");
}
else
{
// Validate credentials
String username = username_field.Value;
String password = password_field.Value;
WebRequest request = WebRequest.Create("https://membershipwebapp.azurewebsites.net/signin");
request.Method = "GET";
request.ContentType = "application/json";
String encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
var reader = new StreamReader(response.GetResponseStream());
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
var objArray = js.Deserialize<dynamic>(objText);
// Check if JSON array is empty
if (objArray.Length == 0)
{
// Display sign in error
this.displayAlert("Username or password was incorrect.Try again.");
}
else
{
Dictionary<String, dynamic> userDictionary = objArray[0];
home_title.InnerHtml = "Welcome, " + userDictionary["FirstName"];
// Display black book data
sign_in_form.Visible = false;
this.getCounties();
selection_form.Visible = true;
counties_div.Visible = true;
committee_list_title.Visible = false;
committee_list.Visible = false;
}
}
else
{
// Display error message
this.displayAlert("An error occurred");
}
response.Close();
}
}
public void countyChange(Object sender, EventArgs e)
{
if (county_list.Items[county_list.SelectedIndex].Value != "")
{
this.getCommittees();
committee_list_title.Visible = true;
committee_list.Visible = true;
}
}
public void getCounties()
{
WebRequest request = WebRequest.Create("https://membershipwebapp.azurewebsites.net/counties");
request.Method = "GET";
request.ContentType = "application/json";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
var reader = new StreamReader(response.GetResponseStream());
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
var objArray = js.Deserialize<dynamic>(objText);
// Check if JSON array is empty
if (objArray.Length == 0)
{
// Display error
this.displayAlert("Error getting counties");
}
else
{
county_list.Items.Add("");
for (int i = 0; i < objArray.Length; i++)
{
// Get dictionary and add county name to list
Dictionary<String, dynamic> countyDictionary = objArray[i];
String countyName = countyDictionary["county"];
county_list.Items.Add(countyName);
}
}
}
else
{
// Display error message
this.displayAlert("Error getting counties");
}
response.Close();
}
public void getCommittees()
{
String selectedCounty = county_list.Items[county_list.SelectedIndex].Value;
String url = "https://membershipwebapp.azurewebsites.net/committee/" + selectedCounty;
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
var reader = new StreamReader(response.GetResponseStream());
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
var objArray = js.Deserialize<dynamic>(objText);
// Check if JSON array is empty
this.displayAlert("committee array length: " + objArray.Length);
if (objArray.Length == 0)
{
// Display error
this.displayAlert("Error getting committees");
}
else
{
committee_list.Items.Add("");
for (int i = 0; i < objArray.Length; i++)
{
// Get dictionary and add county name to list
Dictionary<String, dynamic> committeeDictionary = objArray[i];
String committeeName = committeeDictionary["committee_name"];
committee_list.Items.Add(committeeName);
// committeeSelect.Items.Add(committeeName);
}
}
}
else
{
// Display error message
this.displayAlert("Error getting committees");
}
response.Close();
}
public void committeeChange(object sender, EventArgs e)
{
if (committee_list.Items[committee_list.SelectedIndex].Value != "")
{
// Display committee member info
}
}
}