simple script to apply bootstrap pagination style in asp.net gridview - asp.net

is there any simple jquery script/plugin to apply bootstrap pagination style in asp.net gridview ? I've found some good tips about how to do this, like these links: here and here.
the only problem with these tips/solutions is we need to make a lot of changes to achieve the result and this is not preferable when you have large application and you want to transform it to bootstrap style. we need another solution. like a simple jquery script that can do the job without making lot changes to the current code.

I've made simple jquery script to apply the bootstrap pagination in asp.net gridview and I think it will be useful to share it here in stackoverflow.
source code of this script is hosted in github here.
usage is very simple:
-include the plugin js file in your asp.net page file:
<script type="text/javascript" src="js/bs.pagination.js"></script>
-set gridview property:
PagerStyle-CssClass="bs-pagination"
that's is all you need to apply bootstrap pagination style in asp.net gridview.
check my blog for more info.
Edit:
about the problem when using gridview inside UpdatePanel, the reason of this problem is because “UpdatePanel completely replaces the contents of the update panel on an update. This means that those events we subscribed to are no longer subscribed because there are new elements in that update panel.”
There is more than one solution to solve this problem:
Solution 1:
Use pageLoad() instead of $(document).ready. Modify the code like this:
function pageLoad() {
$('.bs-pagination td table').each(function (index, obj) {
convertToPagination(obj)
});
}
Solution2:
re-change the style after every update. We can do this by adding these lines to the bs.pagination.js file:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
$('.bs-pagination td table').each(function (index, obj) {
convertToPagination(obj)
});
});

Related

How to update UI based on FileUpload control change

I have an FileUpload control on a page. I need to change some values based on the filename once a user selects a file. I'm trying to find out the best way to do this. The only option I can see is listening in JavaScript for a change event and then either..
a) forcing a post back and updating the form
b) updating things on the client side using JavaScript and some back end async calls.
Is there any other options and if not which of this is preferable?
Thanks
If you are using jquery, you can attach a function to the change of the file upload.
Consider the following example html:
<input id="myFile" type="file">
<p><label id="myLabel">No File</label></p>
And let's say we wanted to update the label with the name of the selected file. To do that, we'd use the following javascript:
$(document).ready(function () {
$("#myFile").change(function () {
$("#myLabel").html($(this).val());
});
});
Here's a fiddle in action: http://jsfiddle.net/ffkuL/1/
If you aren't using jquery, you can do something like this:
var upload = document.getElementById("myFile");
upload.onchange = function (e) {
var label = document.getElementById("myLabel");
label.innerHTML = this.value;
};
And here's a fiddle for that one: http://jsfiddle.net/8PYwK/
(Honestly, though, I find that it's far simpler in the long run to use jquery in the long run when dealing with ASP.NET controls.)
Obviously, the label changing in my samples are just examples. Following that pattern, though, you can make whatever changes you need to on the client side (rather than needing to post back).

ClientId is different in localhost and the webserver

I am facing a new kind of problem.
I am using the jQuery to fill the state dropdown on the change of country dropdown and the code of the jquery is on a js file so i bind the static client id like ct100_ddlCountry, this is working properly on the localhost but when i host this website to web server it not working because the client generating on the server is _ct100_ddlCountry.
Please tell me something if anyone has an idea about this. I am new to this kind of problem.
Thanks to all.
If you can't upgrade to .NET 4.0 for clean id's, I wrote a small lib and shoved it on CodePlex to serialize controls to a JSON array on the client.
http://awesomeclientid.codeplex.com/
http://www.philliphaydon.com/2010/12/i-love-clean-client-ids-especially-with-net-2-0/
It serializes the controls and outputs some JavaScript like:
<script type=”text/javascript”>
//<![CDATA[
var controls = {
"txtUserName": "ctl00_ContentPlaceHolder1_txtUserName",
"txtEmail": "ctl00_ContentPlaceHolder1_txtEmail",
"btnSubmit": "ctl00_ContentPlaceHolder1_btnSubmit"
};
//]]>
</script>
Which then allows you to access controls like:
<script type=”text/javascript”>
//<![CDATA[
var element = document.getElementById(controls.btnSubmit);
//]]>
</script>
No need to write spaghetti code :)
Edit: Alternatively, you can use jQuery selectors to do something like:
var control = $('[id*=txtEmail]');
It is not normally good practice to hard code control ids in your js script includes or html source.
Try using something like this:
JS
function DoChange(controlid) {
$("#"+controlid);
}
HTML
<select onchange='DoChange("<%= ddlCountry.ClientID %>");' />
It means if you move your control around in your control tree, then you dont break your code, and it should work on your localhost and IIS
UPDATE
Or like this
JS
function DoChange(control) {
$(control);
}
HTML
<select onchange="DoChange(this);" />
If it is ASP.Net 4.0 then you can use ClientIDMode="Static" to make sure that only IDs provided by you are there on final markup.
Or you can use something like $('id$=country'). $ is used to match the end of Id, but I am sure that is something not optimal.

