I am at a beginners level with c# code. So I am creating serverside controls for a forum that has textboxes and stuff and the submit button is also getting created like that
TableRow LastRow = new TableRow();
TableCell LastRowcell = new TableCell();
LinkButton btnSubmit = new LinkButton(); btnSubmit.Text = "Submit"; btnSubmit.Click += new EventHandler(btnSubmit_Click);
LastRowcell.Controls.Add(btnSubmit); LastRow.Controls.Add(LastRowcell); LastRowcell.ColumnSpan = 2;
Table1.Rows.Add(LastRow);
Similartly, I have mutliple rows that get created on server side and once the form posts that the user filles the text boxes in and then they hit submit button. Upon which the following method shoul dbe called
Noops, that didnt work.. Infact when I press submit, it brings the form back and the controls that I created on server side are no more on the form. It looks like when it posts back all the controls that were created on server side got lost... Thanks for
your help on this..
When creating dynamic controls, you have to recreate them every postback to ensure that their events are wired up correctly. This takes a bit of tweaking to get the timing right. Most people try the Page_Load event. Try over-riding the OnLoad event, which
occurs before Page_Load. Pages have a PreLoad event, unlike .ascx controls, which is also a good place to try to initially wire up the controls.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
I tried to put it in the page_Load but the problem is that since the button gets created later in the ddlListCatagories_SelectedIndexChanged Method. The following line of code in the load method says that it doenst know what btnSubmit is. As btnSubmit
gets created on server side.
The problem with putting it in page_int is that, the submit button comes before all the rest of the textboxes and stuff. I need that to be the last control on the form. Thanks for your help.. Much appreciated..
If you have to create controls at runtime, then you have to make sure you can re-create them at the Page_Init for the sub-sequence request. So, what you can do is, keep a piece of information on what should you re-create later by using ViewState or Session
object. It can be a number to indicate how many controls to re-create. Then make sure you register their event handler as well, E.g. Click... etc.
Otherwise, you will not able to handle their events.
No matter how much time you spend on coding. Wish you happy coding.
My Technical Blog
How can I get the values submitted by the user on the form if the onclick event doesn't fire. I need to have a way to access the user enteries. The pre-init method will lose the enteries placed in the text boxes and selections
TableRow LastRow = new TableRow();
TableCell LastRowcell = new TableCell();
LinkButton btnSubmit = new LinkButton(); btnSubmit.Text = "Submit"; btnSubmit.Click += new EventHandler(btnSubmit_Click);
LastRowcell.Controls.Add(btnSubmit); LastRow.Controls.Add(LastRowcell); LastRowcell.ColumnSpan = 2;
Table1.Rows.Add(LastRow);
btnSubmit.Click += new EventHandler(btnSubmit_Click);
I think the first time btnSubmit will created during ddlListCatagories_SelectedIndexChangedevent handler. It is fine and what you have to do it, set an indicator to a session object. E.g. Session["IsSubmitButtonCreated"] = true; .
Then, at Page_Init, you check again Session["IsSubmitButtonCreated"] this object. If it is true then you have to use the same code to recreate it again. Nothing will goes wrong and you should able to see the
Clickevent handled nicely.
No matter how much time you spend on coding. Wish you happy coding.
My Technical Blog
SarahBenjami...
Member
256 Points
252 Posts
Serverside link button created but onclick not firing
Dec 13, 2012 12:04 AM|LINK
Hi
I am at a beginners level with c# code. So I am creating serverside controls for a forum that has textboxes and stuff and the submit button is also getting created like that
TableRow LastRow = new TableRow(); TableCell LastRowcell = new TableCell(); LinkButton btnSubmit = new LinkButton(); btnSubmit.Text = "Submit"; btnSubmit.Click += new EventHandler(btnSubmit_Click); LastRowcell.Controls.Add(btnSubmit); LastRow.Controls.Add(LastRowcell); LastRowcell.ColumnSpan = 2; Table1.Rows.Add(LastRow);Similartly, I have mutliple rows that get created on server side and once the form posts that the user filles the text boxes in and then they hit submit button. Upon which the following method shoul dbe called
protected void btnSubmit_Click(object sender, EventArgs e) { string stationCodeValues = ((System.Web.UI.Control)(sender)).ClientID; string sCityName = stationCodeValues.Split('_')[0].ToString(); }But this method never gets called. I want to be able to get all the fields that the user filled up and save them in a file.
What am I doing wrong here. Please help.
Thanks
-Sarah
sen338
Member
498 Points
118 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 12:07 AM|LINK
Hi,
Just call the btnSubmit.Click += new EventHandler(btnSubmit_Click); event after Table1.Rows.Add(LastRow);
SarahBenjami...
Member
256 Points
252 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 12:13 AM|LINK
Noops, that didnt work.. Infact when I press submit, it brings the form back and the controls that I created on server side are no more on the form. It looks like when it posts back all the controls that were created on server side got lost... Thanks for your help on this..
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ShowCatagories(); } } protected void ShowCatagories() { ddlListCatagories.DataValueField = "CategoryId"; ddlListCatagories.DataTextField = "CategoryName"; ddlListCatagories.DataSource = categories.ToList(); ddlListCatagories.DataBind(); ddlListCatagories.Items.Insert(0, new ListItem("Select a Catagory", "0")); ddlListCatagories.SelectedIndex = 0; } protected void ddlListCatagories_SelectedIndexChanged(object sender, EventArgs e) { TableRow LastRow = new TableRow(); TableCell LastRowcell = new TableCell(); LinkButton btnSubmit = new LinkButton(); btnSubmit.Text = "Submit"; btnSubmit.Click += new EventHandler(btnSubmit_Click); LastRowcell.Controls.Add(btnSubmit); LastRow.Controls.Add(LastRowcell); LastRowcell.ColumnSpan = 2; Table1.Rows.Add(LastRow); btnSubmit.Click += new EventHandler(btnSubmit_Click); } protected void btnSubmit_Click(object sender, EventArgs e) { string stationCodeValues = ((System.Web.UI.Control)(sender)).ClientID; string sCityName = stationCodeValues.Split('_')[0].ToString(); }CruzerB
Contributor
5399 Points
1098 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 12:14 AM|LINK
Hi,
Try to put this at Page_Init event handler.
My Technical Blog
markfitzme
Star
14471 Points
2236 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 12:17 AM|LINK
When creating dynamic controls, you have to recreate them every postback to ensure that their events are wired up correctly. This takes a bit of tweaking to get the timing right. Most people try the Page_Load event. Try over-riding the OnLoad event, which occurs before Page_Load. Pages have a PreLoad event, unlike .ascx controls, which is also a good place to try to initially wire up the controls.
SarahBenjami...
Member
256 Points
252 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 12:34 AM|LINK
I tried to put it in the page_Load but the problem is that since the button gets created later in the ddlListCatagories_SelectedIndexChanged Method. The following line of code in the load method says that it doenst know what btnSubmit is. As btnSubmit gets created on server side.
btnSubmit.Click += new EventHandler(btnSubmit_Click);
SarahBenjami...
Member
256 Points
252 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 12:40 AM|LINK
The problem with putting it in page_int is that, the submit button comes before all the rest of the textboxes and stuff. I need that to be the last control on the form. Thanks for your help.. Much appreciated..
CruzerB
Contributor
5399 Points
1098 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 01:02 AM|LINK
If you have to create controls at runtime, then you have to make sure you can re-create them at the Page_Init for the sub-sequence request. So, what you can do is, keep a piece of information on what should you re-create later by using ViewState or Session object. It can be a number to indicate how many controls to re-create. Then make sure you register their event handler as well, E.g. Click... etc.
Otherwise, you will not able to handle their events.
My Technical Blog
SarahBenjami...
Member
256 Points
252 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 01:24 AM|LINK
CruzerB
Contributor
5399 Points
1098 Posts
Re: Serverside link button created but onclick not firing
Dec 13, 2012 01:44 AM|LINK
Sarah,
TableRow LastRow = new TableRow(); TableCell LastRowcell = new TableCell(); LinkButton btnSubmit = new LinkButton(); btnSubmit.Text = "Submit"; btnSubmit.Click += new EventHandler(btnSubmit_Click); LastRowcell.Controls.Add(btnSubmit); LastRow.Controls.Add(LastRowcell); LastRowcell.ColumnSpan = 2; Table1.Rows.Add(LastRow); btnSubmit.Click += new EventHandler(btnSubmit_Click);I think the first time btnSubmit will created during ddlListCatagories_SelectedIndexChanged event handler. It is fine and what you have to do it, set an indicator to a session object. E.g. Session["IsSubmitButtonCreated"] = true; .
Then, at Page_Init, you check again Session["IsSubmitButtonCreated"] this object. If it is true then you have to use the same code to recreate it again. Nothing will goes wrong and you should able to see the Click event handled nicely.
My Technical Blog