How to pass a parameter from javascript to a UserControl? - c#

In my ASP.Net application, there's a UserControl called EmployeesUserControl which is shown through a JQuery modal dialog.
Before showing the Employees, I've got to pass a filter criteria which is the entity that the employee is belong to. My question is that how can I pass the value from javascript to the UserControl?
What I tried so far is that I created a HiddenControl called SelectedEntityHiddenControl and I set it a value before showing the popup, hence, when the popup is shown and the Page_Load event handler of the UserControl is called, then I read the value of the hidden control and filter by it. But I don't think it's a good implementation what I've already done.
Any suggestion!

I think it's a good solution. I'm also using HiddenFields to pass values from JS to the ASP.NET Engine and back.
You've to ensure that your values are passed to the asp.net engine at the correct moment within the lifecycle.

Related

how to handle TextBox.KeyPress event in asp.net using ajax

i want to create a web page with a textbox and a gridview.
as you type in the textbox i want the content of grid view to be retrieved from database according to the text of textbox.
i am also using ajax.
is there any way to get the text from textbox as user types in and pass it to the server side code?
i searched on google but the only thing i got was keypress event using jquery or java and display it using java again. but there was nothing about passing it to the code behind.
Thanks and pardon my poor English :(
In asp.net you can specify the AutoPostBack="true" on a textbox this will fire an postback after you lose focus of that TextBox, so not on every keystrike if you desire to have a postback on every keystrike you will have to implement some javascript knowledge.
Here you can find an example:
How do I make a Textbox Postback on KeyUp?
But I believe you are more interessted in a AutoCompleteBox maybe give this link a try:
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
As you said, you can use the java key press event. Then you can create an ajax request and ask the server for data.
You should make sure that the data you are requesting can be retrieved very fast, otherwise you would have a delay when typing. Maybe it's a better approach to fetch the whole result and filter it while typing.
A small example can be found here: Making a Simple Ajax call to controller in asp.net mvc
Other examples can be found with your friend google.com :-)

General concept regarding dynamic textboxes on the fly

I am working on a project which has a requirement to build "pages" on the fly. A page can consist of various controls like textboxes, checkbox etc. Currently when the user wants to add a new textbox I make a ajax request and render partial view and return the HTML and show it on client side. This works but I also want to handle the data properly when these dynamic controls are filled up by user. In a way if I am not wrong I need to be able to make array of HTML controls. Now if we give static List to our view and generate textboxes using Html.TextboxFor we see that the name generated is something:
[0].FruitName
[1].FruitName
[2].FruitName
How do I handle this index part when making a Jquery Ajax request so that I always get the correct indexes and render it on client.
If anybody has any better solution than making ajax request then also please let me know. I need to handle the dynamic rendering of HTML controls and also access their values properly when posted back to server.
Take a look at Non-Sequential Indices at http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx.
He introduced a helper method to generate it as well.
Also, I think you can just pass an index with your ajax call which then gets passed from Controller to your partial view and use it to generate a proper indexed TextBox.
Update:
I've asked a very similar question at Submit javascript dynamically added elements to controller method like Stackoverflow

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.

Dynamic control with DevExpress ASPXComboBox has Javascript problems

I have a problem with a Dev Express component, namely AspxComboBox.
My context is this: I want to dynamically generate the interface for some of my business entities. I have designed a user-control that receives some metadata and, based on that metadata, the controls adds text boxes, date-editors and combo boxes to the interface. All of those controls work like a charm when they are added to the page in a non-dynamic manner.
However, when I add them from the C# code, the following Javascript line has an error:
document.getElementById("usercontrol_combo_I").setAttribute("autocomplete", "off");
"usercontrol" is the ID of the user control I'm designing. "combo" is the ID of the combo.
The error is that the element with the ID ("usercontrol_combo_I") is not to be found in the HTML DOM.
I've discovered that if I choose not to use DataBind on the combo itself (comment out any call to the DataBind() method of the AspxComboBox instance), the JS line that has the error is never rendered (is not present in the final HTML). But, if I leave it like that, any subsequent PostBacks empties the combo list (there are no more items in the combo). The datasource of the combo is a IList instance that is assigned on every page load (even if PostBack == true).
There is a post on DevExpress's support forum that reports the same problem, but, there is no answer from the team.
Anybody here had this problem and found a way to solve it?
With ASP.NET Dev if you're binding on the Page_Load events, you need to bind in ALL requests back to the server, this includes Callbacks as well.
Now getting the HTML element and setting its attributes isn't supported. The only supported way to turn autoComplete off is for a callback to be sent to the server and turn off autoComplete on the server-side property which will update the control. Now the comboBox MUST be the one to perform the callback or wrap the box in a CallbackPanel.
Are you setting the ClientInstanceName of the ASPxComboBox too?
Actually, I've just found a simple workaround.
If I just call DataBind() on my generated control in the page_load event of the page itself, the problem is gone.
For example:
protected void Page_Load(object sender, EventArgs e)
{
base.Page_Load();
this.control.DataBind();
}
Where "control" is a UserControl that contains the combobox.
The weird thing is that I call DataBind even on PostBack and CallBack.
But, hey, it works.
I suppose that there are still a couple more things that I miss when using Devexpress.
But "practice makes perfect" !
Thanks for the reply.

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