Page_Init PreRender Controls, Asp.net - c#

Good Afternoon.
I have a list of panel that are dynamically created on Page_Load. Controls Are added via the Control.Add(p); method. code Follows
Panel p = new Panel();
p.ID = "cell_" + i + "_" + j;
p.Attributes.Add("runat", "server");
p.TabIndex = -1;
p.Attributes.Add("class", "box");
p.Attributes.Add("onClick", "clicked(this);");
p.Attributes.Add("onKeypress", "changeValue(event, this);");
p.Style.Add(HtmlTextWriterStyle.Top, top + "%");
p.Style.Add(HtmlTextWriterStyle.Left, left + "%");
p.Attributes.Add("Text", value);
PanelList.Add(p);
this block of code is in a for loop and the i, j, left, right, and value variables are altered every loop.
Now, When i call this function BEFORE Page_Load Finishes:
foreach (Control c in clientGrid.Controls)
{
Response.Write(c.ClientID.ToString());
}
It sucessfully writes all the client ID's of all the dynamically created panels.
However, When i run the foreach function on postback, the Response.write displays nothing. Its as if asp has forgotten these panels exist!
Now i hear that on Page_Init(), the controls need to be re-rendered!
Im not sure How to do this and after many googles, cant seem to find an answer.
Ive tried using recurtion to iterate through all Page.Controls and pull out the
System.Web.UI.WebControls.Panel
But still no luck :(
If my question in unclear, I cant provide more code and details :)
Thankyou
Alex

The dynamically added controls must be re-added to the page upon each postback. I don't know if this will meet your logical requirements but make sure you add your controls in the page_load method outside of your (!isPostback) condition. Then make sure your logic for writing out the client ids is called after the controls are added to the page.
If you need to store state information about these controls between postback, you will probably have to use some javascript and hiddenfields.
All of your event handlers should be called after Page_Load so if you add the controls in the Page_Load, you should be fine. OnPreRender is sometimes useful for this kind of thing. It gets called after Page_Load but before the post back finishes.

Related

add DIV around every asp:DropDownList control code behind

I try to simply add a div wrapper around every control of type <asp:DropDownList> at a global level (if possible). At this point I have solved it with a asp skin adding "default-select" class to all <asp:DropDownList> and then jquery just adding a div for each of them.
$j(".default-select").wrap("<div class='myClass'></div>");
Now, my question. Is it possible to add this div wrapper from the code-behind instead of relying on a javascript.
Control Adapter:
I know this should be possible by writing a control adapter that override <asp:DropDownList> render method (as described here: Dropdownlist control with <optgroup>s for asp.net (webforms)?). However, I just want to add a wrapping div, not rewrite the entire rendering of the <asp:DropDownList> control (which I have to do if i override the method?). Any suggestions? Maybe there is a way to just add something to the existing adapter??
Custom User Control: Another solution would be to build a custom <mycustom:DropDownList> with the wrapping, but, this would force me to replace every instance of <asp:DropDownList> trough the whole project (large project). I rather just change the original control some how so that my styling applies everywhere.
So summary: Is there an easy way to just make all <asp:DropDownList> render as:
<div class="myClass">
<select><option...></select>
</div>
instead of just:
<select><option...></select>
My first attempt (on Page load): I tried to add this code in the Page_load method but I don't find any way to render that div out?
var j = 0;
foreach (DropDownList control in Page.Controls.OfType<DropDownList>())
{
HtmlGenericControl div = new HtmlGenericControl();
div.ID = "div" +j;
div.TagName = "div";
div.Attributes["class"] = "myClass";
div.Controls.Add(control); // or control.Controls.Add(div); but this wouldn't wrap it.
j++;
}
Your solution just about works. Server control can only exist within the scope of a server form, you will need to perform a recursive search on the page or look directly in the form controls collection. Once you have the DropDownList and wrapped it around a div container it will need to be added to the controls collection.
Also, I think it better to perform this in OnPreInit.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
var j = 0;
foreach (DropDownList control in form1.Controls.OfType<DropDownList>().ToList())
{
var div = new HtmlGenericControl();
div.ID = "div" + j;
div.TagName = "div";
div.Attributes["class"] = "myClass";
div.Controls.Add(control); // or control.Controls.Add(div); but this wouldn't wrap it.
j++;
form1.Controls.Add(div);
}
}
I hope this is helpful. Let me know how you get on.
Either way what you're wanting is custom and you have to code for it. So IMO, the best and simplest option is the custom control. You may have to spend some time refactoring references to replace your <asp:DropDownList>, but in all the time you've spent trying another way, you could've been done by now. :)
I've learned the hard way that keeping it simple is usually the best way to go.