ASP .NET C# - Programmatically expand/collapse AJAX accordion panes?

Using .NET 3.5.
How do I programmtically expand/collapse the panes contained within a AJAX accordion control?
My page will have several accordions controls which I want to be able to mass expand/collapse with some buttons.
UPDATE1
My final code solution looks like this:
<script language="javascript">
function collapse_all(flag)
{
if(flag==true)
{
var behavior = $get("<%=Accordion1.ClientID%>").AccordionBehavior;
behavior.set_SelectedIndex(-1);
}
else
{
var behavior = $get("<%=Accordion1.ClientID%>").AccordionBehavior;
behavior.set_SelectedIndex(0);
}
}
</script>
you can find the accourdion control in javascript and call "set_SelectedIndex(-1)"
so (using jquery)
$("#<%=Accordion1.ClientID%>_AccordionExtender").set_SelectedIndex(-1)
if you have a few, you can either do them all discretly or use one of the jquery selectors to find them all, the accordions will have to have been set up to allow all panes to close i believe (RequireOpenedPane = false)
You can't expand them programmatically via you server-side code(VB.NET/C#) because the expansion of the panes is done in the client-side code(JavaScript). If I were you I'd suggest taking a look at the JQuery Libraries and using their show/hide functions to build a custom accordion control to do what you want. JQuery will seem less "WebForm-like" but you'll find it much more flexible than the AJAX Control Toolkit.
If you put the accordion control in an asp.net ajax update panel, you can collapse all panes easily via the codebehind setting the selected index to zero.

jQuery and asp.net not playing nice together

Please refer to this page for reference: http://loadedgranola.valitics.com/_product_83484/blackberry_lime
I have a jQuery script that runs to replace the h1 tags with a background image. It works great when the document loads but when I click "add to cart", after the javascript alert the jQuery styling breaks. Due to CMS restrictions I have no direct access to their javascript or any of the ASP files but I assume there has to be an easy fix to this.
The code I'm using:
jQuery(document).ready(function(){
var textReplacer = document.title.replace(/ /g,'');
jQuery("h1").addClass('replaced').css("background","url(../images/h1/" + textReplacer + ".png) no-repeat 0 0");
});
I have also tried using the function pageLoad(sender, args) { magic but no luck.
Here you go ..
jQuery(document).ready( function() {
jQuery('<style type="text/css" media="screen">h1{text-indent:-9999px!important;background:url(../images/h1/'+document.title.replace(/ /g,'') +'.png) no-repeat 0 0!important;}</style>').appendTo('head');
});
what it does is add a new css rule that pushes the text way out of the box and adds the background image
Here is what is happening:
When you submit the shopping cart it's doing an AJAX call. The result of that call replaces most of the HTML on the page. Any changes you made before that get replaced.
Possible Solution
You would have to run that replace script again after the AJAX call is complete.
Questions
Why are you replacing the H1 tags on load? What problem are you trying to solve? You might be able to find a better CSS solution.

Modifying the AJAX Control Toolkit Dropdown extender

I am using the example on the AJAX website for the DropDownExtender. I'm looking to make the target control (the label) have the DropDown image appear always, instead of just when I hover over it.
Is there any way to do this?
This can be done using the following script tag:
<script>
function pageLoad()
{
$find('TextBox1_DropDownExtender')._dropWrapperHoverBehavior_onhover();
$find('TextBox1_DropDownExtender').unhover = VisibleMe;
}
function VisibleMe()
{
$find('TextBox1_DropDownExtender')._dropWrapperHoverBehavior_onhover();
}
</script>
I found this and some other tips at this dot net curry example.
It works but I'd also consider writing a new control based on the drop down extender exposing a property to set the behaviour you want on or off.
Writing a new AJAX control isn't too hard, more fiddly than anything.

Resources