By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

ItemCommand event not firing from a dynamic user control,feeling puzzled

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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


This is a page lifecycle issue. Can you post your code?

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com


Nov 19 '05 #2

P: n/a


*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #3

P: n/a


namespace HelpDeskApp.UserControls
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
/// <summary>
/// Summary description for Inbox.
/// </summary>
public class Inbox : System.Web.UI.UserControl
{
string connString =
"SERVER=JUNZHE;DATABASE=HelpDesk;trusted_connectio n=yes;UID=sa;PWD=sa";
protected System.Web.UI.HtmlControls.HtmlInputHidden hidvar;
protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.WebControls.Button idBtnPostBack;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
string cmdText = "SELECT ImageId,RecDate,RecTime,Filename FROM
dtImageDetails";

public event GridItemClickEventHandler GridClick;

// An event is a special property bound to a delegate. A delegate is a
reference to a method signature.
// An instance of a delegate is actually a pointer to a function with
a well-known signature.
// The .NET Framework
// provides a general-purpose delegate for event handlers—the
EventHandler class.
// let's define a custom delegate and a custom event data structure.

public delegate void GridItemClickEventHandler(object sender,
DataGridCommandEventArgs e);

/*private void Page_Init(object sender, System.EventArgs e)
{
DataTable data = ExecuteQuery(cmdText, connString);
DataGrid1.DataSource = data;
DataGrid1.DataBind();
}
*/
private void Page_Load(object sender, System.EventArgs e)
{

hidvar.Attributes.Add("onpropertychange",
Page.GetPostBackEventReference(idBtnPostBack));

// wire the item data bound and button events
// this.DataGrid1.ItemDataBound +=
// new DataGridItemEventHandler(this.DataGrid1_ItemDataBo und);

// Run the query and get some data to display

// Bind the data to the grid
DataTable data = ExecuteQuery(cmdText, connString);
DataGrid1.DataSource = data;
DataGrid1.DataBind();
// Put user code to initialize the page here
}

DataTable ExecuteQuery(string cmdText, string connString)
{
SqlDataAdapter adapter = new SqlDataAdapter(cmdText, connString);
DataTable data = new DataTable();
adapter.Fill(data);
return data;
}

private void idBtnPostBack_Click(object sender, System.EventArgs e)
{

Image1.Visible = true;
Image1.ImageUrl = "C:\\Documents and
Settings\\Ecompex\\Desktop\\imgout.jpg";

}
#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.idBtnPostBack.Click += new
System.EventHandler(this.idBtnPostBack_Click);
this.DataGrid1.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler (this.itemcreated_hand
ler);
this.DataGrid1.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void itemcreated_handler(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
TableCell dischargeCell = e.Item.Cells[2];
dischargeCell.Attributes["onmouseover"]="javascript:showImage();";
}

private void ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{
if (GridClick != null)
GridClick(this, e);

}


}
}

above is the code of the user control that contains the datagrid
DataGrid1,which has a button column with linkbuttons,i gave the command
name as select,what seems to happen is that when I click on the button
so as to fire itemcommand event the postback to the server seems to
suppress it,if i put datagrid databinding in !postback the datagrid will
not diplay,if i can fire itemcommand i have an event handler which gets
invoked and puts a new tab in the menu by doing arraylist.add and a new
different user control being loaded,I dont need the grid once the button
is clicked in the grid.
*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #4

P: n/a
JJ
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

Jan 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.