473,802 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

!IsPostBack problem

Hi,
I'm having trouble with using !IsPostBack. I want to create a linkbutton
within a table when the page loads the first time and then have the page
redirect based on the LinkButton.Comm andArgument string when the LinkButton
is clicked. If I use if(!IsPostBack) as in the following code, the
MyLinkButton_Cl ick is never fired. If I remove the if(!IsPostBack) then when
the LinkButton is clicked the Page_Load is executed (as you would expect) and
after that procedure finishes, the MyLinkButton_Cl ick event fires and the
redirection is successful. Obviously, I don't want the Page_Load to have to
happen for MyLinkButton_Cl ick event to fire. Thanks for any suggestions.
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for test.
/// </summary>
public class MyWebPage: System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Tabl e Table1;

private void MyLinkButton_Cl ick(object sender, CommandEventArg s e)
{
string strMyArgument=e .CommandArgumen t.ToString();
Session.Add("My Argument", strMyArgument);
Response.Redire ct("another_pag e.aspx");
}

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!IsPostBack) //MyLinkButton_Cl ick will not fire if this line is
included
{
try
{
TableRow tRow=new TableRow();
TableCell tCell = new TableCell();
string MyString="click here";
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Co mmand +=new CommandEventHan dler(MyLinkButt on_Click);
string strCommandArgum ent = "MyArgument ";
MyLinkButton.Co mmandArgument=s trCommandArgume nt;
MyLinkButton.Te xt=MyString;

tCell.Controls. Add(MyLinkButto n);

tRow.Cells.Add( tCell);
Table1.Rows.Add (tRow);
}
catch
{
}
}
}
Nov 16 '05 #1
2 7340
hi patrcik

There were few bugs in the code that you posted. I executed your code,but
since you handled the error in try catch statement, I didnt get any error
displayed on the page. But when i removed I found the following bugs.

1) First of all you have not added the table to any control for it to
display. It should be placed under a FORM element at run time or you will get
the following error

Control '_ctl1' of type 'LinkButton' must be placed inside a form tag with
runat=server.

2) Secondly, if you use IsPostBack then your click even of the link button
woudl never get executed. It is so because unless the event is created for
the link button inside the Page_load, it cannot be invoked. If you
understand how the ASP page lifecycle you will undestand what i mean.
I am pasting the corrected code below. please have a look

public class WebForm2 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Tabl e Table1;

private void MyLinkButton_Cl ick(object sender, CommandEventArg s e)
{
string strMyArgument=e .CommandArgumen t.ToString();
Session.Add("My Argument", strMyArgument);
Response.Redire ct("another_pag e.aspx");
}

private void Page_Load(objec t sender, System.EventArg s e)
{

System.Web.UI.H tmlControls.Htm lForm myform =
(System.Web.UI. HtmlControls.Ht mlForm)FindCont rol("Form1");

Response.Write ("here");
Table Table1 = new Table();
Table1.Attribut es.Add("runat", "server");
TableRow tRow=new TableRow();
tRow.Attributes .Add("runat","s erver");
TableCell tCell = new TableCell();
tCell.Attribute s.Add("runat"," server");
string MyString="click here";
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Co mmand +=new CommandEventHan dler(MyLinkButt on_Click);
string strCommandArgum ent = "MyArgument ";
MyLinkButton.Co mmandArgument=s trCommandArgume nt;
MyLinkButton.Te xt=MyString;
MyLinkButton.At tributes.Add("r unat","server") ;
tCell.Controls. Add(MyLinkButto n);

tRow.Cells.Add( tCell);
Table1.Rows.Add (tRow);
myform.Controls .Add(Table1);

}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

}

Happy programming!!

pradeep TP


"patrick_a" wrote:
Hi,
I'm having trouble with using !IsPostBack. I want to create a linkbutton
within a table when the page loads the first time and then have the page
redirect based on the LinkButton.Comm andArgument string when the LinkButton
is clicked. If I use if(!IsPostBack) as in the following code, the
MyLinkButton_Cl ick is never fired. If I remove the if(!IsPostBack) then when
the LinkButton is clicked the Page_Load is executed (as you would expect) and
after that procedure finishes, the MyLinkButton_Cl ick event fires and the
redirection is successful. Obviously, I don't want the Page_Load to have to
happen for MyLinkButton_Cl ick event to fire. Thanks for any suggestions.
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for test.
/// </summary>
public class MyWebPage: System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Tabl e Table1;

