Setting Jquery Multiselect plugin properties from codebeind - c#

Is it possible to set jquery Multiselect plugin properties from codebeind?
I have created a asp.net web user control which shows multiselect jquery dropdown with checkboxes. I am using this control on different places on same page and want to adjust the width of each dropdown. Don't know if it is possible to have a control property in code behind which will change the width of multiselect dropdown.
$("select").multiselect({
selectedText: "--Select ALL--",
minWidth: '300'
});

You can insert javascript in your page that can do that work, but you will need to know the id of each select element to do that.
But I wouldn't recommend this.
The best would be to set the property in your html rather than calling javascript.

Adding more info to ppetrov's answer, you can wrap javascript statements to a function. Like following
function SetSize(size)
{
$("select").multiselect({
selectedText: "--Select ALL--",
minWidth: size
});
}
In serverside code, you can use following to set size.
ClientScript.RegisterStartupScript(typeof(Page), "scrpt", string.Format("SetSize({0});","100"), true);
Assuming you need to do this for all select element,as you have specified this in selector.
But, it is best practice to handle everything in clientside code ( JS) , since you need to handle something in JS.

Related

Plugin Select2 Is always selecting the first option in ASP.NET

I have been searching for a couple of hours to try to fix this.
I am using the Select2 jquery plugin to get a dropdown with multiple select.
The problem is that the first option is always selected.
I have tried to use the tag multiple="multiple" but ASP.NET doesn't know what is that, only allow me to make it multiple="true" or multiple="false"
Any suggestion??
By default Select2 should not be preselecting any value, so my guess is that you are setting the selected value somewhere, probably in the c# code where you are populating the drop down with values. So try to clear the selected index in the Page_Load method
slEmployeeRole.SelectedIndex = -1 or in the js document ready part of the page, try clearing the drop down just to be sure that nothing is selected. And use multiple="true". $('#slEmployeeRole').val(null).trigger('change');

asp.net c# add html to dropdownlist [duplicate]

Hi I want to put image along with some data in asp.net drop down list box.
Can somebody give me a sample code to achieve this functionality?
country flag + country name --> in the same list item
You could use jQuery Image Dropdown:
http://marghoobsuleman.com/jquery-image-dropdown
(source: marghoobsuleman.com)
<script language="javascript">
$(document).ready(function(e) {
try {
// target some ids
MSDropDown.init("#combo1, #combo2");
//by wild card
MSDropDown.init("#divid select");
//or
MSDropDown.init("#formid select");
} catch(e) {
alert(e);
}
})
</script>
I haven't tried to use this JQuery Combo-Box, but from the screenshot, it looks like the items in the combo box can include an image in it. Maybe you could research more regarding this.
With standart dropdownlist, you can not achive this.
You need to write a custom server control that is not based on dropdown list.
You cannot achieve this with a raw drop down list / combo box in C#. The underlying control only supports text and not a combination of text + image.
You'll either need to write a custom control or use something like jQuery to build the drop down with your code populating the results.
That's right, standard tool doesn't accept images, try with a custom tool.
try with this free tool: http://controlsbuilder.com/tools/CustomDropDownList.aspx

How to get the selected checkboxes which are dynamically generated using Jquery

Hi i have a page which consists of dynamically generated checkboxes using razor code. I need to get the selected checkboxes using Jquery. If there is anything simple than Jquery to achieve this means suggest me.
you can get Check box status (checked/Unchecked) in Jquery like this
$('[type="checkbox"]').each(function(){
alert($(this).is(":checked"))
});
$( "input[type=checkbox] :checked" )

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.

access Gridview attributes in javascript

I want to change the font size of my gridview in javascript because i am creating a printable version. How can I change the font size in javascript for the gridview?
With a gridview named GridView1 some javascript code like:
document.getElementById('<%=GridView1.ClientID%>').style.fontSize = "12px";
There are a lot of ways to do it, but the method I would use if I were to attempt it is to get the unique ClientID of each control (label, TextBox, literal) in the gridview that I wanted to change and as it was going through the RowCreated event, I would inject the ClientID into an array of control names. Then when the action in JavaScript is executed, I'd simply need to traverse the array and set the style for each control. Using jQuery would even speed this up.

Categories

Resources