473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
4 4126
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


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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
0
by: ChrisB | last post by:
I'm attempting to open a new window from a LinkButton in a DataGrid. I can set a session variable in the ItemCommand event for the LinkButton like so: // this is used to handle the ItemCommand...
1
by: ronniek | last post by:
Hi, I'm trying to load a WebUserControl (.ascx) into a PlaceHolder but I keep getting an exception that the Page Tree ViewState is modified. If I load the WebUserControl by the designer (not...
2
by: John Ninan | last post by:
I am creating Dynamic Usercontrol in Asp.net application. In this application I have a combobox(aspx Page). Which contains various items. Based on item selected I am dynamically populating...
0
by: EvelynAnd Ethan | last post by:
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 ...
2
by: Developer_Software | last post by:
Thanks in advance to anyone who can help :) I've got a placeholder control WITHIN A USER CONTROL that has its contents dynamically added and removed at runtime by a regular .aspx page. At...
1
by: Stanley Cheung | last post by:
Dear all, how can I pass a parameter to dynamic user control? As I know we can pass parameter into traditional user control. e.g: add pageid into user control and write a property from user...
2
by: RSH | last post by:
Hi, I have been trying to follow a tutorial on raising an event from a user control and then handling it in the parent page. The article is a little vague so I believe I have the code in...
0
by: Eniac | last post by:
Hi, I've been working on a custom user control that needs to be modified and the validation is causing me headaches. The control used to generate a table of 4 rows x 7 columns to display all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.