private void MyLinkButton_Cl ick(object sender, CommandEventArg s e)
{
string strMyArgument=e .CommandArgumen t.ToString();
Session.Add("My Argument", strMyArgument);
Response.Redire ct("another_pag e.aspx");
}

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!IsPostBack) //MyLinkButton_Cl ick will not fire if this line is
included
{
try
{
TableRow tRow=new TableRow();
TableCell tCell = new TableCell();
string MyString="click here";
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Co mmand +=new CommandEventHan dler(MyLinkButt on_Click);
string strCommandArgum ent = "MyArgument ";
MyLinkButton.Co mmandArgument=s trCommandArgume nt;
MyLinkButton.Te xt=MyString;

tCell.Controls. Add(MyLinkButto n);

tRow.Cells.Add( tCell);
Table1.Rows.Add (tRow);
}
catch
{
}
}
}

Nov 16 '05 #2
Thanks for responding. I removed the Try-Catch block and did not get the
error that you mentioned. I think the table and its runat server attributes
are added to the form in the HTML page which I did not include in my original
post. The HTML is below. I guess my question is really more about the ASP
page lifecycle. Does this mean that if I cache the page then the cached
page will be served (until dropped from the cache) and the Page_Load will
only occur when the user clicks on the LinkButton ?
<%@ Page language="c#" Codebehind="tes t.aspx.cs" AutoEventWireup ="false"
Inherits="MyNam espace.MyWebPag e" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>MyWebPag e</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
<asp:Table id="Table1" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px" runat="server"
Width="300px" Height="70px"></asp:Table>
</form>
</body>
</HTML>

"pradeep" wrote:
hi patrcik

There were few bugs in the code that you posted. I executed your code,but
since you handled the error in try catch statement, I didnt get any error
displayed on the page. But when i removed I found the following bugs.

1) First of all you have not added the table to any control for it to
display. It should be placed under a FORM element at run time or you will get
the following error

Control '_ctl1' of type 'LinkButton' must be placed inside a form tag with
runat=server.

2) Secondly, if you use IsPostBack then your click even of the link button
woudl never get executed. It is so because unless the event is created for
the link button inside the Page_load, it cannot be invoked. If you
understand how the ASP page lifecycle you will undestand what i mean.
I am pasting the corrected code below. please have a look

public class WebForm2 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Tabl e Table1;

private void MyLinkButton_Cl ick(object sender, CommandEventArg s e)
{
string strMyArgument=e .CommandArgumen t.ToString();
Session.Add("My Argument", strMyArgument);
Response.Redire ct("another_pag e.aspx");
}

private void Page_Load(objec t sender, System.EventArg s e)
{

System.Web.UI.H tmlControls.Htm lForm myform =
(System.Web.UI. HtmlControls.Ht mlForm)FindCont rol("Form1");

Response.Write ("here");
Table Table1 = new Table();
Table1.Attribut es.Add("runat", "server");
TableRow tRow=new TableRow();
tRow.Attributes .Add("runat","s erver");
TableCell tCell = new TableCell();
tCell.Attribute s.Add("runat"," server");
string MyString="click here";
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Co mmand +=new CommandEventHan dler(MyLinkButt on_Click);
string strCommandArgum ent = "MyArgument ";
MyLinkButton.Co mmandArgument=s trCommandArgume nt;
MyLinkButton.Te xt=MyString;
MyLinkButton.At tributes.Add("r unat","server") ;
tCell.Controls. Add(MyLinkButto n);

tRow.Cells.Add( tCell);
Table1.Rows.Add (tRow);
myform.Controls .Add(Table1);

}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

}

Happy programming!!

pradeep TP


"patrick_a" wrote:
Hi,
I'm having trouble with using !IsPostBack. I want to create a linkbutton
within a table when the page loads the first time and then have the page
redirect based on the LinkButton.Comm andArgument string when the LinkButton
is clicked. If I use if(!IsPostBack) as in the following code, the
MyLinkButton_Cl ick is never fired. If I remove the if(!IsPostBack) then when
the LinkButton is clicked the Page_Load is executed (as you would expect) and
after that procedure finishes, the MyLinkButton_Cl ick event fires and the
redirection is successful. Obviously, I don't want the Page_Load to have to
happen for MyLinkButton_Cl ick event to fire. Thanks for any suggestions.
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for test.
/// </summary>
public class MyWebPage: System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Tabl e Table1;

