SharePoint Web User Control DropDown Box Value Help - c#

I have a page index.aspx this page has two Web user Controls, list.ascx and display.acsx basically list.ascx shows all the lists that are available on that SharePoint site into a dropdown box. The second web user control, displays a list of all the files in the list selected. But there's where I run into the problem my question is how do I transfer the value of the dropdown box from the first web user control into the second one.
thanks

Given that you are using custom web controls, it's a bad idea I find, to intrinsically link two different controls together as dependents. Instead:
Define an event on the first control that is raised appropriately with event arguments containing the data.
Have the encompassing index.aspx page have a handler for this event.
Within this handler, set an appropriate property on the second control, passing the data from the event argument.
This is much cleaner, achieves what you want and de-couples the two controls from one another.

Your list.ascx needs to postback the ListId to the server when the value is changed
<select onchange="PostBackWithListId();" >
<option value="SomeListId">
</select>
jQuery could help here, or you could do it server side with OnSelectedIndexChanged and AutoPostBack.
Then your display.acsx, just needs to read the ListId from the request.

If you just want to pass the data once brutally, no strings attached:
make a class with a static member of the type of data you want to pass.
set the value in one ascx file and read in the second ascx file..

Related

Get dynamically created checkbox values but from onprerender stage

I am trying to dynamically generate checkbox controls in web part (SP 2010) but the number of checkboxes that has to be created depends on the value from previous web part. Abiding by the web part communication infrastructure, I can get that value only on "onprerender" stage. But if I try to create checkboxes with that value count, I cannot get the selected values of the checkbox on postback! I am aware that dynamically created controls should be placed either in page_load or oninit. But, here am supposed not to put there since am getting the particular value in prerender stage. Please help! Am struck with this!
Asp.net needs you to recreate dynamically added controls on every Init after they are first added, till you require those controls on page.
You can use a Session variable to keep track of your count.
If a control is not present when the ProcessPostData method is invoked(Called just after LoadViewState), you cannot receive user input for them. Events like CheckedChanged also will fire only if your controls are present in the Controls collection before ProcessPostData is called.
Refer: Page life cycle

Dynamically Created User Control's Dropbox Items from Database resets on postback

First off, I have managed to create a web application where my dynamically created user controls are recreated and repopulated with the correct information upon postback. I am not sure what my problem is, but i hope that you will be able to help me figure it out based on my situation:
On my page i enter the number of controls to be created into a hardcoded textbox (its on the aspx page) and click the okay butten. This in turn, creates the specified number of user controls dynamically using c# in the background.
So far the desired number of dynamic controls are in a table on the page.
Next...
I have 1 textbox and 4 dropboxes on each dynamic user control. When i type a company name into the textbox field and press enter or click away (on text changed event) it autoposts back and the textbox retains the company name that i have typed in.
Based on this string the dropboxes are populated from the database. Now when i select the desired items from the dropboxes and click on the save button (located outside of the dynamic controls, on the page) it does an insert to the database, but it turns out that upon this postback the indexes from the dropboxes have been reset and the wrong values get inserted.
The following pictures show firstly, how it should be and then how it is.
Basically the company name remains in the textbox of the dynamic control, but the information i choose from the dropbox resets to the first index.
It's hard to tell what happend without code, but this is a common mistake:
If you fill/create the dropdownlist controls in the page load event and you post back, the code will refill/recreate the controls. That's why you have to use something like If(!IsPostBack) in your page load event. Otherwise it will execute that code everytime you do a postback and actually just want to execute the code in your event handler for that button.
If you're dynamically creating the controls, make sure to do that in the Page_Init event. Dynamic controls have to be recreated on every postback. Their state is restored after the Page_Init (if it is a postback), so make sure to only set their values in Page_Load if you want to overwrite them.

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.

How to save user entered data to session state from a dynamically created control?

using c# (asp.net)
i'm programmatically creating several drop down lists (random number).
the user selects a particular value from each list and i need to save the user selected value.
if i try to save the data to session state by using a button click event it says the drop down list object hasn't been created. (obviously cuz i'm creating the drop down lists in the page load event under !IsPostBack.)
if i try to save the data to session state in the page load event under IsPostBack i only get the first value from each list. (obviously cuz when the page is recreated after the postback, the drop down lists have been recreated and the user entered data is lost).
How do i save the user selected value from the drop down lists when a button is clicked?
i'm doing this in the code behind file.
As John Saunders said, you have to recreate all of your DropDownList controls on every postback. Additionally, you have to create them before state is restored. That means that "under !IsPostBack" in the page load event is already too late. Move it up to Page Init.
You have to recreate all of the dropdowns on every postback.
Personally (without the ability to switch to MVC) I would consider getting rid of the postbacks altogether and reading the values back directly from a standard HTTP POST request.
The implementation of dynamic controls with postbacks often ends up rather convoluted and difficult to follow

UserControl - accessing textbox within UserControl within a web form

I am using c#.net
I have different views within my webform, these all generally display different information except for three textboxes (arrival / seen / depart time). To try and cut down on code, I have created a UserControl which contains these three textboxes.
I have referenced the UserControl at the top of my webform and also within each view.
<%#Register TagPrefix="uc1" TagName="userTimes" Src="~/usercontrols/userTimes.ascx"%>
<uc1:userTimes id="userAppointmentTimes" runat="server"></uc2:userTimes>
can’t seem to access the textboxes from the code behind. I need to firstly populate the textboxes and also hold any updated information to be re-inserted back into the database if changed.
Also each textbox has two Validation controls:
First makes sure it is in time
format HH:MM
Second makes sure the arrival is
before the seen time etc
My two questions are:
How do I access the UserControl from
the code behind? I have read that I
need to use the FindControl but I
don’t understand why, when I know
what it is called.
Do I undertake the validation
(server side) within the UserControl
code behind or the webform code
behind?
Thanks in advance for any help.
Clare
1.) You can access the User Control by its ID from the page's code behind - userAppointmentTimes in your case. To access your TextBoxes within the webform you need to use the FindControl-Method at the User Control level. So something like userAppointmentTimes.FindControl("WhateverTextBoxID") should work. You need to cast the result to TextBox of course.
You can't access the text boxes because ASP.Net does not automatically expose them for you. So alternatively you can provide public properties to set/get values to/from your textboxes inside your user control.
Within the user control, you can access your textboxes by their IDs.
2.) Put the validation controls inside your user control.
By webform you mean it's all inside the asp.net form-tag or do you have an asp.net form like FormView nested inside? If the latter is true you need to use FindControl at the FormView level - formView.FindControl("userAppointmentTimes"). Otherwise the user control is accessible from page level via its ID.

Categories

Resources