Programmatically assigned eventhandler not showing up in HTML onClick attribute - c#

I'm currently having a problem with event handlers assigned in the code-behind not appearing in the HTML code of the .aspx page. Below is the code fragment that does that:
HtmlTable table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
foreach(XmlNode item in IDList)
{
LinkButton btn = new LinkButton();
btn.Text = item.InnerText;
btn.Click += new EventHandler(btn_Click);
HtmlTableCell cell = new HtmlTableCell();
cell.Controls.add(btn);
row.cells.add(cell);
}
table.rows.add(row);
Something like that, more or less. So when I run the page and inspected the buttons using FireBug, I noticed that the eventhandler names for the buttons created have been changed to the LinkButtons' unique ID(ct125, 126, 127, etc), I think, and not "btn_Click"
I'm hoping to hear if anyone else has faced this problem before and found a solution for it. Thanks.

your buttons might be using submit behavior for your buttons. Try setting "UseSubmitBehavior=false" for the buttons.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx

When creating controls dynamically, you should assign them Id also, otherwise asp.net will auto-assign them ids like ctl{somenumber}. Also, you need to recreate these controls on each postback, otherwise your eventhandlers will not work.

I was able to get this code to work as expected. I dynamically add 4 LinkButtons and wire the Click event to a function btn_Click. When I click each link, they call the btn_Click method server-side:
protected void Page_Load(object sender, EventArgs e)
{
HtmlTable table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
for (int loop = 1; loop < 5; loop++)
{
LinkButton btn = new LinkButton();
btn.Text = "Test " + loop.ToString();
btn.Click += new EventHandler(btn_Click);
HtmlTableCell cell = new HtmlTableCell();
cell.Controls.Add(btn);
row.Cells.Add(cell);
}
table.Rows.Add(row);
// Add the table to a placeholder.
phInputs.Controls.Add(table);
}
protected void btn_Click(object sender, EventArgs e)
{
// Do something to catch a breakpoint.
var x = 10;
}

Ok, I got it fixed, but I'm absolutely bewildered at why it got fixed this way.
{
HtmlTable table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
int btnIndex = 0;
foreach(XmlNode item in IDList)
{
LinkButton btn = new LinkButton();
btn.Text = item.InnerText;
btn.Click += new EventHandler(btn_Click);
btn.ID = "Btn_Page" + btnIndex.ToString();
HtmlTableCell cell = new HtmlTableCell();
cell.Controls.add(btn);
row.cells.add(cell);
}
table.rows.add(row);
}
protected void Btn_Page_0(object sender, EventArgs e)
{
whatever eventhandler code
}
When I inspected the HTML again, the names of the event handler assigned to the buttons are still being changed to its ID.
<a id="Btn_Page_0" href="javascript:_doPostback('Btn_Page_0','')">
So for each of those buttons, their event handlers are changed to Btn_Page_0, Btn_Page_1, Btn_Page_2, etc.
Strange thing is, they all go back to the Btn_Page_0 event handler when clicked, instead of btn_click
So.....yeah. Kinda boggles my mind why it worked like this, but it works anyhow...

Related

get code behind checkboxes in postback

