Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Well, I have one software(Authoring tool), it has some functionality by which I can fetch values from it in my C# application. So I am trying to fetch a value for TestName. After it I want to display that fetched value in a label, say Label1. I run the code but it is not showing the fetched value "Test 1", instead it just show me "Label1". So for testing when I debug the code using F10 in Visual studio 2010, I saw that in first loop it fetches the correct value that is "Test 1" and in Label also it shows correct value, "Test 1". But then don't know why after completing, it again goes to the first line of code and again whole loop executes. At that time it shows null values and thus at run, it doesnt show me desired value just showing "Label1". I am writing the code in Page_Load of .aspx.cs and my label is in .aspx of the same page. Help. Code is here.
Code is here.
test.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Collections;

public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{    //get the parameters posted from the test
    if (Page.IsPostBack)
    { }
    else
    {
        string testname = Request.Form["TestName"];
        Label1.Text = testname;
    }
}
}


test.aspx :
<%@ Page Language="C#" debug="true" AutoEventWireup="true" CodeFile="test.aspx.cs"   

Inherits="test" %>

<!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>
<%--<script type="text/javascript" language="Javascript">
 function setValues()
{
  //first check if you get TestName value byusing alert
   alert(document.getElementById("TestName").value);
   document.getElementById("Label1").innerHTML =    

document.getElementById("TestName").value;
//give return false to avoid postback
return false;
}
</script>--%>


<form id="form1" runat="server">
<div>

</div>

<p>

   <asp:Label ID="Label1" runat="server" Text="Label" Font-Size="Larger"></asp:Label>

</p>

</form>
</body>
</html>
Posted
Updated 15-Oct-13 0:09am
v2
Comments
[no name] 15-Oct-13 5:59am    
Ctrl + V whole code
JL_Coder 15-Oct-13 6:56am    
OK. I added whole the code. Kindly see it. I also tried isPostback property, it didn't work. I also tried javascript which you can see in code in comments.
Ankur\m/ 15-Oct-13 6:06am    
We will need to see "the loop code" you are talking about.
JL_Coder 15-Oct-13 6:57am    
OK. I added whole the code. Kindly see it. I also tried isPostback property, it didn't work. I also tried javascript which you can see in code in comments.
Ankur\m/ 15-Oct-13 7:29am    
Use 'Reply' button to reply to a comment. Please do not post same comment again and again.
I don't see 'TestName' in your HTML. What is it and how do you fill it's value. Otherwise the code seems fine to me and the label value should retain.

C#
You must be setting that label some where else false
. That is why its not showing.



I'd same problem. than I solved it this way.
Hope this helps your too
 
Share this answer
 
hi,

After page load event, check page is post or not. write your code inside in it:

C#
protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {

}
 
Share this answer
 
Comments
Ankur\m/ 15-Oct-13 6:07am    
And how will this correct the issue?
Salman622 15-Oct-13 9:13am    
when the page is post back it lost all its data
and the requirement of R_Coder is label text which is lost because of post back
if he write his code in !ispostback block the label text will not lost
and it will only load once after that it will not enter that block
here this is the solution........
Ankur\m/ 15-Oct-13 9:25am    
Firstly, a Label control will not loose its text property when the page is post back (unless you have disabled the ViewState for the page/control).
And you have got the question wrong. He never said the value is lost on postback. The value is never really shown, be it first load or a postback. There is some kind of loop where he is losing the value.
JL_Coder 16-Oct-13 2:03am    
@Salman622- I did it also. But it goes again to if block. I checked the label properties, they seem good.
JL_Coder 15-Oct-13 6:11am    
OK. I added whole the code. Kindly see it. I also tried isPostback property, it didn't work. I also tried javascript which you can see in code in comments.
write your code in page load as below



C#
protected void Page_Load(object sender, EventArgs e)
   {

if(!Ispostback)
{
//your code;
}
}
 
Share this answer
 
Comments
Ankur\m/ 15-Oct-13 6:07am    
And how will this correct the issue?
JL_Coder 15-Oct-13 6:11am    
OK. I added whole the code. Kindly see it. I also tried isPostback property, it didn't work. I also tried javascript which you can see in code in comments.
Salman622 15-Oct-13 9:15am    
write your code in if block not in else part
Salman622 15-Oct-13 9:15am    
write your code in if block not in else part

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900