Hi,
I got a similar problem. Mine is a button control which I could not get the
click event to fire off.
protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
if (IsPostBack)
{
this.InitializeManualComponent();
}
}
private void InitializeManualComponent()
{
populatephoto();
if(!IsPostBack)
{
//first time initialization
Cache["PageNo"] = 1;
populateWebpage();
}
else
{
postbackupdate();
}
//populate web page with current pageindex
}
//Set page index from view state
private void postbackupdate()
{
//subsequent update
try
{
int tempint = (int)Cache["PageNo"];
this.ConfirmPage (ref tempint);
this.pageindex = tempint;
}
catch
{
this.pageindex = 1;
}
populateWebpage();
}
private void populatephoto()
{
this function populate a arraylist (DataStoreArray)
}
private void populateWebpage()
{
try
{
foreach (HyperLink[] ar in (ArrayList)(DataStoreArray[this.pageindex-1]))
{
System.Web.UI.HtmlControls.HtmlTableRow tr1 = new HtmlTableRow();
System.Web.UI.HtmlControls.HtmlTableRow tr2 = new HtmlTableRow();
this.phototable.Align = "Top";
tr1.Align = "Top";
tr2.Align = "Top";
foreach(HyperLink hl in ar)
{
HtmlTableCell tc1 = new HtmlTableCell();
HtmlTableCell tc2 = new HtmlTableCell();
if (hl.Text !="")
{
System.Web.UI.HtmlControls.HtmlImage hi = new HtmlImage();
hi.Src = hl.ImageUrl;
hi.Height = 50;
hi.Width = 70;
tc1.Controls.Add(hi);
System.Web.UI.WebControls.Button wb = new
System.Web.UI.WebControls.Button();
wb.Text = hl.Text;
wb.BorderStyle = BorderStyle.None;
wb.BackColor = System.Drawing.Color.White;
wb.Click += new EventHandler(Enlarge);
tc2.Controls.Add(wb);
}
tr1.Cells.Add(tc1);
tr2.Cells.Add(tc2);
}
this.phototable.Rows.Add(tr1);
this.phototable.Rows.Add(tr2);
}
}
catch (Exception error)
{
this.Page.Response.Write(this.pageindex.ToString() );
this.Page.Response.Write(error.ToString());
}
}
public void Enlarge (object sender, EventArgs e)
{
System.Web.UI.WebControls.Button wb =
(System.Web.UI.WebControls.Button)sender;
string url = (string)this.picUrl[wb.Text];
largepic.Src = url;
buttonlogic ();
}
#endregion
#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();
if(!IsPostBack)
{
this.InitializeManualComponent();
}
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.firstpage.Click += new System.EventHandler(this.firstpage_Click);
this.prevpage.Click += new System.EventHandler(this.prevpage_Click);
this.nextpage.Click += new System.EventHandler(this.nextpage_Click);
this.lastpage.Click += new System.EventHandler(this.lastpage_Click);
this.prev.Click += new System.EventHandler(this.prev_Click);
this.Next.Click += new System.EventHandler(this.Next_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//these are the navigational events
//prev and next event is the photo navigator
#region Navigational Events
private void prev_Click(object sender, System.EventArgs e)
{
for(int cnt=0; cnt<this.indexer.Count; cnt++)
{
if ((string)this.picUrl[(string)this.indexer[cnt]] == largepic.Src)
{
if (cnt>0)
{
largepic.Src = (string)this.picUrl[(string)this.indexer[cnt-1]];
}
buttonlogic ();
break;
}
}
}
private void Next_Click(object sender, System.EventArgs e)
{
for(int cnt=0; cnt<this.indexer.Count; cnt++)
{
if ((string)this.picUrl[(string)this.indexer[cnt]] == largepic.Src)
{
if (cnt+1<this.indexer.Count)
{
largepic.Src = (string)this.picUrl[(string)this.indexer[cnt+1]];
}
buttonlogic ();
break;
}
}
}
private void buttonlogic ()
{
if (largepic.Src == (string)this.picUrl[(string)this.indexer[0]])
{
this.prev.Enabled = false;
this.Next.Enabled = true;
}
else if (largepic.Src ==
(string)this.picUrl[(string)this.indexer[this.indexer.Count-1]])
{
this.Next.Enabled = false;
this.prev.Enabled = true;
}
else
{
this.prev.Enabled = true;
this.Next.Enabled = true;
}
}
private void firstpage_Click(object sender, System.EventArgs e)
{
this.pageindex = 1;
updatepagedisplay();
ManageNavButton();
}
private void prevpage_Click(object sender, System.EventArgs e)
{
string pagedisplayvalue = this.pagedisplay.Text;
this.ConfirmNumber(ref pagedisplayvalue);
int displaypage = System.Convert.ToInt32(pagedisplayvalue);
--displaypage;
this.ConfirmPage(ref displaypage);
this.pageindex = displaypage;
updatepagedisplay();
ManageNavButton();
}
private void nextpage_Click(object sender, System.EventArgs e)
{
string pagedisplayvalue = this.pagedisplay.Text;
this.ConfirmNumber(ref pagedisplayvalue);
int displaypage = System.Convert.ToInt32(pagedisplayvalue);
++displaypage;
this.ConfirmPage(ref displaypage);
this.pageindex = displaypage;
updatepagedisplay();
ManageNavButton();
}
private void lastpage_Click(object sender, System.EventArgs e)
{
this.pageindex = this.maxindex;
updatepagedisplay();
ManageNavButton();
}
private void ManageNavButton()
{
if(this.pageindex == 1)
{
this.prevpage.Enabled = false;
this.nextpage.Enabled = true;
this.lastpage.Enabled = true;
this.firstpage.Enabled = false;
}
else if (this.pageindex == this.maxindex)
{
this.prevpage.Enabled = true;
this.nextpage.Enabled = false;
this.lastpage.Enabled = false;
this.firstpage.Enabled = true;
}
else
{
this.prevpage.Enabled = true;
this.nextpage.Enabled = true;
this.lastpage.Enabled = true;
this.firstpage.Enabled = true;
}
}
private void updatepagedisplay()
{
this.cleartable();
populateWebpage();
HyperLink []hlarray =
(HyperLink[])((ArrayList)(this.DataStoreArray[this.pageindex-1]))[0];
this.largepic.Src=hlarray[0].ImageUrl;
this.pagedisplay.Text = System.Convert.ToString(this.pageindex);
Cache["PageNo"] = this.pageindex;
//ViewState["PageNo"] = this.pageindex;
}
private void cleartable()
{
this.phototable.Rows.Clear();
}
#endregion
private void ConfirmNumber(ref string pagedisplayvalue)
{
char[] tempchararray = pagedisplayvalue.ToCharArray();
bool isnum = true;
foreach (char ch in tempchararray)
{
if (!Char.IsNumber(ch))
{
isnum = false;
break;
}
}
if (!isnum)
{
pagedisplayvalue = "1";
}
}
private void ConfirmPage (ref int pagenumber)
{
if (pagenumber > this.maxindex)
{
pagenumber = this.maxindex;
}
if(pagenumber < 1)
{
pagenumber = 1;
}
}
// <script language = "Javascript">
// function PhotoClick(value, value1)
// {
// obj = document.getElementById("PhotoDisplay1_largepic");
// obj.src = value;
// }
// </script>
private void EmbedJavaScript()
{
System.Text.StringBuilder sbMain = new System.Text.StringBuilder();
sbMain.Append("<script language = \"Javascript\">\n");
sbMain.Append("function PhotoClick(value)\n");
sbMain.Append("{\n");
sbMain.Append("obj =
document.getElementById('"+this.largepic.ClientID+ "');\n");
sbMain.Append("obj.src = value;\n");
sbMain.Append("}\n");
sbMain.Append("</script>\n");
Page.RegisterClientScriptBlock("Functions", sbMain.ToString());
}
#region remove
#endregion
}
}
"EvelynAnd Ethan" wrote:
Hi,
ItemCommand event not firing from a dynamic user control ,WHERE A
DATAGRID HAS BUTTON,when i click on the linkbutton first time the
itemcommand event doesnt fire,second time event fires up
any answers??
Regards,
Mandar