Jquery empty() function working too long? - c#

I got weird issue. I'm working with kendo and I've got two cells. First is dropdownlist and the second is depending what I pick in first one. If it's string the second cell is dropdown, if it's int in second cell I got numericbox etc. Now when I'm picking string value second dropdown is showing, but after that when I want to pick another attribute, for example int, the dropdown is not changing on numericbox, just numerixbox is create next to my dropdown.
Screens:
I tried to use .empty() function, but after using this the cell is empty all the time, even if I'll pick another attribute. I just want clear grid cell once after choosing new attribute, not clear it for all time. Tried also $('#myid').html("") but I got the same effect. Can somebody explain me what can I do?

It seems like you'll need a refresh to happen after inputting a new value in the first dropdown. On the kendo UI API page, I've found this reference.
You can code the second dropdown to subscribe to the change event of the first dropdownlist, then emptying the second dropdownlist wont be needed anymore. The example in the reference already shows using the value of the dropdown as a variable. Seeing that the dropdownlist widget in kendo UI doesn't have a visibility property, you'll need to hide or show the wrapper for the element, see the answer from Lars Höppner.
I've added an example I made based on the references:
<input id="dropdownlist" />
<input id="dropdownlist2" />
<script>
function dropdownlist_change(e) {
var dropdownlistToHide = $("#dropdownlist2").data("kendoDropDownList");
var value = this.value();
if (value === "Apples"){
dropdownlistToHide.wrapper.hide();
} else if (value === "Oranges") {
dropdownlistToHide.wrapper.show();
}
}
$("#dropdownlist").kendoDropDownList({
dataSource: [ "Apples", "Oranges" ]
});
$("#dropdownlist2").kendoDropDownList({
dataSource: [ "Dinges", "Dinges2" ]
});
var dropdownlist = $("#dropdownlist").data("kendoDropDownList");
dropdownlist.bind("change", dropdownlist_change);
var dropdownlistToHide = $("#dropdownlist2").data("kendoDropDownList");
dropdownlistToHide.wrapper.hide();
</script>
The final dropdownlistToHide.wrapper.hide(); is meant to hide the dropdownlist widget from the start.

Related

How do I get the text value from a <select> in ASP.NET MVC

I am using ASP.NET Core. I currently use the following <select> element in my view, within a form:
<select asp-for="CarTypeId"></select>
When I make the post I will only get the value for Model.CarTypeId.
However I have another field, Model.CarType which I want to give the value of the selected text in the <select>.
You can add another model property for this and set it during select event using jquery or use formcollection.
It is not a good practice, only the type id should be enough, and use it as a reference to the car type itself, shouldn't be one more field as you described, as this will be redundant.
Anyway the solution is adding a hidden field for CarType, and upon cartypeid change, update its value.
something like this
$('#CarTypeId').on('change', function(){
// this will get the text of the selected option
var theText = $('#CarTypeId').find(":selected").text();
//Update value of the hidden field
$('#CarType').val(theText);
});

How to save the drop down list selected index in asp.net?

