Dukster131
asked on
C# Dynamically Created Control Event Handler not working
I have a dynamically created button control with an EventHandler in C#. The control is generated, but when I click the button it does not fire the event handler. Can anyone give me some suggestions for this?
TableRow r2 = new TableRow();
TableCell c2 = new TableCell();
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Submit";
//btnSubmit.CausesValidation = true;
btnSubmit.Click += new EventHandler(btnSubmit_Click);
c2.Controls.Add(btnSubmit);
r2.Cells.Add(c2);
Table1.Rows.Add(r2);
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblResponse.Text = "Button Clicked";
lblResponse.Visible = true;
}
This explains how to handle event for dinamically created objects
http://weblogs.asp.net/ksamaschke/archive/2003/02/18/2595.aspx
http://weblogs.asp.net/ksamaschke/archive/2003/02/18/2595.aspx
ASKER
this code is generating a web application form. I did not see anything in the above two articles that would explain why this eventHandler is not working.
this is part of the second link
What, if there was a chance to react on the button's click? Even if this reaction was not handled by the button's eventhandler? Look at this:
' Registering a hidden field in the Page
Page.RegisterHiddenField(M e.UniqueID & _
"_buttonClicked", "")
' Defining the Button
Dim objButton As New Button
objButton.Text = "Click me!"
' Here's the trick... :)
objButton.Attributes.Add(" onClick", _
"document.forms[0]." & Me.UniqueID & _
"_buttonClicked.value='" & _
objButton.UniqueID & "';document.forms[0].submi t();")
' Adding the button to the page
Page.Controls.Add(objButto n)
What was done here? Really simple: We added a JavaScript to the button which sets a value in the hidden field. On PostBack we're able to track this hidden field and detect, wheter the button was clicked or not:
' Use this snippet on PostBack
If Page.IsPostBack Then
If Request.Item(Me.UniqueID & _
"_buttonClicked").Length > 0 Then
Response.Write("Button " & _
Request.Item(Me.UniqueID & _
"_buttonClicked") & _
" was clicked!")
End If
End If
What, if there was a chance to react on the button's click? Even if this reaction was not handled by the button's eventhandler? Look at this:
' Registering a hidden field in the Page
Page.RegisterHiddenField(M
"_buttonClicked", "")
' Defining the Button
Dim objButton As New Button
objButton.Text = "Click me!"
' Here's the trick... :)
objButton.Attributes.Add("
"document.forms[0]." & Me.UniqueID & _
"_buttonClicked.value='" & _
objButton.UniqueID & "';document.forms[0].submi
' Adding the button to the page
Page.Controls.Add(objButto
What was done here? Really simple: We added a JavaScript to the button which sets a value in the hidden field. On PostBack we're able to track this hidden field and detect, wheter the button was clicked or not:
' Use this snippet on PostBack
If Page.IsPostBack Then
If Request.Item(Me.UniqueID & _
"_buttonClicked").Length > 0 Then
Response.Write("Button " & _
Request.Item(Me.UniqueID & _
"_buttonClicked") & _
" was clicked!")
End If
End If
ASKER
Wow. That seems really convoluted to just get the EventHandler for the Click event. Also I need to know how to do this for a radio list that is generated to determine which button was clicked and make a textbox visible or invisible on each row - which could be up to 53 rows of dynamically generated radio lists. I don't think the solution above is going to work for that. I'm just wondering why the following does not work.
btnSubmit.Click += new EventHandler(btnSubmit_Cli ck);
and then do whatever I need to do in the btnSumbit_Click event.
btnSubmit.Click += new EventHandler(btnSubmit_Cli
and then do whatever I need to do in the btnSumbit_Click event.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, that is what I needed to know. I did have it in a !IsPostBack. Once I took it out of there it worked. I will look at the video you have suggested - just wish it was in c#. This submit button was out of loop that I am running on generating the rows. However, within the loop I have some radioLists that are being generated for each row with a textbox for comments. I didn't know if you might have an idea on adding the EventHandler for the RadioLists. I'll put what I have below.
//Add a Radio Button List
RadioButtonList rdbtnList = new RadioButtonList();
rdbtnList.ID = "rdList" + count;
rdbtnList.Items.Add(new ListItem("Green", "Green"));
rdbtnList.Items.Add(new ListItem("Amber", "Amber"));
rdbtnList.Items.Add(new ListItem("Red", "Red"));
rdbtnList.SelectedIndex = 0;
rdbtnList.RepeatDirection = RepeatDirection.Horizontal;
c.Controls.Add(rdbtnList);
r.Cells.Add(c);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm not sure I'm following you on casting to Button. Here is what I came up with maybe you can point me in the right direction:
rdbtnList.SelectedIndexCha nged += new EventHandler(this.rdbtnLis t_Selected IndexChang ed);
So I created the Event Handler. I'm just not sure how to capture which radio button was selected and the value of it.
protected void rdbtnList_SelectedIndexCha nged(objec t sender, EventArgs e)
{
lblResponse.Text = "Button Clicked ";
lblResponse.Visible = true;
}
rdbtnList.SelectedIndexCha
So I created the Event Handler. I'm just not sure how to capture which radio button was selected and the value of it.
protected void rdbtnList_SelectedIndexCha
{
lblResponse.Text = "Button Clicked ";
lblResponse.Visible = true;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok. My problem is not generating the different radiobuttonlists. My problem is finding out which radio button has been selected for each row. there are three buttons with values of Green Amber and Red. I can generate the number of rows I need - one for each computer. So if there are 50 computers and I have all the radio lists generated and I want to get the values for each one, how do I loop through those to get the values when they click on a submit button. Also I would like to make a textbox visible when they choose Red or Amber for any computer on the line for that computer. I already have the textboxes being generated as invisible so I just need them to appear when Amber or Green is chosen for that computer.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok. I'm getting close. I'm not finding the Textbox though. Here is what I have
//Add a TextBox
TextBox txtReason = new TextBox();
txtReason.ID = "Reason" + j.ToString();
txtReason.Columns = 50;
txtReason.TextMode = TextBoxMode.MultiLine;
txtReason.Rows = 3;
txtReason.Visible = false;
c.Controls.Add(txtReason);
r.Cells.Add(c);
Then on the Selected Index Changed event it always gives me a null value for tb below.
protected void rdbtnList_SelectedIndexCha nged(objec t sender, EventArgs e)
{
RadioButtonList list = sender as RadioButtonList;
if (list != null)
{
// Get the row number from the end of the list's ID
string rownum = list.ID.Substring(6);
// Use the row number to get the associated text box
TextBox tb = FindControl("Reason" + rownum) as TextBox;
// Show or hide the text box as appropriate
if (tb != null)
{
if (list.SelectedValue == "Amber" || list.SelectedValue == "Red")
tb.Visible = true;
else
tb.Visible = false;
}
}
}
//Add a TextBox
TextBox txtReason = new TextBox();
txtReason.ID = "Reason" + j.ToString();
txtReason.Columns = 50;
txtReason.TextMode = TextBoxMode.MultiLine;
txtReason.Rows = 3;
txtReason.Visible = false;
c.Controls.Add(txtReason);
r.Cells.Add(c);
Then on the Selected Index Changed event it always gives me a null value for tb below.
protected void rdbtnList_SelectedIndexCha
{
RadioButtonList list = sender as RadioButtonList;
if (list != null)
{
// Get the row number from the end of the list's ID
string rownum = list.ID.Substring(6);
// Use the row number to get the associated text box
TextBox tb = FindControl("Reason" + rownum) as TextBox;
// Show or hide the text box as appropriate
if (tb != null)
{
if (list.SelectedValue == "Amber" || list.SelectedValue == "Red")
tb.Visible = true;
else
tb.Visible = false;
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah, I have all of the controls with the same counter so if it works for one it should work for all of them. If I view source code it is showing a Reason1 etc. for the textboxes. I remembered something I had learned before and tried this and it seemed to work with the following code.
protected void rdbtnList_SelectedIndexCha nged(objec t sender, EventArgs e)
{
RadioButtonList list = sender as RadioButtonList;
if (list != null)
{
// Get the row number from the end of the list's ID
string rownum = list.ID.Substring(6);
// Use the row number to get the associated text box
string strTextBox = "Reason" + rownum;
string strReason = "lblReason" + rownum;
TextBox tb = (TextBox)list.Parent.Paren t.FindCont rol(strTex tBox) as TextBox;
Label lblReason = (Label)list.Parent.Parent. FindContro l(strReaso n) as Label;
// Show or hide the text box as appropriate
if (tb != null)
{
if (list.SelectedValue == "Amber" || list.SelectedValue == "Red")
{
tb.Visible = true;
lblReason.Visible = true;
}
else
{
tb.Visible = false;
lblReason.Visible = false;
}
}
}
}
Thanks for all the help I think I can figure the rest out.
protected void rdbtnList_SelectedIndexCha
{
RadioButtonList list = sender as RadioButtonList;
if (list != null)
{
// Get the row number from the end of the list's ID
string rownum = list.ID.Substring(6);
// Use the row number to get the associated text box
string strTextBox = "Reason" + rownum;
string strReason = "lblReason" + rownum;
TextBox tb = (TextBox)list.Parent.Paren
Label lblReason = (Label)list.Parent.Parent.
// Show or hide the text box as appropriate
if (tb != null)
{
if (list.SelectedValue == "Amber" || list.SelectedValue == "Red")
{
tb.Visible = true;
lblReason.Visible = true;
}
else
{
tb.Visible = false;
lblReason.Visible = false;
}
}
}
}
Thanks for all the help I think I can figure the rest out.
http://support.microsoft.com/kb/317794