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
{
}
}
}