dynamically created listbox c# - c#

I have to create multiple listbox and bind them as they created dynamically. I have created listbox as loop is for creating no of listbox as needed
for(int i=0;i<requirement;i++)
{
Listbox dynamiclistbox = new listbox();
//then i bind it with a list<>
//then i add listbox to a panel
panel.Controls.Add(dynamiclistbox);
}
My problem is that when i click on submit button the dynamic controls disappear as dynamic controls loose state on postback
can someone help me to create no. of listbox dynamically and bind also bind them on same button click . and get the listbox on postback
i.e, to know the selected item on listbox

You will need to put your code inside Page_Load event. If it is already there, then you probably have it inside something like
if (!Page.IsPostBack) {
//Your code
}
If that is the case, then you will need to put your code outside the if, since it is only adding the controls when it is not a postback and you need to add them when it is postback as well.
EDIT:
Currently the controls are created on button click. It is advisable to have a separate function which receives an input and generates the controls, add them to panel. Call this function from your click event. When this is correct, create an asp:HiddenField and set its Value to the input you need in the click handler. On Page_Load call your function with the Value of your HiddenField.

write code for binding in !IsPostBack section
for(int i=0;i<requirement;i++)
{
Listbox dynamiclistbox = new listbox();
dynamiclistbox.CssClass = "";
dynamiclistbox.ID = ""
dynamiclistbox.EnableViewState = true;
if (!IsPostBack){}

Related

How to set up page loading in ASP.Net for dynamic gridviews?

Here is how my code is set up.
The webpage itself works like this:
You have a drop down that allows you to select between different values. Lets call it dropdown A. Depending on the value selected, a gridview gets generated.
How generation works:
When a item in the dropdown A gets selected, inside the selectedIndexChanged is a method call to a function that creates a DataTable. That datatable gets binded to the gridview inside selectIndexChanged.
When it gets bounded, onRowBoundEvent gets called, and this is where I add all the necessary controls with unique IDs.
There is a button called saved that looks at the data in gridview, and saves it.
Problem: When I press save, there are no controls in the gridview for me to find.
I can use findControl since I know all the ids, but how do I make the controls stick around?
If I bind it in the page_load, how do I know what gridview to generate since if I select a value from dropdown A, page_load still fires before I can get a selection value from dropdown A, so I can't make a simple conditional statement based on the dropdown value.
I can't show any code, sorry. But this is more of a conceptual question I have.
I was able to figure this out on my own.
PrePage_Load during the project lifecycle has access to controls. Dropdown A in my example can be accessed in PrePage_Load, allowing me to get the necessary values and set them before Page_Load starts.
For controls in the gridview, I made it so the gridview does not automatically load from viewstate, and I rebuilt the gridview myself during page_load.

Accessing control values in the Page_Init event in ASP.NET Webforms

I am creating a grid programatically in my code-behind and it must be created in the Page_Init event since I am programtically adding GridTemplateColumns to the grid.
The problem is that the creation of the grid relies on the value of an ASP.NET DropDownList control that the user first selects when they get to the page. This causes a Postback and during this postback I go into the Page_Init event. Here I need the value they selected.
I have spent all day trying to figure out how I can access this control's value in the Page_Init.
I tried this and it is always null:
string productId = Request.Form[ddlProduct.UniqueID];
try something like this.
string controlName = page.Request.Params["__EVENTTARGET"];
if (!String.IsNullOrEmpty(controlName))
{
control = page.FindControl(controlName);
}

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 to access dynamically added radiobutton in update panel

ive product detail page , ive radiobutton in this page which i created dynamically in an updatepanel because of postback... i created it very well , there is an checkedchanged event for all radiobuttons it works very well but i need to get radiobutton text value but i cant reach the radiobutton, it looks as control in updatepanel... but here thing at least for me:D... after i deleted update panel i can reach radiobuttons easily... why it happens like this , i couldnt understand... but i know something i need to prevent page postback on radiobuttons checkedchanged event so how can i do this by using ajax or updatepanel something...
thx...
You said that you can reach radiobutton but it looks like a control. I think you access like that:
myUpdatePanel.Controls[0] // or in a foreach or something like that
What about cast each of that controls?
var myRadioButton = (RadioButton)myUpdatePanel.Controls[0];

Is it possible to put another element (button) inside a Checkboxlist Item?

I have a CheckBoxList that is populated via DataSource (each one with it's value coming from a database so I can't hard-code anything inside it), and I need to Add a button for details on the right side of an specific Item of the CheckBoxList when some event is fired.
Can i do that? how?
Each item in the CheckBoxList is a ListItem object. These don't inherit from Control so they don't have their own ControlCollection property. This means you can't add a LinkButton or Button to the item(s).
If it was based on a Control object, you'd be able to hook into the OnDataBound event of the CheckBoxList, and iterate through the items until you find the one that needs the button. From there you'd be able to add the control (button) to the individual item's item.Controls collection. But you're going to be pretty limited for the ListItem because it doesn't have this functionality.
What does the details button do? If it's simply a client-side button, you could maybe inject html into the Text property of the ListItem, although I haven't verified this works:
foreach (ListItem item in myCheckBoxList)
{
item.Text += " <input type=\"button\"/>";
}
Either way it won't be pretty, and you might be better to create a simple user control. In the control you could still use a CheckBoxList, but you could add HyperLink or Buttons to the UserControl dynamically. You could use CSS or other means to lay out the button in the right spot.
You will have to write a custom user control if you want to do that.

Categories

Resources