Dynamic Page - accessing elements in events - c#

How to create a dynamic page in pageload() event and how to call the values like textbox or radio button on an button click event or in any other events?
Kindly give a small example. Thanks in advance!

You can create create any ASP.NET control dynamically and add them to the Page.
TextBox txtBox = new TextBox();
If you have to access these values in the post-back. Then it is better to create them in Page_Init() than Page_Load(). If you are creating a dynamic element then you have to create them on every post-back.
There are some good articles on these topic. Google them

Related

Disable only the link part of a ASP.Net LinkButton

I have a ASP.Net LinkButton in a Repeater. In some conditions on some rows i'm trying to disable the link of the button so that it acts more like a Label, where other rows will be a link that fires off the ItemCommand event.
What is a good solution for this? I'm trying to avoid having two separate control on the page to handle pretty much the same thing.
Anyone have experience with this?
Wire into the ItemDataBound event for the repeater. In each item, find the link button, and then use a method like on this solution to disable the link button on an item by item basis
How to completely disable the link button?

Wiring up a dynamically created LinkButton that is being inserted into a PlaceHolder that is in a ListView Item

I have a ListView that is outputting line items that each have a PlaceHolder object.
Within that PlaceHolder I am outputting any number of LinkButtons.
I am adding a Click event Handler to each Link Button to invoke a code-behind method.
HOWEVER,
The method is not being fired when the link buttons are clicked. I need help determining why this is and how I might be able to fire a method in the code-behind when one of these Link Buttons are clicked.
I have already performed the process of storing the LinkButton controls so that they repopulate the Placeholders after a post back. (They don't disappear when clicked).
All suggestions welcomed! Thanks!
Here is a link for an example of LinkButton to review and verify your code is correct. if not helpful, copy your code to let us help you and solve the problem.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton(v=vs.110).aspx

Dynamic Drop Down List ASP.net

How can I create a dynamic drop down list without using AutoPostBack. I ask because I just want to change the value of what the second drop down list displays and then the user clicks a button and it submits. But If I use AutoPostBack then it postbacks to page and runs code that shouldn't be run until that final box has been selected. (I use Postback in the other part of my program so using !IsPostBack isnt a option.) I looked at Javascript but ASP generates all of its controls names at runtime. What can I do? I have looked at the Ajax CascadingDropDown control but my problem with that is it contained in a XML file or a Database, I need this to be contained inside my Page. Any Ideas?
You can use the CascadingDropDown control from the AJAX Control Toolkit
Maybe this example will help? It's part of the ASP.NET AJAX Control Toolkit available here.
You can use AJAX to get the values for the second drop down list, based on the selected value of the first. Add a onchange event handler on the client-side to the first drop down list that makes the AJAX call and fills the second on success.

ASP.net Dynamically adding controls on Button press. Postback issue

I have a user control which contains several buttons, depending on the button pressed a different control is added to the page (lets say button 1 adds a TextBox, button2 adds a label).
I have code along the lines of:
protected void but1_click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.ID = "tb1";
paramsCtrlDiv.Controls.Add(tb);
}
protected void but2_click(object sender, EventArgs e)
{
Label lb = new Label();
lb.ID = "lb1";
paramsCtrlDiv.Controls.Add(lb);
}
I then have a third button (button3) to get all controls on the page and their values. (Assume each button is only clicked once for this example).
My problem is when button3 is pressed the paramsCtrlDiv.controls array doesn't contain the controls that have been added. I know I need to add these controls at Page_Load time on each postback. My issue is as I don't know exactly what controls the user has added I don't know what I want to add a Page_Load (there could be a text box, then label, just a label or just a tb), I can't control what the user presses.
I know I could store everything in the session but I'm not sure this is an elegant solution. There can also be several instances of this control on different tabs so each one has to correctly maintain it's own control collection
Because you are doing this dynamically, you need a way of storing what your are doing so the server can recreate it with each PostBack. If you don't want to use Session, store the data in the ViewState (which will persist with the page regardless of time). Create a List<YourControlObjects> and make sure it is Serializable and then store it in the ViewState. You probably want to store the control type, location, etc. so that you can rebuild it on Page_Load each time there is a PostBack.
The problem comes down to you needing to maintain your own state for these dynamically created controls. This is just one suggestion but you could do this many different ways.
I personally would handle this using a ListView. Include all of the controls you need in the ItemTemplate with Visible=false and bind them to a small list stored in the viewstate. Programatically set the correct control visible on row databind.
Note you will have to collect your data in the controls and save it in your list before you rebind it.

How do I add response from TextBox? c#

I have created my own control which contains TextBox control as a child control. this textbox has autopostback set on. when the postback is called I dont get any text in request.querrystring.
where should I specify text entered into texbox will be send to response ? or is there any easier way of sending this information? I want to have the entered text once again in textbox after postback.
thank you
For dynamically created controls, you need to create them and add them to the control tree in the Page_Init() event; Page_Load() is too late, and Page_PreRender() is way too late.
Once in the control tree, the runtime will restore the state of the control for you after a postback. You can access the contents of the control using myTextBox.Text -- you don't need to use Request.QueryString
For a control that only contains a TextBox, things would be much easier if you used a user control rather than an INamingContainer. That way, you could just use <asp:TextBox> in your .ascx markup.
A custom control? Any posted control like this loads its data in LoadPostData method. You can override this in your custom control class, and access the value from teh control via:
var text = postDataCollection[textboxID];
The textbox within your custom control has to have an ID.
If you are talking something else, let me know the specifics.
Thanks.
(TextBox)Control.FindControl("control id")
get dynamic control's value in postback , Request.Form("control client id")

Categories

Resources