I want to add some checkboxes at the beginning of every row in a table in the page_load: (I add them in a asp:placeholder)
protected void Page_Load(object sender, EventArgs e)
{
Table dtTable = new Table();
TableHeaderRow dtHeaderRow = new TableHeaderRow();
TableHeaderCell dtHeaderCheckbox = new TableHeaderCell();
dtHeaderCheckbox.Controls.Add(dtHeaderCkBox);
dtHeaderRow.Cells.Add(dtHeaderCheckbox);
foreach (DataColumn col in _ds.Tables[0].Columns)
{
TableHeaderCell dtHeaderCell = new TableHeaderCell();
dtHeaderCell.Text += col.ColumnName;
dtHeaderRow.Cells.Add(dtHeaderCell);
}
dtTable.Rows.Add(dtHeaderRow);
TableRow row;
for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
{
row = new TableRow();
TableCell dtCell = new TableCell();
CheckBox ckBox = new CheckBox();
ckBox.ID = "chkBox_" + _ds.Tables[0].Rows[i]["IDENTIFIER"].ToString();
ckBox.AutoPostBack = false;
ckBox.EnableViewState = false;
dtCell.Controls.Add(ckBox);
row.Cells.Add(dtCell);
for (int j = 0; j < _ds.Tables[0].Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Text = _ds.Tables[0].Rows[i][j].ToString();
row.Cells.Add(cell);
}
dtTable.Rows.Add(row);
}
phUnconfirmedDiv.Controls.Add(dtTable);
}
The problem is now, when the user press a submit button(and postback), I don't have access to my checkboxes:
protected void btnAccept_OnClick(object sender, EventArgs e)
{
List<CheckBox> chkList = new List<CheckBox>();
foreach (Control ctl in form1.Controls)
{
if (ctl is CheckBox)
{
if (ctl.ID.IndexOf("chkBox_") == 0)
{
chkList.Add((CheckBox)ctl);
}
}
}
ScriptManager.RegisterStartupScript(this, GetType(), "event", "alert('" + chkList.Count + "');", true);
}
Dynamically generated controls lost their state once they are rendered on view. And for you, to access them again in your code-behind, when your postbacks, you will have to re-create them and after that you will be able to manipulate them.
As far as getting the checked values of the checkboxes is concerned, you could try something like this. This might not be exact, should give an idea though.
This would be your check-box :
<input type="checkbox" id="yourId" name="selectedIds" value="someValue"/>
In your codebehind :
value = Request.Form["selectedIds"];
Hope this helps.
I now managed to solve this problem. Thanks all for your tips!
All checkboxes that are checked are sent through the postback. So the "easiest" way is to search for all parameters, sent in postback, that are beginning like "chkBox_" and then save them in a list/array. So I know which data should be updated in my database:
protected void btnAccept_OnClick(object sender, EventArgs e)
{
List<String> chkList = new List<String>();
// All checked checkboxes are sent via the postback. Save this parameters in a list:
foreach (string s in Request.Params.Keys)
{
if (s.ToString().IndexOf("chkBox_") == 0)
{
chkList.Add(s.ToString());
}
}
Is autopostback set to true on the user interface controls for the button? ei:
<telerik:RadButton runat="server" ID="rBtnRelease" Text="Release" Width="100px" OnClick="rBtnRelease_Click" AutoPostBack="False"/>
That's usually the common mistake when one tries to get data back and forth between the client and the server side.

Link button to display grid view asp.net dynamically

I have to display n grids, n is variable, then I dont know how many grids I'll have.
My problem is, I have to init this grids with Visible false and when click in a button show the grid specific for that button, then how can I link a button to a gridview?
My code that generate the grids:
foreach (List<DataRow> lst in grids)
{
dt = lst.CopyToDataTable();
GridView grv = new GridView();
grv.AlternatingRowStyle.BackColor = System.Drawing.Color.FromName("#cccccc");
grv.HeaderStyle.BackColor = System.Drawing.Color.Gray;
grv.ID = "grid_view"+i;
grv.Visible = false;
grv.DataSource = dt;
grv.DataBind();
Label lblBlankLines = new Label();
lblBlankLines.Text = "<br /><br />";
Label lblTipo = new Label();
string tipoOcorrencia = lst[0]["DESC_OCORRENCIA"].ToString();
tipoOcorrencia = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(tipoOcorrencia);
int quantidade = lst.Count;
lblTipo.Text = tipoOcorrencia + ": " + quantidade;
LinkButton lkBtn = new LinkButton();
lkBtn.ID = "link_button"+i;
lkBtn.Text = "+";
place_grids.Controls.Add(lblBlankLines);
place_grids.Controls.Add(lkBtn);
place_grids.Controls.Add(lblTipo);
place_grids.Controls.Add(grv);
place_grids.DataBind();
i++;
}
Thanks in advance.
Modify your foreach loop as below.
private void GenerateControls()
{
int i = 0;
foreach (List<DataRow> lst in grids)
{
dt = lst.CopyToDataTable();
GridView grv = new GridView();
grv.AlternatingRowStyle.BackColor = System.Drawing.Color.FromName("#cccccc");
grv.HeaderStyle.BackColor = System.Drawing.Color.Gray;
grv.ID = "grid_view" + i;
//grv.Visible = false;//Commented as the grid needs be generated on client side, in order to make it visible from JavaScript/jQuery
grv.Attributes.Add("style", "display:none;");
grv.DataSource = dt;
grv.DataBind();
//Adding dynamic link button
LinkButton lnkButton = new LinkButton();
lnkButton.Text = "button " + i;
//lnkButton.Click += new EventHandler(lnkButton_Click);
lnkButton.ID = "lnkButton" + i;
lnkButton.OnClientClick = "ShowGrid('" + grv.ClientID + "');";
Label lblTipo = new Label();
lblTipo.Text = "text " + i;
lblTipo.ID = "lbl" + i;
tempPanel.Controls.Add(lblTipo);
tempPanel.Controls.Add(grv);
tempPanel.Controls.Add(lnkButton);
tempPanel.DataBind();
i++;
}
}
Then you will have to add a link button click event as below, if you want server side event to fire. (Un-comment the line where event handler is assigned to link button.)
protected void lnkButton_Click(Object sender, EventArgs e)
{
LinkButton lnkButton = (LinkButton)sender;
String index = lnkButton.ID.Substring(lnkButton.ID.Length - 1);
GridView grv = (GridView)tempPanel.FindControl("grid_view" + index);
grv.Visible = true;
}
You will need to add all dynamically added controls in the Page_Init event for maintaining their state. Refer below links can be useful.
Dynamically Created Controls losing data after postback
ViewState in Dynamic Control
Call method GenerateControls from Page_Init event as below.
protected void Page_Init(object sender, EventArgs e)
{
GenerateControls();
}
EDIT :
JavaScript function...
function ShowGrid(gridID) {
document.getElementById(gridID).style.display = ''
}
I have kept the server side click event as it is. But I have commented the line where the event handler is assigned to the link button.

Checked_Change Event of programatically generated Checkbox inside GridView Row

I have a GridView which holds user data. When the Page_Load Method is called, I get data using a DataTable and then bind it to the GridView. At the end of each row, I have added a CheckBox. This CB is used as a pointer for which entity the user wants to edit.
My problem is the Check_Changed Event of the CheckBoxes. I do not know how to add a handler if the control is generated programmatically. I also need the index of the row (a field value is also possible, but the column header and the column itself are hidden).
foreach (GridViewRow gvr in grdMitgliedsliste.Rows)
{
//add checkbox for every row
TableCell cell = new TableCell();
CheckBox box = new CheckBox();
cell.Controls.Add(box);
gvr.Cells.Add(cell);
//Hide columns for userid, status, etc.
gvr.Cells[0].Visible = false;
gvr.Cells[3].Visible = false;
gvr.Cells[4].Visible = false;
gvr.Cells[5].Visible = false;
gvr.Cells[8].Visible = false;
gvr.Cells[9].Visible = false;
}
I have already tried implementing the handler from here, but it gives me no index argument so the program cannot determine in which row the checkbox was checked.
protected void Page_Load(object sender, EventArgs e)
{
List<string> names = new List<string>();
names.Add("Jhonatas");
this.GridView1.DataSource = names;
this.GridView1.DataBind();
foreach (GridViewRow gvr in GridView1.Rows)
{
//add checkbox for every row
TableCell cell = new TableCell();
CheckBox box = new CheckBox();
box.AutoPostBack = true;
box.ID = gvr.Cells[0].Text;
box.CheckedChanged += new EventHandler(box_CheckedChanged);
cell.Controls.Add(box);
gvr.Cells.Add(cell);
}
}
void box_CheckedChanged(object sender, EventArgs e)
{
string test = "ok";
}
TableCell cell = new TableCell();
CheckBox box = new CheckBox();
box.Check += new EventHandler(Checked_Changed);
cell.Controls.Add(box);
gvr.Cells.Add(cell);
sorry, im allready about to drive home so its just an fast answer.
mabye you have to correct the event after box."event" ...
You should go this way:
first of all when you are generating the checkbox
CheckBox box = new CheckBox();
box.AutoPostBack=true;
provide an id to the checkbox as
box.ID=Convert.toString(Session["Count"]);
do initialize the "Count" when the page loads in the session.
also increment the "Count" everytime you add a new checkbox.
secondly, define the event handler for your dynamic check box like this:
box.CheckedChange += MyHandler;
and define the MyHandler
protected void MyHandler(object sender, EventArgs e)
{
//Do some stuff
}
now you may get the id for the checkbox from which the event has been fired inside the MyHandler, which will actually be the row number.
CheckBox cb = (CheckBox)sender;
string id = cb.ID;

Link button's Click Function is not being call

I am generating dynamic link buttons in c#.At click of any of them,a other function will be call that will show which link button was clicked.But it is not being call at click of any link button.
This is how i am generating it.
Int32 i; //create a integer variable
for (i = 1; i <= 10; i++) // will generate 10 LinkButton
{
LinkButton lb = new LinkButton(); //create instance of LinkButton
lb.Text = Convert.ToString(i) + ""; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); // LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); // LinkButton CommanName
lb.OnClientClick+= new CommandEventHandler(lb_Command); //Create Handler for it.
//type lb.Command += and press double time Tab Key it will generat the lb_Command() code
PlaceHolder1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
This is function code.
void lb_Command(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandName; // will display the which Linkbutton clicked
Label1.Text = "aaaa";
// Response.Redirect(“LnkBtn.aspx?val=” + Label1.Text); // you can also use as QueryString to send values to another page
}
please provide help.
Thanks.
You are using the wrong event :
ClientClick is just a client side event triggered in javascript.
What you want is the Click Event
[...]
lb.Click += new CommandEventHandler(lb_Command); //Create Handler for it.
If you still aren't catching the event, then chances are you are declaring your LinkButton dynamically at the wrong time in the Page Lifecycle (as mentionned by Eoin Campbell in his comments and in his answer).
He links to an article that's really good on the subject, you should read it to understand why you aren't catching the event.
Judging by your comments on your question and on different answers, you are declaring the button in your Page_Load function. That will not work because Page_Load occurs too late in the page lifecycle.
From what I understand, right now you have :
void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Int32 i; //create a integer variable
for (i = 1; i <= 10; i++) // will generate 10 LinkButton
{
LinkButton lb = new LinkButton(); //create instance of LinkButton
lb.Text = Convert.ToString(i) + ""; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); // LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); // LinkButton CommanName
lb.OnClientClick+= new CommandEventHandler(lb_Command); //Create Handler for it.
PlaceHolder1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
}
}
You need to get rid of that. Instead, use :
void Page_Init(object sender, EventArgs e)
{
Int32 i; //create a integer variable
for (i = 1; i <= 10; i++) // will generate 10 LinkButton
{
LinkButton lb = new LinkButton(); //create instance of LinkButton
lb.Text = Convert.ToString(i) + ""; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); // LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); // LinkButton CommanName
lb.OnClientClick+= new CommandEventHandler(lb_Command); //Create Handler for it.
PlaceHolder1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
}
The article linked above will explain why that is. It's a hard concept to grasp but a very important one.
You are mixing client and server side code. OnClientClick is method for attaching client side code. lb_Command runs on the server side.
Use Click instead.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx
versus
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.click.aspx
Ok. two things. as others have pointed out you should be using the LinkButton Command or Click handlers.
But your second problem is that you are running into Page Life Cycle issues by attempting to only generate your controls
on if(!Postback)
in the Page_Load.
Read this: It's a very good article on the subject that dates all the way back to .net 1
https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
In a nutshell, you should be creating your controls every single time (not just on the initial post) and you need to wire up your event handler (the += part) early enough in the page lifecycle such that the events are wired, by the time the PageLifeCycle attempts to trigger them.
Override the page's OnInit method and move your code there, without the if(!Postback) check
Instead of the below code
lb.OnClientClick+= new CommandEventHandler(lb_Command); //Create Handler for it.
use the following
lb.Command+= new CommandEventHandler(lb_Command); //Create Handler for it.
Here is the full code. It running at my end.
protected void Page_Load(object sender, EventArgs e)
{
Int32 i; //create a integer variable
for (i = 1; i <= 10; i++) // will generate 10 LinkButton
{
LinkButton lb = new LinkButton(); //create instance of LinkButton
lb.Text = Convert.ToString(i) + " "; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); // LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); // LinkButton CommanName
//lb.Click += lb_Click; //Create Handler for it.
lb.Command += lb_Command;
//type lb.Command += and press double time Tab Key it will generat the lb_Command() code
form1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
}
void lb_Command(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandName; // will display the which Linkbutton clicked
}
Thanks.
If link button is inheriting from Button class then you should attach to the OnClick event. ?
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click(v=vs.100)
or
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onclick.aspx

