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