private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("Reciprocal link: " + reciplink.Text);
Response.Write("<br>");
Response.Write("Customer website: " + custurl.Text);
Response.Write("<br>");
Response.Write("Customer title: " + custtitle.Text);
}
Problem is that when I display these values on the web page the input boxes and original form are still there. So the values are be written on top of the previous form. How do you clear the contents of the web page so that when the end user clicks a submit button they see ONLY the values they entered. Do you spawn a new page or ?One thing you can do is set the Visible property of web controls to false.
reciplink.Visible = false;
custurl.Visible = false;
custtitle.Visible = false;
Hi,
using response.write is not such a good idea to show information to your users because it writes everything before the opening html tag.
Rather use a label control or literal control for this and set the Text property.
I cooked up a little piece of code to demonstrate what I mean:
Some extra information: I placed the textbox and button control in a panel control so I only have to set the visibility of this 1 control instead of setting this property of every control (I'm on vacation right now so I'm a bit lazy ;-) ).
<%@. Page Language="C#" %>
<script runat="server"
void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Your input was: " + TextBox1.Text.Trim();
Panel1.Visible = false;Response.Write(Label1.Text);
}</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Panel id="Panel1" runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
</p>
</asp:Panel>
</p>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
When you take a look at the rendered page source in your browser (view | Source) you'll see the text "Your input was: ..." appear on the first line in the html by this breaking the code. This comes from the response.write. It used to be a mechanism in classic asp but nowadays we havetracing for that.
Grz, Kris.
Thanks for your response. I do not understand where you are writing the variable to page. Obviously I get the response.write but using the panel I still do not get it. I have played around and can see how placing objects on one panel and then setting it to false will remove all the input boxes. But how are you returning data to the browser without a response statement??
Also this statement:
<asp:Label id="Label1" runat="server"></asp:Label
Is this essentially just initializing a variable so that you can use it in the c# function?
When I run my code using your suggestion the panel does disappear but I do not get any out put like yours?
this is what I am using:
private void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Your input was: " + TextBox1.Text.Trim();
Panel1.Visible = false;
}
AsP.Net is event driven and object oriented - - in any given event, you assign text to the text property of a label (an object) - - that, in turn, is displayed on the screen.
your label isn't inside the panel is it?
if you make the panel invisible and the label's inside the panel, the label will be invisible also.
Something like this: (im using a codebehind page for everything i do)
test.aspx
<%@. Page language="c#" src="http://pics.10026.com/?src=test.aspx.cs" AutoEventWireup="true" Inherits="xtest.test" %>
<HTML>
<HEAD>
<title>Test</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:panel id="InputFields" runat="server" Visible="false">
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button></P>
</asp:panel><asp:panel id="Result" runat="server" Visible="false">
<P>
<asp:Label id="Label1" runat="server"></asp:Label></P>
</asp:panel></form>
</body>
</HTML>
test.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace xtest
{
public class test : System.Web.UI.Page
{protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Panel InputFields;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Panel Result;private void Page_Load(object sender, System.EventArgs e)
{
if ( ! Page.IsPostBack )
{
InputFields.Visible = true;
Result.Visible = false;
}
}public void Button1_Click(object sender, System.EventArgs e)
{
InputFields.Visible = false;
Result.Visible = true;Label1.Text = TextBox1.Text.Trim();
}
}
}
That's it. I had the label inside the panel, duh! Thanks. From a best practices standpoint, would you recommend using web controls on a panel as above for most of your user input pages? In turn you could place multiple panels on a particluar page for more flexibility etc?
Hi,
you can place multiple panels on your page or you could use user controls or create your own composite controls.
That's just the big advantage and sometimes even the big disadvantage of ASP.NET: you've got a lot of freedom to do what you want to do. And just like a kid that needs to learn responsibility a developer using ASP.NET needs some time to find out which methodolgy is the best way for solving a particular problem.
Grz, Kris.
0 comments:
Post a Comment