i want that after clicking a button saves the index i have selected in order to storage that number to my database. My drop-down-list is in a comment below.
The drop-down-list is filled by using a query. And so far what it does, is that after clicking the button it storages the first index (which is 0), even when i want to save another index.
I have done some research, and so far i cannot solve it, hope someone can help me out.
Code for Dropdown list:-
<asp:DropDownList ID="ddlCategory" runat="server" Height="25px" Width="428px" Font-Size="Large"> </asp:DropDownList>
here's how i fill the drop-down-list
ClsUtil clsutil = new ClsUtil();
DataSet ds = clsutilerias.FillDDL(Category);
ddlCategory.DataTextField = ds.Tables[0].Columns["Name"].ToString();
ddlCategory.DataValueField = ds.Tables[0].Columns["Category_ID"].ToString();
ddlCategory.DataSource = ds.Tables[0];
ddlCategory.DataBind();
The DropDownList has properties. You need to set SelectedIndex, SelectedItem or SelectedValue properly (and in the correct event in relation to when you do the binding etcetera).
Edit: Maybe you updated your question, or maybe I misunderstood, but either way it seems now that you don't want to set the SelectedIndexperhaps as much as get the SelectedIndex. Even so, if you read the documentation and look at the examples provided in the links, you should have enough information to know what to do.
Basically, one of the <option> elements in the <select> on the client (the HTML code) will be selected when the data is posted back to the server. If your <asp:DropDownList> is AutoPostBack="True" you can go for a callback in the OnSelectedIndexChanged event, but there are other ways too, with or without auto-post-back. (You don't really need the view state, or even the <asp:DropDownList> at all, but can trigger a POST by submitting the form data in any way that suits your needs or preferences, and then read any values your interested in server-side.)
It seems that you found your solution in checking IsPostback in the Page, and not re-populate the list if the value is true. That's good. If you don't need to data-bind every time you render the page (even after a POST), that's a viable and common solution.
Another option, of course, would be to read the posted data in any event that happens before you do the data-binding. You may also want to handle the value of SelectedIndex to re-select the correct option in your drop-down after having populating it again, if you don't like having ASP.NET doing it for you.
When you are clicking the button, the entire page is refreshing that why you got the first index all the time
The best solution is :
if(IsPostBack == false)
{
//Save your data code here
}
You have other tricky solution which is :
put the dropboxmenu inside an update panel control
While binding datasource to combo, mention DataMember and DataValue property for combo box so that selecting elements from combo you get selectedValue as DataValue.
Well i have solved my problem, my problem was that when i clicked the button, the pageLoad method executed again, and there was where i had the code to fill my drop-down-list. so i just added:
if(!IsPostBack)
{
//here goes the code to fill the Drop down list
}

How to get IDs of Generated controls in ASP.net using JavaScript or JQuery

I have DataGrid and inside it one of the columns is a TextBox. The DataGrid is generated dynamically from Database. It is for invoicing. One invoice might have two rows, another might have 10. The ID of each of these textboxes are different, and I need to read the value of each of these text boxes when the user enters an amount, add all of them up, and show the total in another field.
Problem: I don't know how to get to each textbox (there is ValueChanged event that fires when the user enters an amount for that specific text box)
I'm trying to solve this using Javascript or JQuery. (No updatePanel)
Any help is greatly appreciated.
Thanks.
Give each text box a class name. Then you can iterate through each textbox by the following
var total=0;
$(".className").each(function(){
total += Number(this.val());
});
If your grid has an id of grid, then you could do:
$('#grid input[type=text]').each(function(){
///Glorious code!!
});
Using jQuery,
$(document).on("ready", function(){
$("document").on("keyup input paste", "input", function(){
var inputID = $(this).attr('id');
//more code here...
});
});
Using event delegation, the event will always be fired on all textboxes. Change "input" to a more specific selector if needed.
Don't use IDs if you want to add listener to each of all your textbox, instead use class.
<input type="text" class="my-text-box">
and the JS(using jQuery)
$('.my-text-box').keyup(function(){
console.log($(this).val());
});
this will display the value of the textbox whenever you type something.

how to get selected value of Disabled dropdown in c#

In my form there are two DropDownList controls.
1st is enabled and 2nd is disabled.
After selecting 1st dropdown I am changing selected value of 2nd dropdown using javascript.
Its working fine. But when I am trying to get selected value of 2nd dropdown, it will return value of first element (i.e. 'select').
Please refer my code
<asp:DropDownList ID="ddlStartTime1" runat="server" AutoPostBack="false"
Width="70" Enabled="false"></asp:DropDownList>
NOTE: I am using javascript to change selected value of 2nd(disabled) dropdown.
Javascript code:
$(document).ready(function() {
$('#<%= ddlStartTime1.ClientID %>').change(function() {
$('#<%= ddlEndTime1.ClientID %>').val($('#<%= ddlStartTime1.ClientID%>').val());
})
});
Is there any alternate way to get changed value of disabled DropDownList?
If you are trying to read the value of 2nd dropdown (disabled one) at server, you will never be able to read the updated value, becuase data in disabled controls will not be posted back to server from client.
You should either enable the dropdown before posting your data to server or use hidden controls to hold data of your disabled dropdown.
You would need to add another input that is hidden. Whenever you change the value of your 1st DropDownList, you'd change the value of your 2nd DropDownList AND the value of the hidden input.
Server-side, you're not looking at the value of the 2nd DropDownList, but at the value of your hidden input. Make sure the hidden value is always sync with the 2nd DDL when you post your form.
Just Add disabled dropdownlist's value in hidden field on change then read value from hidden field rather than dropdownlist.may be this will help you.

How to disable dropdown list value in the current form that was selected in the previous page

I am having 2 web formsand will have a drop down list on those web forms. If i select a value from drop down and click on ok i will get tranfer to next page. In that page i will have a drop down with the same values in the previous form . What i need is i would like to disable the selected value in the previos form and would like to display the remaining normal
I dont know your exact scenario but i would suggest that you look at whether you can maybe achieve your needs using a single form instead of two.
If that is not an option :-
If you are POSTING the form on click of OK, you can retrieve the value of the original page using PreviousPage. Refer eg below.
I am not quite sure what you mean by "disable the selected value in the previos form and would like to display the remaining normal" but once you retrieve this value, then you can manipulate the data in your current page any way you want.
DropDownList oldValue = (DropDownList)PreviousPage.FindControl("DropDownOldValue");
oldValue.SelectedValue - should then give you the value selected on the previous page
Got the answer
DropDownList oldvalue = (DropDownList)PreviousPage.FindControl("DropDownList1");
string str = oldvalue.SelectedValue.ToString();
ListItem i = DropDownList1.Items.FindByValue(str);
i.Attributes.Add("style", "color:gray;");
i.Attributes.Add("disabled", "true");

Categories