473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

!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.CommandArgument string when the LinkButton
is clicked. If I use if(!IsPostBack) as in the following code, the
MyLinkButton_Click 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_Click event fires and the
redirection is successful. Obviously, I don't want the Page_Load to have to
happen for MyLinkButton_Click event to fire. Thanks for any suggestions.
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 MyNamespace
{
/// <summary>
/// Summary description for test.
/// </summary>
public class MyWebPage: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;

private void MyLinkButton_Click(object sender, CommandEventArgs e)
{
string strMyArgument=e.CommandArgument.ToString();
Session.Add("MyArgument", strMyArgument);
Response.Redirect("another_page.aspx");
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack) //MyLinkButton_Click 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.Command +=new CommandEventHandler(MyLinkButton_Click);
string strCommandArgument = "MyArgument";
MyLinkButton.CommandArgument=strCommandArgument;
MyLinkButton.Text=MyString;

tCell.Controls.Add(MyLinkButton);

tRow.Cells.Add(tCell);
Table1.Rows.Add(tRow);
}
catch
{
}
}
}
Nov 16 '05 #1
2 7313
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.Page
{
protected System.Web.UI.WebControls.Table Table1;

private void MyLinkButton_Click(object sender, CommandEventArgs e)
{
string strMyArgument=e.CommandArgument.ToString();
Session.Add("MyArgument", strMyArgument);
Response.Redirect("another_page.aspx");
}

private void Page_Load(object sender, System.EventArgs e)
{

System.Web.UI.HtmlControls.HtmlForm myform =
(System.Web.UI.HtmlControls.HtmlForm)FindControl(" Form1");

Response.Write ("here");
Table Table1 = new Table();
Table1.Attributes.Add("runat","server");
TableRow tRow=new TableRow();
tRow.Attributes.Add("runat","server");
TableCell tCell = new TableCell();
tCell.Attributes.Add("runat","server");
string MyString="click here";
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Command +=new CommandEventHandler(MyLinkButton_Click);
string strCommandArgument = "MyArgument";
MyLinkButton.CommandArgument=strCommandArgument;
MyLinkButton.Text=MyString;
MyLinkButton.Attributes.Add("runat","server");
tCell.Controls.Add(MyLinkButton);

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

}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(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.CommandArgument string when the LinkButton
is clicked. If I use if(!IsPostBack) as in the following code, the
MyLinkButton_Click 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_Click event fires and the
redirection is successful. Obviously, I don't want the Page_Load to have to
happen for MyLinkButton_Click event to fire. Thanks for any suggestions.
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 MyNamespace
{
/// <summary>
/// Summary description for test.
/// </summary>
public class MyWebPage: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;

private void MyLinkButton_Click(object sender, CommandEventArgs e)
{
string strMyArgument=e.CommandArgument.ToString();
Session.Add("MyArgument", strMyArgument);
Response.Redirect("another_page.aspx");
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack) //MyLinkButton_Click 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.Command +=new CommandEventHandler(MyLinkButton_Click);
string strCommandArgument = "MyArgument";
MyLinkButton.CommandArgument=strCommandArgument;
MyLinkButton.Text=MyString;

tCell.Controls.Add(MyLinkButton);

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="test.aspx.cs" AutoEventWireup="false"
Inherits="MyNamespace.MyWebPage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>MyWebPage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.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.Page
{
protected System.Web.UI.WebControls.Table Table1;

private void MyLinkButton_Click(object sender, CommandEventArgs e)
{
string strMyArgument=e.CommandArgument.ToString();
Session.Add("MyArgument", strMyArgument);
Response.Redirect("another_page.aspx");
}

private void Page_Load(object sender, System.EventArgs e)
{

System.Web.UI.HtmlControls.HtmlForm myform =
(System.Web.UI.HtmlControls.HtmlForm)FindControl(" Form1");

Response.Write ("here");
Table Table1 = new Table();
Table1.Attributes.Add("runat","server");
TableRow tRow=new TableRow();
tRow.Attributes.Add("runat","server");
TableCell tCell = new TableCell();
tCell.Attributes.Add("runat","server");
string MyString="click here";
LinkButton MyLinkButton = new LinkButton();
MyLinkButton.Command +=new CommandEventHandler(MyLinkButton_Click);
string strCommandArgument = "MyArgument";
MyLinkButton.CommandArgument=strCommandArgument;
MyLinkButton.Text=MyString;
MyLinkButton.Attributes.Add("runat","server");
tCell.Controls.Add(MyLinkButton);

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

}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(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.CommandArgument string when the LinkButton
is clicked. If I use if(!IsPostBack) as in the following code, the
MyLinkButton_Click 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_Click event fires and the
redirection is successful. Obviously, I don't want the Page_Load to have to
happen for MyLinkButton_Click event to fire. Thanks for any suggestions.
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 MyNamespace
{
/// <summary>
/// Summary description for test.
/// </summary>
public class MyWebPage: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;

private void MyLinkButton_Click(object sender, CommandEventArgs e)
{
string strMyArgument=e.CommandArgument.ToString();
Session.Add("MyArgument", strMyArgument);
Response.Redirect("another_page.aspx");
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack) //MyLinkButton_Click 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.Command +=new CommandEventHandler(MyLinkButton_Click);
string strCommandArgument = "MyArgument";
MyLinkButton.CommandArgument=strCommandArgument;
MyLinkButton.Text=MyString;

tCell.Controls.Add(MyLinkButton);

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
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
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...
0
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...
4
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"...
6
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...
5
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...
1
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...
4
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,...
5
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.