How can I populate textbox2 based on an autocomplete textbox1 selected value using ajax? If I used the text change event on the autocomplete textbox it fires when scrolling through the list? Somehow I need to capture the selected item?
<script language="javascript" type="text/javascript">
function getSelected() {
alert($get("<%=TextBox1.ClientID %>").value);
}
</script>
<td class="style7" colspan="3">
<asp:TextBox ID="TextBox2" runat="server" Height="97px"
Width="679px"></asp:TextBox>
</td>
So it appears that there is no server-side event in the AutoCompleteExtender that is raised when the user clicks on an item in the drop down list, but there is a client-side event available called OnClientItemSelected. This OnClientItemSelected property can then be bound to a JavaScript function which can then call back to the server-side.
<asp:TextBox ID="TextBox1" runat="server" Height="24px" Width="486px"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServicePath="AutoComplete.asmx" TargetControlID="TextBox1"
MinimumPrefixLength="2" UseContextKey="true" ContextKey="StateDropDown"
CompletionListElementID="autocompleteDropDownPanel" OnClientItemSelected="PostBackAutoCompleteChoice()">
</asp:AutoCompleteExtender>
<script type="text/javascript">
function PostBackAutoCompleteChoice() {
__doPostBack('<%= TextBox1.ClientID %>', '');
};
</script>
with asp.net code below my ajax_loader image doesn't work well and always shown ..
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblname">
Name</asp:Label><br />
<asp:TextBox runat="server" ID="txbname"></asp:TextBox><br />
<asp:Label ID="lblemail" runat="server">
Email</asp:Label><br />
<asp:TextBox runat="server" ID="txbemail" /><br />
<asp:Label runat="server" ID="lblsugg">
Suggestion</asp:Label><br />
<asp:TextBox runat="server" Rows="3" Columns="20" ID="txbsugg" TextMode="MultiLine"></asp:TextBox>
<asp:Button runat="server" ID="btnsubmit" OnClick="btnsubmit_onclick" Text="submit" />
<asp:Label runat="server" ID="lblresultmsg"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<div id="loading">
<p>
<img src="Images/ajax-loader.gif" />
Please Wait</p>
</div>
and jquery code
$("#loading").ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
any suggestions !?
ajaxStart and ajaxStop works only for ajax request sent by jQuery, if you use other libraries or UpdatePanel, it won't help you.
jQuery only.
Whenever an Ajax request is about to be sent {With jQuery-gdoron}, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time.
Example of jQuery ajax request that will fire the ajaxStart and ajaxStop:
$.ajax({
url: 'foo',
...
...
});
You could create a generic way to handle this by adding the following code to a common js include. Here's a quick and dirty example:
Note: Be sure you initialize it by calling SetupGlobalAjaxHandlers on your page load.
function SetupGlobalAjaxHandlers()
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
}
function InitializeRequest(sender, args)
{
if(typeof(OnPageInitRequest) != "undefined")
OnPageInitRequest();
}
function EndRequest(sender, args)
{
if(typeof(OnPageEndRequest) != "undefined")
OnPageEndRequest();
}
Then, in any page that includes the js file, you could optionally implement the OnPageInitiRequest() and OnPageEndRequest() methods. There, you could show/hide your loading indicator.
I do recommend, though, that you use an UpdateProgress control as you get the show/hide logic for free.
This technique opens up some possibilities for you, say, if you wanted to disable controls while a partial postback is occurring.
I'm writing an ASP.NET webpart for use in a SharePoint site and trying to use an UpdatePanel to render query results. I want to use a JQuery plugin to modify the table returned from the asynchronous postback, but I'm having trouble getting the startup script to execute on the asynchronous udpate.
I found this post which indicates that the UpdatePanel won't do an eval() on startup scripts; instead, you must register the startup script block with the ScriptManager. It all makes sense until it doesn't work. The MSDN reference documentation seems to concur with the approach taken there.
My control is too long to post completely, but here's a trimmed-down representation that I think covers anything relevant. Forgive me if there are missing controls in the paste below - I had to remove some parts, and there may be some dangling tentacles, so to speak. The below is the code for the webpart, which is not unlike the SmartPart which loads a user control (.ascx).
As you can see, I am using the ScriptManager.RegisterStartupScript method. I've tried both overloads; once for the Page, and once for the ListView (renamed as 'AspListView') which is in the update panel. In neither case does the startup script execute on the asynchronous update, and I'm at a loss for why.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace MyNamespace
{
using AspListView = System.Web.UI.WebControls.ListView;
[Guid("601b3bdb-ed2a-4ec8-8a40-c37de8ab048d")]
public class ListSearch : StaticTemplateWebPart
{
private AspListView resultsList;
public ListSearch()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
ScriptLink.Register(Page, "jquery-1.3.2.js", false);
ScriptLink.Register(Page, "jquery-ui-1.7.2.custom.min.js", false);
ScriptLink.Register(Page, "jquery.timepickr.js", false);
ScriptLink.Register(Page, "jquery.quicksearch.js", false);
string scriptBlock =
#"
if ($('table#discrepancy-results').length)
{
$('table#discrepancy-results tr').quicksearch({
position: 'before',
attached: 'table.results',
stripeRowClass: ['odd', 'even'],
labelText: 'Keyword Search'
});
}";
ScriptManager.RegisterStartupScript(Page, typeof(Page), UniqueID, scriptBlock, true);
/* adding other controls, getting references, databinding, etc. */
}
void searchButt_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
var beginDT = DateTime.Parse((beginDateText.Text ?? "") + " " + (beginTimeText.Text ?? ""));
var endDT = DateTime.Parse((endDateText.Text ?? "") + " " + (endTimeText.Text ?? ""));
var dataList = SPContext.Current.Web.Lists["MyDataList"].Items;
var results = SearchListItems(dataList, beginDT, endDT, keywordText.Text ?? "");
if (results.Count > 0)
{
resultsList.DataSource = results;
resultsList.DataBind();
}
}
}
}
}
And the user control that gets loaded:
<%# Control Language="C#" ClassName="ListSearchControl" %>
<% if (false)
{ %>
<script src="../../LAYOUTS/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
<% } %>
<script type="text/javascript">
$(function() {
$('id').trigger('click');
$('#<%= BeginTime.ClientID %>').timepickr({
handle: '#<%= BeginTimeTrigger.ClientID %>',
convention: 12,
trigger: 'nothing'
});
$('#<%= EndTime.ClientID %>').timepickr({
handle: '#<%= EndTimeTrigger.ClientID %>',
convention: 12,
trigger: 'nothing'
});
});
</script>
<asp:Panel ID="ControlPanel" runat="server">
<asp:Panel ID="Inputs" runat="server">
<asp:Panel CssClass="DateInputWrapper" runat="server">
<asp:Panel CssClass="BeginDateInput" runat="server">
<asp:Label Text="Begin Date: " runat="server" />
<asp:TextBox ID="BeginDate" Columns="14" runat="server"></asp:TextBox>
<asp:Image ID="BeginDateImg" ImageUrl="/_layouts/Images/calendar.gif" runat="server" />
<ajax:CalendarExtender ID="BeginDateExtender" TargetControlID="BeginDate" PopupButtonID="BeginDateImg"
Format="MMMM d, yyyy" runat="server">
</ajax:CalendarExtender>
<asp:Label Text="Begin Time: " runat="server" />
<asp:TextBox ID="BeginTime" Columns="6" Text="04:00 am" runat="server"></asp:TextBox>
<asp:Image ID="BeginTimeTrigger" runat="server" ImageUrl="/_layouts/1033/Images/clock.png" />
</asp:Panel>
<asp:Panel CssClass="EndDateInput" runat="server">
<asp:Label Text="End Date: " runat="server" />
<asp:TextBox ID="EndDate" Columns="14" runat="server"></asp:TextBox>
<asp:Image ID="EndDateImg" ImageUrl="/_layouts/Images/calendar.gif" runat="server" />
<ajax:CalendarExtender ID="EndDateExtender" TargetControlID="EndDate" PopupButtonID="EndDateImg"
Format="MMMM d, yyyy" runat="server">
</ajax:CalendarExtender>
<asp:Label Text="End Time: " runat="server" />
<asp:TextBox ID="EndTime" Columns="6" Text="03:59 am" runat="server"></asp:TextBox>
<asp:Image ID="EndTimeTrigger" runat="server" ImageUrl="/_layouts/1033/Images/clock.png" />
</asp:Panel>
</asp:Panel>
<asp:Panel CssClass="Submit" runat="server">
<asp:Button ID="SearchButton" Text="Search" runat="server" />
<asp:Label CssClass="SearchStatusText" runat="server" />
</asp:Panel>
</asp:Panel>
</asp:Panel>
<asp:Panel ID="ResultsPanel" runat="server">
<asp:ListView ID="ResultsList" runat="server">
<LayoutTemplate>
<table id="discrepancy-results">
<tr class="header-row">
<th>Scheduled Date/Time</th>
<th>Code</th>
<th>Description</th>
</tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="result-row">
<td><%# Eval("ScheduledDate") %></td>
<td><%# Eval("Code") %></td>
<td><%# Eval("Description") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
</asp:Panel>
<asp:Panel ID="DetailsPanel" runat="server">
</asp:Panel>
I'd like to add that the updatepanel startupscripts wouldn't work for us unless you used the UpdatePanel's ID, and typeof(UpdatePanel). Using startupscripts elsewhere outside an updatepanel wasn't as fussy. We got this working like this:
ScriptManager.RegisterStartupScript(UpdatePanelId, typeof(UpdatePanel), "myScript",
/ *
* Register a startup script to run
* on the client after running this method on the server
* /
#" alert('Add your function to replace this.');", true);
The solution presented by Darin would work as well, but in our case the updatepanel has more functionality and would require additional logic to 'track' what was being done so the script wouldn't execute on every single update to the update panel, but only after a certain event. This way the script is only executed on this event.
Instead of using ScriptManager.RegisterStartupScript here's something else you might try. Register for the end_request event of the UpdatePanel and execute your jQuery script:
// This could also be done in jQuery's $(document).ready
function pageLoad() {
Sys.WebForms
.PageRequestManager
.getInstance()
.add_endRequest(endRequestHandler);
}
function endRequestHandler(sender, args) {
// Shouldn't the next test be:
// $('table#discrepancy-results').length > 0 ???
// and is it necessary at all? I suppose the quicksearch plugin
// won't apply on empty array
if ($('table#discrepancy-results').length) {
$('table#discrepancy-results tr').quicksearch({
position: 'before',
attached: 'table.results',
stripeRowClass: ['odd', 'even'],
labelText: 'Keyword Search'
});
}
}
ScriptManager.RegisterStartupScript takes a parameter key, which you are setting to the control's UniqueID. This key needs to be unique during page scripts execution. So, you should change this line:
ScriptManager.RegisterStartupScript(Page, typeof(Page), UniqueID, scriptBlock, true);
with
ScriptManager.RegisterStartupScript(Page, typeof(Page), Guid.NewGuid(), scriptBlock, true);
or some other way you like to generate unique keys.
after playing with it a bit, I got it to work. I think the problem was that I was just using quicksearch() incorrectly somehow. I tried later with a simple alert() and it worked fine. Thanks for the suggestions!
I have a gridview column that's gets a large amount of text from the server. So instead of showing all that text after the grid loads I want to show it only if a user clicks an Expand link then closes it with a collapse link. Here is what I have. Please note that I already know that I can put both javascript functions in one; I'm testing right now in two separate functions.
<script type="text/javascript" language="javascript" >
function hidelink() {
var col = $get('col');
var exp = $get('exp');
col.style.display = 'none';
exp.style.display = '';
}
function showlink(){
var col = $get('col');
var exp = $get('exp');
col.style.display = '';
exp.style.display = 'none';
}
<asp:GridView ID="GridView2" Width="400px" runat="server" AutoGenerateColumns="False"
AllowPaging ="True"
BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="APPID"
DataSourceID="SqlDataSource3"
PagerSettings-Mode="NextPreviousFirstLast" EnableSortingAndPagingCallbacks="True">
<PagerSettings Mode="NextPreviousFirstLast" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="stuff" HeaderText="Name" ReadOnly="True"
SortExpression="app" />
<asp:BoundField DataField="Description" HeaderText="Short Descr"
ReadOnly="True" SortExpression="des" />
<asp:TemplateField HeaderText="Long Descr" SortExpression="data">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("data") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<div id="col">
<asp:LinkButton ID="expand" runat="server" OnClientClick ="hidelink();return false;">Expand</asp:LinkButton>
</div>
<div id="exp" style="display:none">
<asp:LinkButton ID="collapse" runat="server" OnClientClick ="showlink();return false;">Collapse</asp:LinkButton>
</div>
<asp:Panel ID="Panel1" runat="server" >
<table>
<tr>
<td> <%#Eval("LongDescription")%>
</td>
</tr>
</table>
My issue is that only the first record does everything it should. (expand/collapse) but the other rows only expand and does not hide the expand link in the div tag. It is only finding the id of the first row because when the expand button is hit on any other row it changes the first row to show the collapse link. How can i fix this?
The problem is that because you have repeating elements, the ids of the DIVs are being reused. This is illegal in HTML. The id property of each element must be unique. A better way to handle it is to pass in a reference to the current element to the handler and have it derive the element that it needs to operate on by traversing the DOM.
<div>
<asp:LinkButton ID="expand" runat="server" OnClientClick ="hidelink(this);return false;">Expand</asp:LinkButton>
</div>
<div style="display:none">
<asp:LinkButton ID="collapse" runat="server" OnClientClick ="showlink(this);return false;">Collapse</asp:LinkButton>
</div>
Note: I'm using jQuery in these functions as it makes it easier to traverse the DOM. You can do the same with your own DOM traversal functions and by setting the style properties if you like.
function hidelink(ctl) {
var myDiv = $(ctl).closest('div');
myDiv.hide();
myDiv.next('div').show();
}
function showlink(ctl){
var myDiv = $(ctl).closest('div');
myDiv.hide();
myDiv.prev('div').show();
}
You need to use unique IDs for each row. IDs can only apply to one element in the page, and your code is applying one ID to all the instances of this large column in the table.
Alternatively, you can just use the DOM methods to locate the right element to show/hide. For example:
<div>
Expand
<table style="display: none;">
<tr>
<td><%#Eval("LongDescription")%></td>
</tr>
</table>
</div>
Then for your script:
function showHideDesc(link) {
var table = link.parentNode.getElementsByTagName("TABLE")[0];
if (table.style.display == "none") {
table.style.display = "";
link.innerHTML = "Collapse";
}
else {
table.style.display = "none";
link.innerHTML = "Expand";
}
}
I have two Ajax Toolkit calendar extenders. One of them is a start date and the other is the corresponding end date. What I would like to happen is when a date is selected in the Start calendar the End calendar will jump to that date. This sounds pretty simple but I have been having a hard time getting it to happen.
Someone put me out of my misery... What would be the way to accomplish this?
here you go this worked for me
<asp:TextBox runat="server" ID="txt1" OnTextChanged="txt1_TextChanged" AutoPostBack="true"></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" ID="cal1" TargetControlID="txt1"></ajaxToolkit:CalendarExtender>
<asp:TextBox runat="server" ID="txt2"></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" ID="cal2" TargetControlID="txt2"></ajaxToolkit:CalendarExtender>
protected void txt1_TextChanged(object sender, EventArgs e)
{
cal2.SelectedDate = Convert.ToDateTime(txt1.Text);
}
or you can do this through javascript I would recommend using jquery though to find the textboxes instead of using straight javascript
<asp:TextBox runat="server" ID="txt1" onchange="SetEndDate()"></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" ID="cal1" TargetControlID="txt1"></ajaxToolkit:CalendarExtender>
<asp:TextBox runat="server" ID="txt2"></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" ID="cal2" TargetControlID="txt2"></ajaxToolkit:CalendarExtender>
<script type="text/javascript">
function SetEndDate()
{
var txt1 = document.getElementById("txt1");
var txt2 = document.getElementById("txt2");
txt2.value = txt1.value
}
</script>