Get dynamically created element by ID in C# [duplicate]

I have stumbled across a problem with my asp.net form.
Within my form the end user chooses a number of textboxes to be dynamically created, this all works fine with the following code:
protected void txtAmountSubmit_Click(object sender, EventArgs e)
{
int amountOfTasks;
int.TryParse(txtAmountOfTasks.Text, out amountOfTasks);
for (int i = 0; i < amountOfTasks; i++)
{
TextBox txtAddItem = new TextBox();
txtAddItem.ID = "txtAddItem" + i;
txtAddItem.TextMode = TextBoxMode.MultiLine;
questionNine.Controls.Add(txtAddItem);
txtList.Add(txtAddItem.ID);
}
}
However this has also caused a small problem for me, later on in my form on the submit button click, I send the results to the specified person it needs to go to (using smtp email). Again this part is fine, until I am trying to retrieve the text from these dynamically created textboxes.
What I have tried
I have tried using this msdn access server controls ID method however this was not working.
I tried to add these new textboxes to a list, however I was unsure on how to update these textboxes when they have text in them. Therefore my results were returning null because of this.
I have also looked at other questions on SO such as this however they are usually for WPF or winforms, rather than my problem with asp.net (this usually isn't an issue, but I don't need to get the text from every textbox control in my page, just the ones that were dynamically created).
I have also tried changing how I call the code that I hoped would have worked:
string textboxesText = string.Join("\n", txtList.Select(x => x).ToArray());
and then in my concatenated string (email body) I would call:
textboxesText
The problem
As they are dynamically created I am finding it difficult to call them by their id for example: txtExampleID.Text, also as I have to increment the ID's by one each time (so they don't override each other) it has made things a little bit more difficult for me.
I am not asking for a code solution, I would prefer pointers in the right direction as I am still learning.
So to sum it all up: I need to get the text from my dynamically created textboxes to add it to my email body.
The issue is these text boxes need recreated in the Load event of the page, every single time, so that both events and values can be hooked back up and retrieved.
I think the most straight forward approach, in your case, would be to extend idea #1 that you had already tried. Build a List of these controls with enough information to recreate them in Load, but you need to store that List in either ViewState or Session.
ViewState["DynamicControls"] = list;
or
Session["DynamicControls"] = list;
I would use ViewState if you can because it gets destroyed when the user leaves the page.

Issues with Custom Server Controls and Events in the ASP.NET page lifecycle

I am starting to go nuts with this one, it "seems" I can't get this to work in an elegant way and I'm sure i should be able to.
On the righthand side of the page I dynamically create Custom Server controls that have events in them that fire, I have proved this bit works in testing.
I have a Treeview on the left side of the page and this is autopopulated at page load assuming it is not a postback, if it is then its viewstate handles this nicely. When you click on one of the branches of the tree it will post back and when the event fires it will then populate the right side with new custom server controls that hold the inforation relevant to what branch you clicked. The right panel always populates with the correct controls, however the events don't fire when they are clicked as the new branch Id is only know after the page_load event (when the treeview event fires) which is too late for all the Event cleverness to register I think.
I have looked into using Response.Redirect, but then I lose the viewstate on the Treeview so have started looking at forcing a Javascript post back but haven't got very far with that.
I really can't believe this is an unusual problem so hopeing there is an elegant solution. If its just i am going about it the total wrong way then happy to change all the logic if we can get this to work!!
Many many Thanks!!
Matt
*UPDATE**
I'm not allowed to close this yet so will paste below
seems typing that out got me thinking about the Javascript route and what .NET must have some where. A bit of reading and I have come up with this.
PostBackOptions pbo = new PostBackOptions(pnlEvents); pbo.ActionUrl = Request.Url.AbsoluteUri; pbo.PerformValidation = false; string postback = ClientScript.GetPostBackEventReference(pbo); ClientScript.RegisterStartupScript(typeof(Page), "AutoPostBackScript", postback, true);
This will cause another postback if you run this after the event fires that you get your information from, allowing you to load the Server Controls forn Init or Load as long as you store your value in viewstate or its in another controls viewstate.
Events can be registered in Page_Init or Page_Load. If you are still lost, check ASP.NET Page Life Cycle Overview on MSDN.
Update:
You can simply send the param in URL, and get it in Page_Load, not using postback mechanism at all.

