Passing Values between Web User Controls - c#

I've several web user controls on my asp.net page and I wanna pass values between them. For example :
There is a dropDownList in one of them and when user selects any item from dropDownList, it can pass the selected value to the other user control which includes GridView to show related data of selected item value from the user control which contains the dropdownlist. (woow pretty awkward sentence tho)
Thanks and Regards..
P.s : Can we use User controls as class in the way to return values ?

On the DropDownList onChange event you could then assign the selected item value to the property of the other user control.
Ok, your UserControls should have properties that allow data to be set. For example in Control2 you would have:
public string needData {
get { return MyData; }
set {
MyData = value;
//do whatever you need to do with that data here
}
Now in your page you would capture the DropDownList OnChange event and inside that you would something like this:
myControl2.needData = myDropDownList.SelectedValue;
Does that make more sense?

Use delegates, example here
Hope this helps

Related

To get value dynamically in gridview based on button click

i have one dropdownlist drpSelectCollege and 2 tables College_M and CollegeSubject_M.In dropdown collegeid is been selected from College_M and now on the basis of College Id another field Subject along with corresponding value of Collegeid is to be displayed in gridview on a click of button search in c#.Plz help me out....
I guess you have a list which you use to bind in the DropDownList.
Look for the SelectionChanged event, when the event is fired you can use the SelectedIndex to use in your list and get the value you want.

Keeping selected item from dropdown box after page refresh in asp web form

I have a aspx page that has several drop down list. So when the users forget to input some values i have to show an error to user, but every DDL are initialized again, and the users information are lost, how can I avoid this ?
When the users forget to input values i return them to current url
url1=www.spadsystem.com
Response.Redirect(Url1);
I heard that we can avoid this problem by using something like absolute i am not sure.
Use SessionState object to store the SelectedIndex property of ComboBox (e.g. Cmb), then apply it in Page_Load() event.
Example 1: Store value in Session
int _idx = Cmb.SelectedIndex;
Session["idx"] = _idx.ToString();
Example 2: Read from Session and apply to ComboBox:
Cmb.SelectedIndex = (int)(Session["idx"]);
More details at: http://msdn.microsoft.com/en-us/library/vstudio/ms178581%28v=vs.100%29.aspx
Rgds,
Avoid redirecting to another page and back to show the error, and ensure you initialize the lists if IsPostBack is false. Then, the ASP.NET Viewstate will take care of it all for you and keep all the selected values.
First of all do not redirect the user when its not required.
Secondly use RequiredFieldValidator for your Fields. It will prevent the postback when your users forget to input any value.
And thirdly, if you are Binding dropdownlists programetically, do that in
If(!IsPostBack)
{
//Your ddl initialization here
}
This will ensure you get the selected values from DropDownLists
agree with use RequiredFieldValidator. and, controls should hold their values if view state is on for the control (check properties).

Complicated Viewstate issue on dynamic controls

I have a control that is loaded dynamically and contains a repeater amongst other items such as a group of text boxes.
The control is used for address verification.
The process is:
control displays.
User enters the address info.
page posts back and the control validates the address info against a web service.
If the address isn't 100%, it displays a list of potential matches. <- this occurs in a repeater. In other words, it is databound in code.
The user checks a box next to the one they want to accept.
page posts back
So far, 1 - 6 work. The problem is that when the page posts back, there are no repeater items.
How can I get the repeater to load it's items without having to run another databind() operation?
Failing that, how can I run the databind() effectively? In other words, I don't have the controls street/city/state/zip values during the oninit. So, how can I reapply the viewstate changes?
Most controls are rendered as HTML form elements like <input type="text" name="myId" id="myId/>.
Part of Web Forms is really just a wrapper around all of this.
Whenever you need to get at the raw values, you can just look in the Request.Forms collection for any control values that are posted back with the name of the control you are looking for (which will usually be made up of the unique id that asp.net generates for uniqueness of controls, and your name).
This is exactly what you did - you went through the Forms collection looking at the raw form values that were POSTed to the server. It's also what you often do when you implement the IPostBackDataHandler interface when creating your own controls - you scoop the value out of Request.Forms.
The annoying thing with checkboxes is that with "standard" HTML they only get submitted if they are ticked.
Two important facts:
Controls in a Repeater Item should be created in OnItemCreated and not OnItemDataBound.
OnItemCreated runs during a Postback without one having to call DataBind().
I've been loading user controls into repeater items and the type of user control is based on the data item being bound to the repeater. So I added the type of user control into the repeater item's viewstate so that during the OnItemCreated that runs on postback (without a data item to bind to because DataBind() has not been called) it knows what type of user control to load. The viewstate will then be applied.
protected void rpTransactions_OnItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var dataItem = e.Item.DataItem;
string typeName;
string viewStateKey = string.Concat("typeName", e.Item.ItemIndex);
if (dataItem == null)
{
typeName = ViewState[viewStateKey].ToString();
}
else
{
typeName = dataItem.GetType().ToString();
ViewState[viewStateKey] = typeName;
}
Control template = TransactionTemplateFactory(typeName);
template.ID = "trans";
e.Item.FindControl("phTransSpecific").Controls.Add(template);
}
}
If the code in OnItemDataBound runs, it fishes this user control out and binds its data to it.

Is there a textboxlist control available somewhere?

There are ASP controls such as radiobuttonlist and checkboxlist and you can databind them to a database query. It's great for creating dynamic lists with user interaction. What I'm trying to do is generate a list of textboxes in the same fashion. A list of textboxes that behave the same way.
The object is to have a checkboxlist that is generated via datasource/database. When the user is finished selecting items from this list, they click a button. That list hides (using jquery) and a new list is created based on their selections. However, the new list is now a list of their selections accompanied by an empty textbox. The user fills in the textboxes for each entry and submits again which commits it to a database.
SO:
checkbox - description
checkbox - description
checkbox - description
checkbox - description
Becomes:
Description - Textbox
Description - Textbox
The reason that I'm looking for a list-type control is so that I can ultimately loop through it for submission to the database using linq. Does that make sense? My real question is if there is a control like this yet. I gave the full description in case someone has any other ideas, short of creating a custom control.
There's nothing out of the box that does what you describe no. But you can still loop through controls. I would put your form controls inside of an asp:Panel or a div with runat="server" and use something like the following code to cycle through them as you described.
foreach(Control ctl in myPanel.Controls)
{
//check control type and handle
if (ctl is TextBox)
{
//handle the control and its value here
}
}
asp:ListBox has a property named SelectionMode, which can be set to SelectionMode="Multiple" and allows you to select items you want. This is not exactly what you need but it's a simple solution.
After the selection is made in checkboxlist, make a postback to server. Here create a datatable with two columns (description & text). For every selected item in the checkboxlist add a row to this table and bind it to a gridview control. Here 'text' column will be always empty. Configure the gridview to use template column with a textbox in the itemtemplate

Getting a value in a textbox from a dropdown list

I have a textbox named textbox1 and a dropdownlist. Dropdownlist contains the list of branches and i want that when i select a branch from dropdown i want to get the respective branch code to be generated in a textbox from the Database or from c# coding. How will it be possible ?
Am I missing something, is it more complex that just putting an event handler on the dropdown so that when it's value changes it calls your method that can do whatever it needs to do to generate the string for the textbox?
Skipping passed the "database" portion of your question (and assuming your data isn't bound to the DropDownList)..
One way in ASP.NET, to accomplish this, I believe, is to have your code print DropDownList.SelectedValue's value in the textbox during an event.
Here is a link showing how to bind an event to DropDownList in jQuery

Categories

Resources