Why does findcontrol need unique id, when I have given it the row to scan

The code
public partial class Table_Traversing : System.Web.UI.Page
{
Table table1 = new Table();
Button button1 = new Button();
protected void Page_Load(object sender, EventArgs e)
{
for (int adding_rows = 0; adding_rows < 4; adding_rows++)
{
TableRow table_row1 = new TableRow();
TableCell table_cell1 = new TableCell();
TableCell table_cell2 = new TableCell();
Label The_text = new Label();
CheckBox checkmate = new CheckBox();
The_text.Text = "This is the text :-)";
checkmate.ID = "checkmate";
table_cell2.Controls.Add(checkmate);
table_cell1.Controls.Add(The_text);
table_row1.Controls.AddAt(0, table_cell1);
table_row1.Controls.AddAt(1, table_cell2);
table1.Rows.Add(table_row1);
}
button1.Text = "click me to export the value";
form1.Controls.AddAt(0, table1);
form1.Controls.AddAt(1, button1);
button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
CheckBox check_or_not = new CheckBox();
for (int i = 0; i < table1.Rows.Count; i++)
{
check_or_not = (CheckBox)table1.Rows[i].FindControl("checkmate");
Response.Write(check_or_not.Checked.ToString());
}
}
}
The error
Multiple controls with the same ID 'checkmate' were found. FindControl requires that controls have unique IDs.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Multiple controls with the same ID 'checkmate' were found. FindControl requires that controls have unique IDs.
Just append the row number to the ID:
checkmate.ID = "checkmate" + adding_rows.ToString();
And of course, append it to your FindControl parameter as well:
check_or_not = (CheckBox)table1.Rows[i].FindControl("checkmate" + i.ToString());
You added the checkbox to the cell, not the row:
table_cell2.Controls.Add(checkmate);
Hence - one row has multiple cells with id "checkmate":
E.g
<tr id="somerow">
<td><input type="checkbox" id="checkmate"/></td>
<td><input type="checkbox" id="checkmate"/></td>
</tr>
So within the row "somerow", there are multiple checkboxes with id "checkmate".
Your code to add the checkboxes seemingly looks like you're only adding one though - so it must be something you've missed.
Try removing the FindControl code and see what actual HTML get's rendered.

Categories