!IsPostBack problem

G

Guest

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

Guest

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
 
G

Guest

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top