How to pull PostBack data into a dynamically added UserControl (.NET)? - c#

I have a Panel on my Page:
<asp:Panel ID="pnlTest" runat="server" />
Then I dynamically add a TextBox to it on Page_Load:
TextBox simpleTextBox = new TextBox();
pnlTest.Controls.Add(simpleTextBox);
simpleTextBox.ID = "SimpleTextBox-1";
Is there a way to pull in the information typed in this TextBox without pulling it directly from Request.Form? I thought I could do something like this after I added it again:
lblPresentResults.Text = myTextBox.Text;
I know this example seems contrived, but I figured I'd try to eliminate all the other variables in my specific application, especially to ask a question here.

You need to add the textbox before viewstate loads, such as in Page_Init, and you should be able to do this.

Just create the text box on Init or PreInit rather than Load, so that it exists in the page before ViewState is restored. Then ASP.Net will update it for you automatically.

Related

Extending GridView with default buttons and update panel

I am trying to extend the ASP GridView control by adding some buttons above it. Such as Insert/Delete/Print etc. I must extend the GridView control because it is used at a lot of places in the project. The buttons and the control must be wrapped in an UpdatePanel so it can do partial updating. I am trying to do something like this:
UpdatePanel up = new UpdatePanel();
ImageButton newButton = new ImageButton();
newButton.ImageUrl = "../Images/new.gif";
up.ContentTemplateContainer.Controls.Add(newButton);
up.ContentTemplateContainer.Controls.Add(this);
Page.Form.Controls.Add(up);
The problem is I don't know where to put it, and I don't even know if this will work. If I put it at OnLoad/OnPreRender I get this:
"The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."
This error occures on the second add.
If I put it in CreateChildControls Page is not yet created.
Please give me some pointers.
In this situation, I'd probably build my own custom server control that inherits from GridView.
Here's a link to a MSDN article that should get you started: Walkthrough: Developing and Using a Custom Web Server Control
EDIT:
Since you're already creating the custom server control; I think this might be your problem line: Page.Form.Controls.Add(up)
Try using simply Controls.Add(up). What it looks like you're trying to do is add the update panel to the Page itself; when you should really be adding it to the controls collection of your extended GridView

Issue with page life cycle (asp.net)

I have got this event in my page aspx.cs:
public void deleteBtn_Click(object sender, CommandEventArgs e)
{
UsefulStaticMethods.DeleteComment(int.Parse(e.CommandName));
}
I am trying to delete a comment from the page. The deletion is successful. However, the website interface doesnt update itself after that event happens.
My Page Load is responsible to drawing all the comments on the page with a dynamic button (delete comment).
I know when the delete button fires, the page Load fires before.. and thats a bit is a problem..cause the page load recreates the page interface, while the deleteBtn_click deletes the comment, and I want to update the interface straight away... "Refresh" the page without the comment that was deleted..
If i execute a function to draw the whole table again, it will draw another comment list along with the comment list drawn at the page Load event.
I cant not refuse not to draw the comment list at page_load, cause i need everything recreated at postback time (including the dynamically created button).-cant use !Ispostback
The question is how can i achieve this/overcome the issue?
Typically, if your using data-binding then you can just re-bind the control in question. Perhaps, you should modify your function that draws the comment list to clear the existing list (possible by removing rows from table control or clearing control collection from container panel or placeholder (you can introduce a placeholder control just for clearing purpose)).
Yet another hack to refresh the page is to restart the page-life cycle by doing Server.Transfer to the same page. Generally, I wouldn't recommend this approach unless page code structure is very complicated and refreshing the data would take many lines of code.
You need to rebind control. Suppose your button is in Grid than you need to Rebind() grid. If not than there is one more way. Put Contents in Update panel and set Update panel Trigger with Delete button. SO when Delete button is clicked that update panel cause Update.
Use data list or repeater server control to display comments and than bind the server control again after deletion. Use !Ispostback on Page_Load.

Using C#, how can I read the content of dynamic created textboxes?

