Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Div tag appending, not replacing

Status
Not open for further replies.

clanm

Programmer
Joined
Dec 26, 2005
Messages
237
Location
US
All,

I figured out the code (right after if(xmlHttp.status == 200)). My earlier problem was a null reference to the div tage 'show' only in IE, but it worked in FireFox.

The app works like:
If the number isn't unique via a database SQL count(*), it'll tell you so, but then if you enter another number (which is in fact distinct), then the user gets the message right underneath the previous {div id=show} message...it's not replaced....just appended! I'd like to have it replace the value, rather than stick the new message underneath.

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="AjaxTesting.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
<!-- Start script -->
<script language="javascript">
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlHttp = false;
}
}
else
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
xmlHttp = false;
}
}
if (!xmlHttp)
{
alert("Error creating the XMLHttpRequest object.");
}
else
{
return xmlHttp;
}
}
function process()
{
varWBS = encodeURIComponent(document.getElementById("TextBox1").value);
xmlHttp.open("GET", "test.aspx?WBS=" + varWBS);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}
function handleServerResponse()
{
//if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
//{
// document.getElementById("show").innerHTML = xmlHttp.responseText;
//}
//}
//New code
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var parentElement = document.getElementById('show');
var wrappingDiv = document.createElement('div');
wrappingDiv.innerHTML = xmlHttp.responseText;
parentElement.appendChild(wrappingDiv);

//document.getElementById("show").innerHTML = xmlHttp.responseText;
//document.all[show].innerHTML = xmlHttp.responseText;
}
else
{
alert("hmmmm");
}
}
}
//End code
</script>
<!-- End script -->
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table id="maintbl" border="1">
<tr>
<td><asp:textbox id="TextBox1" onblur="process()" style="Z-INDEX: 101" runat="server"></asp:textbox></td>
<td><asp:textbox id="TextBox2" style="Z-INDEX: 101" runat="server"></asp:textbox></td>
</tr>
<tr>
<td>
<div id="show"></div>
</td>
</tr>
</table>
</form>
</body>
</HTML>
 
cLFlaVA,

Thanks for the reply!

The repeating div tags are fine. They may just work for the user.
I can type in the value in one text box, then it'll go to the code behind of another page and do the correct functionality....which will tell the user if the number is distinct or not.
The only problem is when I post back the page, I get an IE error, ieexplore.exe error, The instruction at "0x4a594476" referenced memory at "0x00000004". The memory could not be "read".

HTML code is:
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
<!-- Start script -->
<script language="javascript">
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlHttp = false;
}
}
else
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
xmlHttp = false;
}
}
if (!xmlHttp)
{
alert("Error creating the XMLHttpRequest object.");
}
else
{
return xmlHttp;
}
}
function process()
{
varWBS = encodeURIComponent(document.getElementById("TextBox1").value);
xmlHttp.open("GET", "test.aspx?WBS=" + varWBS);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}
function handleServerResponse()
{
//if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
//{
// document.getElementById("show").innerHTML = xmlHttp.responseText;
//}
//}
//New code
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var parentElement = document.getElementById('show');
var wrappingDiv = document.createElement('div');
wrappingDiv.innerHTML = document.getElementById("TextBox1").value+' '+xmlHttp.responseText;
parentElement.appendChild(wrappingDiv);

//document.getElementById("show").innerHTML = xmlHttp.responseText;
//document.all[show].innerHTML = xmlHttp.responseText;
}
else
{
alert("hmmmm");
}
}
}
//End code
</script>
<!-- End script -->
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table id="maintbl" border="1" style="WIDTH: 952px; HEIGHT: 59px">
<tr>
<td style="WIDTH: 327px"><asp:textbox id="TextBox1" onblur="process()" style="Z-INDEX: 101" runat="server" Width="100%"></asp:textbox></td>
<td><asp:textbox id="TextBox2" style="Z-INDEX: 101" runat="server"></asp:textbox></td>
</tr>
<tr>
<td colspan="2">
<div id="show" style="FONT-SIZE: smaller; COLOR: red" runat="server"></div>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</td>
</tr>
</table>
</form>
</body>
</HTML>


The code behind for the test.aspx page is:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
sCon1.Open()
Dim strSQL As String = "Select count(*) from Table where [Number] = '" & Server.HtmlEncode(Request.QueryString("WBS")).ToString() & "'"
Dim cmd As New SqlCommand(strSQL, sCon1)
Dim intCnt As Integer = cmd.ExecuteScalar()
sCon1.Close()
If intCnt = 0 Then
Response.Write("is unique, and able to be added!")
Else
Response.Write("is already used. Please select another Number!")
End If

Catch ex As Exception
Response.Write(ex.ToString())
Finally
sCon1.Close()
End Try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top