Button click to add to a List then button click to output data to page

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have two buttons and two text boxes on my web page. One text box is an auto-complete AJAX field and the other is a multi-line text box. The multi-line text box receives the auto-completed value of the other text box via Button 1 click event. Button 2 should print the count of the List<string>. The Button 2 click event doesnt work, it always prints a value of 0, even if there are multiple lines of text in the multi line text box. Whats going on here? Other aspects of the program seem to work fine.using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Services;
using System.Configuration;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
List<string> drugList = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
txtCategory.Focus();

}
[WebMethod]
public static List<string> GetAutoCompleteData(string CategoryName)
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand("sql query here", con))
{
con.Open();
cmd.Parameters.AddWithValue("@CategoryText", CategoryName);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["column_name"].ToString());
}
return result;
}
}

}

protected void Button1_Click(object sender, EventArgs e)
{

if (txtCategory.Text.Length > 0)
{
//this is where the breakdown happens.
drugList.Add(txtQueryTerms.Text);
StringBuilder sb = new StringBuilder(txtQueryTerms.Text);
sb.AppendLine(txtCategory.Text);
txtQueryTerms.Text = sb.ToString();

txtCategory.Text = "";
}
else
{
Response.Write("you must enter a string of at least one character");
}

}

protected void Button2_Click(object sender, EventArgs e)
{

Response.Write(drugList.Count);
foreach (string s in drugList)
Response.Write(s);

}
}

markup<%@ Page Language="C#" AutoEventWireup="true" Inherits="_Default" Codebehind="Default.aspx.cs" %>

<!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 id="Head1" runat="server
<title>AutoComplete Text Box with jQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js </script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js </script>
<script type="text/javascript
$(document).ready(function () {
SearchText();
});
function SearchText() {
$(".auto").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData",
data: "{CategoryName:" + document.getElementById(txtCategory).value + "}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error Occurred");
}
});
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server
<div class="demo
<div class="ui-widget
<label for="tbAuto
Enter Drug&nbsp; Name:
</label>
<asp:TextBox ID="txtCategory" CssClass="auto" runat="server </asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button 1" />
<asp:TextBox ID="txtQueryTerms" runat="server" Height="300px" TextMode="MultiLine </asp:TextBox>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button 2" /><br />
<asp:Label ID="Label1" runat="server" Text="Label </asp:Label>





</form>
</body>
</html>

View the full article
 
Back
Top