Hy,
I have created some dynamic textboxes with standard content.
Does anyone know how can I read the content of these textboxes (assuming that user modified the standard content) when I press one button?
Thanks a lot.
Jeff
Update
This is how I am creating the textboxes:
foreach (string name in listOfNames)
{
TextBox tb = new TextBox();
tb.Text = name;
tb.BorderStyle = BorderStyle.None;
tb.BorderWidth = 0;
tb.Font.Name = "Arial";
tb.Font.Size = 8;
}
The specific will vary depending on the technology you are using. However the concept would remain very similar, though for ASP.NET it will be a little more interesting.
WinForms/WPF/Silverlight
Maintain a list of the dynamically created textboxes and when the button is pressed you can run through the list of textboxes and read the Text property to get the user input.
ASP.NET - After the tag update it seems this section is most appropriate to your requirement.
For ASP.NET you will need to create the textboxes in an override of the OnInit method, this should happen on each postback. Then in the Button.Click event you can read the user input from the textboxes that you created in the OnInit function. You need to ensure that the controls are created with the same ID on each post back.
You need to ensure that the text boxes are recreated on every postback.
If you do not recreate them, you will not be able to access their properties or events.
The best place to create dynamic controls is the page Init event handler.
I suggest reading up on the ASP.NET page life cycle.
Update (following updated question)
Make sure to set an ID (and a different one, at that) for the text boxes, so you can refer to them later on.
I can't see where you are adding these controls to the page either.
Request.Form is a collection of Key-Value pairs that represents all of the data coming back from the ASP.NET request. If you access that you can get any value whether its control is specified in the ASPX code or dynamically created in the code behind file.
You can also get their values placed back in them automatically if you recreate them during the init part of the page lifecycle. If you do this, their values will be set when ASP.NET recreates the state of the page and applies the values from the form.
You should be able to access them as you would a normal control, you just need to get a reference to the control.
Remember to re-create all controls on each postback.
this is a useful article on dynamic controls in asp.net

Updating label using jquery and code behind

I have a label on a page and I'm updating the text property of the label (with a calculated value) when text is changed in a textbox.
I'm updating the label like so:
$myLabel.text("123");
The text is being displayed correctly on the screen butwhen I try to save the text value to an object in the code behind (When I press a button) the text property of the label is "" and not "123".
Code behind:
var myLabel = myLabel.Text;
//the var myLabel is "" when it should be "123"
Any ideas as to why this would be?
Thanks in advance,
Zaps
Not sure if this is the correct way to do it but I got around the problem by using a hidden field.
I updated the label text as above:
$myLabel.text("hello");
but then I updated the hidden field value:
$('#<%= hiddenField.ClientID %>').val("hello");
I was then able to use the hidden field in the code behind:
var myLabel = hiddenField.Value.ToString();
This seems to work fine.
Why don't you check the value that was entered in the textbox. based on your description, that should be the same and it will be available. Otherwise, I think you need to post some more code to clarify what you are doing.
The value of the label text needs to be stored in ViewState, otherwise it will be overwritten on the postback triggered by the button click.
One option would be to also change the value of a hidden control. Any changes to this value will be available in the code behind on postback.
<asp:Hidden id="hiddenLabel" runat="server" />
Html controls like labels, spans, divs don't post their values to the server - while inputs do. ASP.NET maintains changes in controls by using ViewState.
When you change the value of a server control, it's state is often persisted there. If you change the value on the client side via JavaScript, the ViewState isn't changed, and this is why on PostBack you get the original Empty value.
In what function are you putting var myLabel = myLabel.Text;?
It won't work in the init function -- you need to give the page time to load from the viewstate. Best in the button push event handler.
Update:
You need to use an form input control (eg TextBox) not label. Labels are readonly.

How can i create a ModalPopupExtender control dynamically from a server control?

I have a composite server control that does quiet a number of things; one of which is to display a ModalPopup OnClick of a dynamically generated HtmlAnchor control.
I need to create this ModalPopupExtender control dynamically in my server control and trigger it from within.
I have been able to create it and trigger it from a button created at design time but not at runtime. This is as a result of the ID assign to the link is always not found by the ModalPopupExtender control.
I have tried assigning a static ID but no success. Can anyone help?
I figured it out. All i needed to do was recreate the HtmlAnchor control in the overridden CreateChildControls method on postback.
Thanks David for you concern.

Categories