User Controls required an ID in postback?

I add some div into a panel on server side, when the page is generated, and I add a ID for each one :
HtmlGenericControl divContainerInside = new HtmlGenericControl("div");
divContainerInside.ID = "inside_" + m_oIDCategoria + "_" + numero;
than, on postback (after re-creating them), I cycle them :
foreach (HtmlGenericControl divInside in myPanel.Controls.OfType<HtmlGenericControl>())
{
Response.Write(divInside.ID);
}
all is ok! But, if I remove that divContainerInside.ID when I generate it, I get a NullException cycling them. Why?
I guess you get NullException when you try to read the ID, which you haven't set.
If you change your code like this, you'll get the value:
foreach (HtmlGenericControl divInside in myPanel.Controls.OfType<HtmlGenericControl>())
{
Response.Write(divInside.ClientID);
}
PS: I don't know if you have got this line of code:
myPanel.Controls.Add(divContainerInside);
If you want to find out more about web controls you can read this article and this.
You cant add a control to the page using response.Write you need to add it to the Pages or another controls control collection like below:
Page.Controls.Add(divInside);

Prevent duplicate postback in ASP.Net (C#)

Simple one here... is there a clean way of preventing a user from double-clicking a button in a web form and thus causing duplicate events to fire?
If I had a comment form for example and the user types in "this is my comment" and clicks submit, the comment is shown below... however if they double-click, triple-click or just go nuts on the keyboard they can cause multiple versions to be posted.
Client-side I could quite easily disable the button onclick - but I prefer server-side solutions to things like this :)
Is there a postback timeout per viewstate that can be set for example?
Thanks
I dont think that you should be loading the server for trivial tasks like these. You could try some thing jquery UI blocking solution like this one. Microsoft Ajax toolkit should also have some control which does the same. I had used it a long time ago, cant seem to recall the control name though.
With jQuery you can make use of the one event.
Another interesting read is this: Build Your ASP.NET Pages on a Richer Bedrock.
Set a session variable when the user enters the page like Session["FormXYZSubmitted"]=false.
When the form is submitted check that variable like
if((bool) Session["FormXYZSubmitted"] == false) {
// save to db
Session["FormXYZSubmitted"] = true;
}
Client side can be tricky if you are using Asp.Net validation.
If you have a master page, put this in the master page:
void IterateThroughControls(Control parent)
{
foreach (Control SelectedButton in parent.Controls)
{
if (SelectedButton is Button)
{
((Button)SelectedButton).Attributes.Add("onclick", " this.disabled = true; " + Page.ClientScript.GetPostBackEventReference(((Button)SelectedButton), null) + ";");
}
if (SelectedButton.Controls.Count > 0)
{
IterateThroughControls(SelectedButton);
}
}
}
Then add this to the master page Page_Load:
IterateThroughControls(this);
I have had the same scenario. The solution of one of my coworkers was to implement a kind of Timer in Javascript, to avoid considering the second click as a click.
Hope that helps,
Disable the button on click, utilize jquery or microsoft ajax toolkit.
Depending on how important this is to you, could create an array of one time GUID's which you remove from the array once the update has been processed (ie posted back in viewstate/hidden field)
If the guid is not in the array on postback, the request is invalid.
Substitute database table for array in a clustered environment.

Categories