private void MyLinkButton_Cl ick(object sender, CommandEventArg s e)
{
string strMyArgument=e .CommandArgumen t.ToString();
Session.Add("My Argument", strMyArgument);
Response.Redire ct("another_pag e.aspx");
}

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!IsPostBack) //MyLinkButton_Cl ick will not fire if this line is
included
{
try
{
TableRow tRow=new TableRow();
TableCell tCell = new TableCell();
string MyString="click here";
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Co mmand +=new CommandEventHan dler(MyLinkButt on_Click);
string strCommandArgum ent = "MyArgument ";
MyLinkButton.Co mmandArgument=s trCommandArgume nt;
MyLinkButton.Te xt=MyString;

tCell.Controls. Add(MyLinkButto n);

tRow.Cells.Add( tCell);
Table1.Rows.Add (tRow);
}
catch
{
}
}
}

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1304
by: Mathana g | last post by:
Hi, I have snippet as follows if(!IsPostBack) LoadPreRequisites(); I am getting IsPostBack as true even the page loads first time. What might be the problem
3
1697
by: Ravi | last post by:
Hi, I have a simple .aspx page where user enters data (e.g Name address, etc). At the end, he/she clicks Save button which Posts to the same page and in the Save button's click event I am saving the data into Database. Now after saving the details the same page gets rendered to the user. If the user refreshes the browser as its a post - back page the server is trying to store the values again. what are the different possible ways to avoid...
0
1306
by: Boris Zakharin | last post by:
I am using the .Net framework 1.1 and VS.NET 2003. I have some code where, for some reason, IsPostBack returns false messing up my code. I have a DataGrid, whose data I am trying to edit. Instead of using inline editing I have the code set a variable to the primary key of the row I'm going to edit and then displaying editting controls and filling them with data. Also I have code in the load event which clears the above variable when...
4
2522
by: N. Demos | last post by:
Hello, I'm learning ASP.NET, and am having a strange problem with some example code from the book I'm using. The code increments and displays the value stored in a session variable when the "Add" button is clicked. In addition, the session variable is reset to zero when the "Empty" button is pressed. The problem is if the value is non-zero and the page is reloaded the value is incremented. It appears as if the "Add" onClick event...
6
1474
by: RA | last post by:
btnAdd_Click does not get hit; if I have IsPostBack check in Page_load. If I don't have IsPostBack check; I am able to debug through btnAdd_Click. If I don't look for IsPostBack then it re-populates the table before doing anything; which I don't want. Following is the code. private sub page_load(.......) ' if ispostback = false then populatetable
5
1669
by: David Lozzi | last post by:
Hello, I have an interesting issue, so bear with me as I try to explain. I have a datalist posing as tabs for my application. And as each tab is clicked, a placeholder is then populated with the associated control. For Example: Details | Advanced | Configuration | Policies <placeholder> The jumping around works great. I can see each control when clicked. Here's
1
1253
by: VMI | last post by:
For some strange reason, when I click on a RadCombobox to open it, it automatically reloads the page but also runs the code within the !IsPostBack if/then statement. It's doing postback, but also reloading the entire page. Is there any way to avoid this? I've tried setting the different properties of the RadCombobox, but it still does the same thing. But apparently it happens with EnableLoadOnDemand equal to true. Any help is...
4
1756
by: gihope | last post by:
Hi, I have an ASP.NET 2.0 C# issue that has been troubling me for some time, and if someone could shed some light on this I would appreciate it. This seems to be a commonly themed question, although most relate to DataGrids and this is a slightly different scenario. This is completely hypothetical, but say I have a form for editing database information. This form contains two drop down boxes, one of which determines the content of the...
5
3862
by: BM | last post by:
I have a question that seems like it should have a simple answer, but I can't seem to find it by searching... Anyway, I'm trying to capture the IsPostBack event when I select an item within an UpdatePanel. I don't want items running during postback (list, grid population, etc) events - pretty standard: If IsPostBack Then Exit Sub But, since AJAX doesn't technically postback (?), the IsPostBack is coming back false...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.