I have the following code and I'm trying to set an event dynamicly in the foreach statement...
SqlConnection
conn =newSqlConnection(ConfigurationManager.AppSettings["BvtQueueConnectString2"]);SqlCommand select =newSqlCommand("select top 100 b.jobid as [Job ID], bq.timesubmitted as [Time Submitted], b.timereceived as [Time Received], bq.requestid as [Request ID], bq.timecompleted as [Time Completed], bq.buginfoid as [Bug Info ID], e.eventid as [Event ID], bq.closingeventid as [Closing Event ID], et.eventtype as [Event Type], e.eventtime as [State Start Time], l.twolettercode as [Language], p.projectname as [Project Name], p.packagedesignation as [Package Designation] from bvtjobs b inner join bvtrequestsnew bq on b.jobid = bq.jobid inner join eventlog e on bq.requestid = e.bvtrequestid inner join eventtypes et on e.eventtype = et.eventtypeid inner join languages l on bq.targetlanguage = l.langid inner join projects p on bq.targetplatform = p.projectid order by b.jobid asc", conn);SqlDataAdapter da =newSqlDataAdapter(select);DataSet ds =newDataSet();conn.Open();
da.Fill(ds);
conn.Close();
Table table =newTable();foreach (DataRow drin ds.Tables[0].Rows)
{
TableRow tr =newTableRow();for (int i = 0; i < dr.ItemArray.Length; i++){
TableCell tc =newTableCell();tc.Text = dr.ItemArray.GetValue(i).ToString();
tc.BackColor =
Color.White;if (dr.ItemArray.GetValue(8).ToString() =="COMPLETE"){
tc.BackColor =
Color.BlueViolet;}
tr.Cells.Add(tc);
}
table.Rows.Add(tr);
}
foreach (TableRow tablerowin table.Rows){
TableCell newtc =newTableCell();Button mybutton =newButton();newtc.Controls.Add(mybutton);
mybutton.Text =
"details";//set the event here
tablerow.Cells.Add(newtc);
}
Panel1.Controls.Add(table);
DataBind();
I am assuming you have added a button dynamically, and you want to create a handler for this button.
First you need to create a function to do what you want, e.g.
Sub myButtonClicked (sender, args)
....
End sub
Then you need to tell asp.net that this is the function to run when the button is clicked. In your code, before you add the button to the tablecell, write:
AddHandler myButton.click, AddressOg myButtonClicked
A little warning though - this may not work. This is because you have to create any dynamic controls everytime the page load. Have a read ofthis page which should help you.
|||thanks for the response.
The solution that I have found is to add the eventhandler and specify what you want to do init. In this case redirect towww.google.com :
protected
void Page_Load(object sender,EventArgs e){
SqlConnection conn =newSqlConnection(ConfigurationManager.AppSettings["BvtQueueConnectString2"]);SqlCommand select =newSqlCommand("select top 100 b.jobid as [Job ID], bq.timesubmitted as [Time Submitted], b.timereceived as [Time Received], bq.requestid as [Request ID], bq.timecompleted as [Time Completed], bq.buginfoid as [Bug Info ID], e.eventid as [Event ID], bq.closingeventid as [Closing Event ID], et.eventtype as [Event Type], e.eventtime as [State Start Time], l.twolettercode as [Language], p.projectname as [Project Name], p.packagedesignation as [Package Designation] from bvtjobs b inner join bvtrequestsnew bq on b.jobid = bq.jobid inner join eventlog e on bq.requestid = e.bvtrequestid inner join eventtypes et on e.eventtype = et.eventtypeid inner join languages l on bq.targetlanguage = l.langid inner join projects p on bq.targetplatform = p.projectid order by b.jobid asc", conn);SqlDataAdapter da =newSqlDataAdapter(select);DataSet ds =newDataSet();conn.Open();
da.Fill(ds);
conn.Close();
Table table =newTable();foreach (DataRow drin ds.Tables[0].Rows)
{
TableRow tr =newTableRow();for (int i = 0; i < dr.ItemArray.Length; i++){
TableCell tc =newTableCell();tc.Text = dr.ItemArray.GetValue(i).ToString();
tc.BackColor =
Color.White;if (dr.ItemArray.GetValue(8).ToString() =="COMPLETE"){
tc.BackColor =
Color.BlueViolet;}
tr.Cells.Add(tc);
}
table.Rows.Add(tr);
}
foreach (TableRow tablerowin table.Rows){
TableCell newtc =newTableCell();Button mybutton =newButton();newtc.Controls.Add(mybutton);
mybutton.Text =
"details";mybutton.Click +=
newEventHandler(mybutton_Click);tablerow.Cells.Add(newtc);
}
Panel1.Controls.Add(table);
DataBind();
}
void mybutton_Click(object sender,EventArgs e){
Response.Redirect(
"http://www.google.com");}
No comments:
Post a Comment