In this example we are learning about the following features related to web-controls.

  • IsPostBack and PostBackUrl
  • events raised by the server control

Page..::.IsPostBack : Gets a value indicating whether the Page is being loaded in response to a client postback, or if it is being loaded and accessed for the first time.

Control_IsPostBack : Gets a value indicating whether the Control is being loaded in response to a client postback, or if it is being loaded and accessed for the first time.

PostBackUrl: gets the address of a page

protected void LB1_Click(object sender, EventArgs e)
{
label_clear();
Label1.Text += sender.ToString();

}
protected void IB1_Click(object sender, ImageClickEventArgs e)
{
label_clear();
Label1.Text += sender.ToString() + "<br/>";
Label1.Text += "X cord: " + e.X.ToString() + " Y Cord : " + e.Y.ToString();

}

Step 1

Step 2: Copy the code from default and paste in this page to save coding time

<%@ Page Language="C#" debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
void label_clear()
{
Label1.Text = "";
}
protected void LB1_Click(object sender, EventArgs e)
{
label_clear();
Label1.Text += sender.ToString();

}
protected void IB1_Click(object sender, ImageClickEventArgs e)
{
label_clear();
Label1.Text += sender.ToString() + "<br/>";
Label1.Text += "X cord: " + e.X.ToString() + " Y Cord : " + e.Y.ToString();

}
void Page_load()
{
string str1 = "<br/>-------------------------<br/>";
//image control
Control i_control = FindControl("IB1");
//linkl button cotrol
Control b_control = FindControl("LB1");
if (!IsPostBack)
{
//load this page if it not IsPostBack
if (i_control != null)
{
Control p_control = i_control.Parent;
Control g_control = p_control.Parent;
IB1.Focus();
Label1.Text += " Load activation image <br/> ";
Label1.Text += "control type :" + i_control.GetType().ToString();
Label1.Text += "<br/>";
Label1.Text += "Parent type :" + p_control.GetType().ToString();
Label1.Text += "<br/>";
Label1.Text += "Root type :" + g_control.GetType().ToString();
}

if (b_control != null)
{
Control p_control = b_control.Parent;
Control g_control = p_control.Parent;
Label1.Text += str1;
Label1.Text += " Load activation button <br/> ";
Label1.Text += "control type :" + b_control.GetType().ToString();
Label1.Text += "<br/>";
Label1.Text += "Parent type :" + p_control.GetType().ToString();
Label1.Text += "<br/>";
Label1.Text += "Root type :" + g_control.GetType().ToString();
}
//str1 =
}

}
void page_reloaded(object sender, EventArgs e)
{
label_clear();
string str1 = "<br/>-------------------------<br/>";
//image control
Control i_control = FindControl("IB1");
//linkl button cotrol
Control b_control = FindControl("LB1");
if (IsPostBack)
{
//string str1;
if (i_control != null)
{
Control p_control = i_control.Parent;
Control g_control = p_control.Parent;
IB1.Focus();
Label1.Text += " Reloaded Image Control <br/> ";
Label1.Text += "control type :" + i_control.GetType().ToString();
Label1.Text += "<br/>";
Label1.Text += "Parent type :" + p_control.GetType().ToString();
Label1.Text += "<br/>";
Label1.Text += "Root type :" + g_control.GetType().ToString();
}

if (b_control != null)
{
Control p_control = b_control.Parent;
Control g_control = p_control.Parent;
Label1.Text += str1;
Label1.Text += " Reload Button Control <br/> ";
Label1.Text += "control type :" + b_control.GetType().ToString();
Label1.Text += "<br/>";
Label1.Text += "Parent type :" + p_control.GetType().ToString();
Label1.Text += "<br/>";
Label1.Text += "Root type :" + g_control.GetType().ToString();
}
//str1 =
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>mm.cont6</title>
</head>
<body>The page shows how to sue link-button, image control, Autopostback=True
<br />Manipulating Div features using style in property pane:&nbsp; width: 200px; font-family: 'Book Antiqua'; background-color: #C0C0C0
<br />
The cordinates indicates the mouse cursors on the image button, meaning where on
the image you are clicking.<br />
<form id="form1" runat="server">
<div style="width: 178px; font-family: 'Book Antiqua'; background-color: #C0C0C0;" >
<asp:LinkButton runat="server" ID="LB1" Text="Link Me" onclick="LB1_Click"
BorderColor="#CC6600" BorderStyle="Solid" Height="25px" PostBackUrl="~/Find_control.aspx"
/>&nbsp;&nbsp;&nbsp;
<asp:ImageButton ID="IB1" runat="server" BorderColor="#CC33FF"
BorderStyle="Solid" BorderWidth="4px" Height="21px" ImageUrl="~/chick.gif"
Width="32px" ImageAlign="Bottom" onclick="IB1_Click" PostBackUrl="~/Find_control.aspx"
/>
<br />
<asp:Button ID="Button1" runat="server" Text="Re-Load" OnClick="page_reloaded" PostBackUrl="~/Find_control.aspx" />
<asp:Button ID="Button2" runat="server" Text="Go-Back" OnClick="page_reloaded" PostBackUrl="~/Default.aspx" />

<br />
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/Default.aspx">HyperLink</asp:HyperLink>

</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
 

Run time ananlysis of IsPastBack and PostBackUrl

The default load page is Default.aspx, ( how we get that Link )

Now void Page_load() is loaded by while page is being loaded to a web-browser, therefore if (!IsPostBack), this condition is to be true, therefore all the messages will printed accordingly.

Please click on Hyperlink, note the path/address is shown at the task bar panel.

Page is loaded with void Page_load(), now try to reload

Note Re-Load, directs to self, as show in this code snippet PostBackUrl="~/Find_control.aspx";

The above action will initiate the function void page_reloaded(object sender, EventArgs e), that would need two built in parameters to catch the events

What if you don't provide the parameter ? Below I created a condition, with no parameter, and found that during re-building up the  solution, the compiler reported two errors.

Error 1 No overload for 'page_reloaded' matches delegate 'System.EventHandler' C:\inetpub\wwwroot\aspnet.35\mm.cont6\Find_control.aspx 92 30 http://manas6/aspnet.35/mm.cont6/
Error 2 No overload for 'page_reloaded' matches delegate 'System.EventHandler' C:\inetpub\wwwroot\aspnet.35\mm.cont6\Find_control.aspx 93 30 http://manas6/aspnet.35/mm.cont6/
 

Back to Re-Load function, now you may try other functions and events by clicking link-button,  image button or Hyperlink.

Link Button, runs JavaScript function that links up the events to the server side language (here it is C# ).

The image button runs with different parameters as you might have noted in the code layout.

Debugging

Note the underlying machineries that represents the image button's position