jQuery selector and DataPager cannot coexist! (ASP .NET) - asp.net

I am using a ASP .NET ListView with a MS SQL Database. I use the ListView to show the information and in the list view, there is an hyperlink which opens the editing window for the specific record in a jQuery Modal Dialog Box.
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server"> </div>
</LayoutTemplate>
<ItemTemplate>
<!--More Items -->
<a name="Link" href="EditItem.aspx?id=<%# Eval("articleid")%>">Edit</a>
<!--More Items -->
</ItemTemplate>
</asp:ListView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SQL-SELECT-STATEMENT">
</asp:SqlDataSource>
<div>
And jQuery works by taking the link of each item which has the name "Link" and associating it with a dialog box.
<script type="text/javascript">
$(document).ready(function() {
$("a[name=Link]").each(function() {
var $link = $(this);
var $dialog = $('<div></div>')
.load($link.attr('href'))
.dialog({
autoOpen: false,
title: "Edit Article",
width: 500,
height: 550
});
$link.click(function() {
$dialog.dialog('open');
return false;
});
});
});
</script>
Everything worked as it should. Earlier all the records could be shown in one go but as the number of records grew, I needed a paging solution. DataPager for the rescue!
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="5">
<Fields>
<asp:NextPreviousPagerField />
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
But now the DataPager's controls are not working! I cannot go back,forward or click a page number as it will open the first data bound item's href in the ListView without a dialog box (Just like a normal page). But if i remove the jQuery code, the paging works as it should but I don't get the neat modal dialog box for my editing :(
Any ideas why these cannot exist with each other? Thanx a lot in advance :)
UPDATE: What doesn't work is that they cannot co-exist peacefully. If i remove the jQuery code, the DataPager works fine but I will lost my modal dialog box for editing. If i put the jQuery code back, the modal dialog box works fine but the DataPager doesn't :(

My guess is that link selector is probably selecting data pager links.
You can try $("a[name='Link']").each(.. note single quotes around attribute value.
Alternately, why don't you use id or class selectors. For example:
<a id="editLink" ...
and
$('a#editLink;).each(...

Related

CKEditor not working properly inside update panel

I am having a problem with CKEditor inside an update panel in asp.net.
I have tab control on page with multiple CKEditor's i.e one ckeditor in each tab.
string scriptAdd = #"var editor = CKEDITOR.instances['ctl00_ContentPlaceHolder1_faqeditor']; if (editor) { editor.destroy(true); } CKEDITOR.replace('ctl00_ContentPlaceHolder1_faqeditor');";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "", scriptAdd, true);
The above code snippet helps in rendering the editor in update panel. But when a postback is done it still shows the earlier value and not the changed one i.e the editor does not reflect the changes made after the tab is changed in the update panel.
The same thing works perfectly fine without update panel.
Is there any solution for this problem?
just force ckeditor to update the textarea on change :
var ckEditor = CKEDITOR.replace('ctl00_ContentPlaceHolder1_faqeditor');
ckEditor.on("change", function (event) {
event.editor.updateElement();
});
Sorry for the late response on this, but the answer may be helpful to others as well. You also need to do the following in code behind:
ScriptManager.RegisterOnSubmitStatement(this, this.GetType(), "updatescript", "CKEDITOR.instances['ctl00_ContentPlaceHolder1_faqeditor'].updateElement();");
Hope this helps.
<form id="form1" runat="server">
<asp:ScriptManager ID="scrpM" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btnshow" runat="server" Text="Show Hidden Editor" />
<div id="divEditor" runat="server" visible="false">
<asp:PlaceHolder ID="plCKEditor" runat="server"></asp:PlaceHolder>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
----------
Add editor inside a div with visible="false"
and on the button click you set visible="True"
it's works fine for me

AjaxToolkit: the last TabContainer on the page is focused on page load

I'm using more than one TabContainer on a page in an ASP.NET project and I noticed a really strange behavior: when the page is loaded the focus jumps to the last TabContainer on the page, causing it to scroll down. I don't explicitly focus on any control so I don't understand where this is coming from. I also switched places between the controls and it is always the last one that is focused.
The TabContainers don't have any fancy settings, this is basically what they look like:
<cc1:TabContainer ID="tabContainer" runat="server">
<cc1:TabPanel runat="server" HeaderText="Header1" ID="tabPanel1" TabIndex="0">
<HeaderTemplate>
<asp:Label ID="lblTab1" runat="server" Text="Tab1"></asp:Label>
</HeaderTemplate>
<ContentTemplate>
... (anything goes here, it still doesn't work)
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel runat="server" HeaderText="Header2" ID="tabPanel2" TabIndex="1">
<HeaderTemplate>
<asp:Label ID="lblTab2" EnableViewState="False" runat="server" Text="Tab2"></asp:Label>
</HeaderTemplate>
<ContentTemplate>
... (anything goes here, it still doesn't work)
</ContentTemplate>
</cc1:TabPanel>
</cc1:TabContainer>
I know I can set focus on a control, I tried it but the page first scrolls to the tab container and then returns to the focused control (it doesn't look good). I tried this to set the focus to another control:
<body id="main" onload="javascript:document.getElementById('lnkLogout').focus();">
Is this the standard behavior for the TabContainer? How can I get rid of it?
Place script below right after ScriptManager control:
<script type="text/javascript">
Sys.Extended.UI.TabContainer.prototype._app_onload = function (sender, e) {
if (this._cachedActiveTabIndex != -1) {
this.set_activeTabIndex(this._cachedActiveTabIndex);
this._cachedActiveTabIndex = -1;
var activeTab = this.get_tabs()[this._activeTabIndex];
if (activeTab) {
activeTab._wasLoaded = true;
//activeTab._setFocus(activeTab); -- disable focus on active tab in the last TabContainer
}
}
this._loaded = true;
}
</script>
Try this out. It helped me:
window.Sys.Application.findComponent('<%=tabContainer.ClientID %>');
tabContainer.set_activeTabIndex(1); ( //Here set the id of the last tab that is the index of the last tab. Index will start with 0 upto last - 1 as in array.. )
This is an old thread, but it never got resolved – here or in any of the other threads I found – and I had the same problem.
I fixed it by putting my javascript in the body element: onload="scrollTo(0,0);"
You can set focus server-side to avoid the page jumping around.
Try this in Page_Load:
PageUtility.SetFocus(foo);
Also check you whether you are setting Page.MaintainScrollPositionOnPostback.
Let me know if that helps.
UPDATE - you can simply call .Focus() on whatever control you want to be in focus by default.
eg: YourControlToFocus.Focus()
I had a similar problem, but I found a more simple solution.
In the case you use a:
<asp:toolkitscriptmanager ID="ScriptManager1" runat="server">
</asp:toolkitscriptmanager>
and more panel in the tab container ( 3 for example):
<asp:tabcontainer runat="server" ID="tc1" ActiveTabIndex="0" >
<asp:TabPanel runat="server" ID="TB1" Height="250" >
<asp:TabPanel runat="server" ID="TB1" Height="250" >
<asp:TabPanel runat="server" ID="TB1" Height="250" >
For example, you can use the property:
ActiveTabIndex="0"
OR
tc1.ActiveTabIndex = 2 'code behind
Where the integer is the ID of the tab you want to Focus.
It works for me! I Hope I can Help someone!
Enjoy

Video Embede code in Div panel? In Asp.net?

In Web application, i am using datalist control to bind the Embed videos to a <div> dynamically. They are coming good, but when we click on video in the datalist they are start to play. I dont want that, I would like to play the particular video in a popup, and make a non action on that video [div which contain embeded video]. I want to make that div enable false.
<asp:DataList ID="DtlstVideos" ToolTip="Click On Video Title" CellPadding="5" CellSpacing="5" runat="server" RepeatColumns="5" RepeatDirection="Horizontal" OnItemCommandXSSCleaned="DtlstVideos_ItemCommand">
<ItemStyle />
<ItemTemplate>
<div id="divVideos" runat="server"><%# Eval("photos") %></div>
<asp:LinkButton ID="lnkVide" CommandName="Click" runat="server" Text='<%# Eval("videotitle") %>' ToolTip="Play" CommandArgument='<%# Eval("id") %>' Font-Bold="true" ForeColor="Blue"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
This is the current code, when a user clicks on the LinkButton a popup will fire and the video will play, but when click on div the video is playing in datalist only, is there any solution?
Id am not sure if I did understand you correct but I hope this might help.
If you are using youtub, you'll be able to access a preview picture of the movi in different-sizes.
The image is accessble via a certain convention like this:
http://img.youtube.com/vi/VIDEO_ID/#.jpg
Find details here: http://www.reelseo.com/youtube-thumbnail-image/
So what I would do is display only the image in the datalist (maybe with some div overlay which looks like a player)... and then on click pass the movi ID to your popup and then embed and play the movie in that popup...div or new window whatever you want..
HTH
Well, I would not use that <div> as you are using rather I would make use of the ImageButton that will act as the video image as when clicking you can fire your command and hooking up a javascript event on it you can fire up the popup when you want.
Let's imagine that you can use jQuery with this:
<asp:DataList ID="DtlstVideos" ToolTip="Click On Video Title" CellPadding="5" CellSpacing="5" runat="server" RepeatColumns="5" RepeatDirection="Horizontal" OnItemCommandXSSCleaned="DtlstVideos_ItemCommand">
<ItemTemplate>
<asp:ImageButton id="divVideos" CssClass="img-video" runat="server" CommandArgument="<%# Eval("id") %>" data-video-id="<%# Eval("id") %>" CommandName="Click" ImageUrl="<%# Eval("photos") %>" />
<%# Eval("videotitle") %>
</ItemTemplate>
</asp:DataList>
and with jQuery you can simply hook up the click event to fire up your pop up and your command
$(function() {
$(".img-video").on("click", function() {
var url = 'playVideo.aspx?id=' + $(this).attr("data-video-id");
window.open(url, "Video Player","menubar=0,resizable=0,width=650,height=450");
return true; // let the image button fire the command argument
});
});
But I would never let it fire the Command event here and I would hook up always in the playVideo.aspx page as there you do have the video id as well and you can do what you need in that page without needing to fire the command event on the video strip.

Server Control Auto Postback with form has _blank attribute

My case is I have an asp.net page has a form
<form id="form1" runat="server" target="_blank">
and a button redirect to another page and this page will open in a new window because of the target attribute of the form .
<asp:Button ID="button1" runat="server" PostBackUrl="~/kindofpage.aspx" Text="Generate" />
and I have a dropdownlist has auto postback = true to post the past to fill another dropdownlist by selected data .
<asp:dropdownliast id="Make" name="Make" runat="server" autopostback="true"></asp:dropdownlist>
the question is : why when I select item from the auto postbacked dropdown an blank page opened ?
I need a way to post the page by the dropdownlist without openning a blank page ..
Thank you,
For lack of a better idea, you could just remove the target="_blank" attribute from your markup, and when your button is clicked, modify the form tag with JavaScript and set the attribute.
You can set the OnClientClick property and run JavaScript when it's clicked. For example:
<asp:Button ID="button1" OnClientClick="document.getElementById('form1').setAttribute('target', '_blank')" runat="server" PostBackUrl="~/kindofpage.aspx" Text="Generate" />
You could always just adjust your buttonpress code to open a new window such as this:
<asp:Button ID="myBtn" runat="server" Text="Click me"
onclick="myBtn_Click" OnClientClick="window.open('kindofpage.aspx', 'kindofpage');" />
then remove the:
target="_blank"
From the form tag.
I struggled with a similar situation but solved it in the following way.
As mentioned in this answer, you can use the OnClientClick property to set the target to "_blank". E.g.
<asp:Button ID="button1" OnClick="codebehind_method" OnClientClick="document.forms[0].target = '_blank';" runat="server" Text="targets new window" />
Then, in the aspx page that my "codebehind_method" function redirects to, I reset the target of the opener form like so:
<script type="text/javascript">
function resetTarget() {
opener.document.forms[0].target = '';
}
</script>
<body onload="resetTarget()">
Now, if you go back to your opener form and use a control that does not have the "OnClientClick" property set, the AutoPostBack should occur in the same tab.
If you want to find your form by ID, replace "document.forms[0]" with:
document.getElementByID('yourFormName')
<form id="form1" runat="server">

.net Accordion Causing me Problems

I had a bunch of controls that I displayed, hid, enabled and disabled based on actions in the web page. Everything worked until i put them into an accordian. Now I can't get the Javascript to be able to update their state. I have a small example
this is the Javascript
<script type="text/javascript">
var ctrl = document.getElementById('<%= btmRocp.ClientID %>');
function ShowPanel(control)
{
alert('<%= btmRocp.ClientID %>');
ctrl.disabled = true;
}
</script>
This is the Accordian
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:Accordion ID="MyAccordion"
runat="Server"
SelectedIndex="0"
>
<Panes>
<cc1:AccordionPane ID="accordianPane0" runat=server>
<Header>Create New Report </Header>
<Content>a
<asp:Button ID="Button1" onmouseup="ShowPanel('') " runat="server" Text="Button" />
<asp:Button ID="btmRocp" runat="server" Text="Button" />
</Content>
</cc1:AccordionPane>
<cc1:AccordionPane ID="accordianPane1" runat=server>
<Header>Create New Report </Header>
<Content>b</Content>
</cc1:AccordionPane>
</Panes>
</cc1:Accordion>
I would love to know what i am doing wrong here the Alert prints out the right ID.
If i do something where i pass the "this" Object to the function i can disable that button but I truly need it to disable, or hide like 10 objects
Does anyone have an idea?
Sample Code at http://www.riconllc.com/accordian.zip
What is the default state of the Accordion? collapsed? I have no idea how the Accordion works, but I'm suspecting that it is modifying the HTML DOM such that when the page first loads "btmRocp" is not actually present on the page itself, until it becomes "visible". That is, it might be injecting controls into and out of the page, based on the accordion status.
Your best bet in figuring out this behavior is to insert "debugger;" statements into your page at appropriate points, to inspect the live DOM at those points in time.
<textbox id="debugbox" onblur="this.value = eval(this.value);"></textbox>
Is a good way to monkey with script on your page as well.

Resources