Using the AutoCompleteExtender with ASP.Net - asp.net

How is everyone today?
The Problem
Basically, I'm delving into the world of the AjaxControlToolkit today, with the main aim of fulfilling my AutoComplete requirements. I've set everything up as tutorialised and am a little confused as to why things aren't working (well I have an inkling as to what may be holding me back).
I've added the AjaxControlToolkit dll to my project and in my Markup I have the following :
at the top
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControlToolkit" %>
then within my content
<ajaxControlToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxControlToolkit:ToolkitScriptManager>
<asp:TextBox ID="txtSearch" runat="server" CssClass="search"></asp:TextBox>
<ajaxControlToolkit:AutoCompleteExtender ID="autoCompleteSearchExtender" runat="server" TargetControlID="txtSearch" ServiceMethod="GetCompletionList"></ajaxControlToolkit:AutoCompleteExtender>
Then in the code behind, I have my nice little function (which the breakpoint within is never reached)
<System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _
Public Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
' Get current list
Dim myList As List(Of MyClass) = GetSearchResultList()
Return (From s In myList Select s.Name).ToArray()
End Function
The function is never being called, for a reason I am unsure of.
Potential Issues
There are a couple of potential issues where things may be going wrong:
I've tried adding the AjaxControlToolkit dll to the Toolbar in VS (2010), but all the controls are greyed out...
The GetCompletionList function (WebMethod) I have written is in the code behind the page. Could this not be called because it has to be in a Web Service perhaps?
I've also just realised that my function in the code behind isn't Shared, is this required? Because the list associated with the auto complete is dynamic.
Any help would be appreciated.
Thanks in advance.

Try putting your GetCompletionList function in a web-service (asmx is easiest). Make sure the the web-service class has a [ScriptService] attribute and that your function has a [ScriptMethod] attribute.
You'll also need to supply the path to the web-service in your AutoCompleteExtender's "ServicePath" property (i.e. ServicePath="~/MyService.asmx")
Also, you don't need the "contextKey" parameter in your function unless you are passing a context key from your AutoCompleteExtender control.
hth

Update: Add these attributes and try it. I have implemented and it works with page behind web methods.
ServiceMethod="yourGetfunction"
MinimumPrefixLength="2"
CompletionInterval="100"
EnableCaching="FALSE"
CompletionSetCount="20"

You can check this solution: http://suggester.codeplex.com/
Demo to test: http://show-demos.net/suggester/
Its not from ALAX Toolkit but it uses ASP.NET AJAX and jQuery and has more rich functionality

I kind of made this working:
<WebMethod()> _
Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
Dim listData As New List(Of String)
listData.Add("A")
listData.Add("B")
listData.Add("C")
Return listData
End Function
My html:
<form id="maincontent" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
</asp:ScriptManager>
<div>
<table style="margin-top:40px;color:White">
<tr>
<td>
Type in your search:
</td>
<td>
<asp:TextBox ID="searchBox" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ServiceMethod="GetCompletionList" MinimumPrefixLength="1"
CompletionInterval="10" EnableCaching="false" CompletionSetCount="1" TargetControlID="searchBox"
ID="AutoCompleteExtenderPersonSearch" runat="server" FirstRowSelected="false">
</asp:AutoCompleteExtender>
</td>
</tr>
</table>
</div>
</form>

Related

ASP.NET form using label

I have to create an asp.net form using label. I found an example for this task and tried to run it but nothing from the expected result appeared. Do you have any idea what else should be done to run it? (I'm still at the very beginnig of asp.net.)
Here is the code:
<%# Page Language="vb" %>
<script runat="server">
Sub submit(Sender As Object, e As EventArgs)
label1.Text=txt1.Text
End Sub
</script>
<html>
<body>
<form runat="server">
Write some text:
<asp:TextBox id="txt1" Width="200" runat="server"/>
<asp:Button id="b1" Text="Copy to Label" OnClick="submit" runat="server"/>
<p><asp:Label id="label1" runat="server"/></p>
</form>
</body>
</html>
Please check the default access modifier for your function(SUB).
In c# we use protected and from MSDN
Class members, including nested classes and structs, can be public,
protected internal, protected, internal, or private. The access level
for class members and struct members, including nested classes and
structs, is private by default. Private nested types are not
accessible from outside the containing type.
So try with Friend access modifier for VB.Net.

.NET 4.5 WebForms: do I (still) really have to specify all 3 templates in a FormView?

Investigating the new strongly-typed, model-binding approach within ASP.NET 4.5 WebForms:
In Scott Hanselman's example of WebForms model binding (amongst others) I've seen the use of a FormView that opens in "Edit" mode, containing a number of DynamicControls e.g.
<asp:FormView runat="server" ID="MyForm" ... DefaultMode="Edit">
<EditItemTemplate>
<asp:DynamicControl runat="server" ID="Field1" DataField="Field1" Mode="Edit" />
<asp:DynamicControl runat="server" ID="Field2" DataField="Field2" Mode="Edit" />
</EditItemTemplate>
</asp:FormView>
In my situation, my FormView's ItemTemplate, EditItemTemplate and InsertItemTemplate will be identical, except the ItemTemplate's controls will be in "ReadOnly" mode.
Do I (still) really need to provide three near-identical copies of the template within the FormView?
I'm happy to use DynamicControls, but the team here will never go for the "3x copy-paste" approach seemingly required for the FormView, especially for our large templates.
I had thought that maybe:
the DynamicControls could get their "Mode" from the containing FormView?
I could use something other than a FormView to contain my DynamicControls?
Should I manage the DynamicControls' mode in code-behind to avoid template duplication?
Any examples/ideas?
No, you don't have to specify all 3 templates. I've had the same scenario and this is my solution:
Set the default mode to the most often used mode
Then in code behind of the form manage the form mode
In code behind copy the template e.g. EditTemplate you handcoded to the other one you need
protected void Page_Init()
{
var action = RouteData.Values["action"].ToString();
switch (action)
{
case "add":
personForm.ChangeMode(FormViewMode.Insert);
this.Page.Title += " Add";
break;
case "edit":
personForm.ChangeMode(FormViewMode.Edit);
this.Page.Title += " Change";
break;
default:
personForm.ChangeMode(FormViewMode.ReadOnly);
break;
}
// Reuse inserttemplate for editing
if (personForm.CurrentMode == FormViewMode.Edit)
{
personForm.EditItemTemplate = personForm.InsertItemTemplate;
}
}
Contrary to what many believe, you need only one template in a FormView, the EditItemTemplate.
Below is a simple example showing how to do it (notice that this is not connected to the idea of "Dynamic Data".).
In this way of doing it, the ReadOnly mode is never used, and thus an ItemTemplate is not needed. And the FormView will use the EditItemTemplate for both editing and inserting.
This way to do it simplifies the markup a lot, and when you make adjustments to the layout, you only have to do it in the one single template.
Notice that the save-button has no CommandName. The command is instead determined in the FormView1_ItemCommand event (see code).
Also notice that the mode of the FormView is determined in the event SqlDataSource1_Selected (see that code, with comments).
I have not included the markup for the SqlDataSource1, because there is nothing special you need to think about for that one. Just make it as usual.
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="ApplicationId,UserId"
>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("firstName") %>'></asp:TextBox><br />
<asp:TextBox ID="txtAge" runat="server" Text='<%# Bind("age") %>'></asp:TextBox><br />
<asp:Button ID="btnSave" runat="server" Text="Save" />
</EditItemTemplate>
</asp:FormView>
Private Sub FormView1_ItemCommand(sender As Object, e As FormViewCommandEventArgs) Handles FormView1.ItemCommand
Select Case FormView1.CurrentMode
Case FormViewMode.Edit
FormView1.UpdateItem(True)
Case FormViewMode.Insert
FormView1.InsertItem(True)
End Select
End Sub
Private Sub SqlDataSource1_Selected(sender As Object, e As SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
If e.AffectedRows = 0 Then
' nothing exists yet, so make formview ready to insert
FormView1.ChangeMode(FormViewMode.Insert)
Else
' something exists already, so make formview ready to edit
FormView1.ChangeMode(FormViewMode.Edit)
End If
End Sub
Just set AutoGenerateEditButton="true". All input types in your item template will then be editable.
You might have to show/hide border of the textboxes and the other input type using jQuery.

ASP.NET AJAX Auto-Complete using WebMethod?

I'm trying to use an auto-complete, and it works fine if I call a function without a parameter in the web service:
<WebMethod()> _
<Script.Services.ScriptMethod()> _
Public Function GetCompanyNames2() As String()
Dim db As New DataClassesDataContext
Dim company = From c In db.CT_Companies
Where c.CompanyName.Contains("Airport")
Select c.CompanyName
Return company.ToArray
End Function
If I try to call a function that takes a parameter, which I need to do so I can get the text entered by the user in the text box, it won't even hit the function at all, I put a break point on it and it never hits the break point:
<WebMethod()> _
<Script.Services.ScriptMethod()> _
Public Function GetCompanyNames(ByVal prefixTest As String) As String()
Dim db As New DataClassesDataContext
Dim company = From c In db.CT_Companies
Where c.CompanyName.Contains(prefixTest)
Select c.CompanyName
Return company.ToArray
End Function
Does anybody know what's going on?
Here is the content from default.aspx:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<div>
<asp:TextBox ID="txtCompanyName" runat="server"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCompanyName"
ServicePath="~/AutoComplete.asmx" ServiceMethod="GetCompanyNames" MinimumPrefixLength="1"
EnableCaching="true" />
</div>
Have you tried to rebuild your webservice project and refresh the web reference (the web service proxy) in your web project?
This is probably too long after the fact to be useful but the parameter must be named prefixText and not prefixTest as you have here.

asp.net data binding in literal

Lets say we have the following in the default.aspx file
<asp:Literal runat="server" Text="<%= TestMethod() %>" />
What needs to be defined in the default.aspx.cs file to make this work?
I tried to add a method called TestMethod to the _Default class which simply returned the string Test, but it didn't seem to work.
Can anyone help?
Thanks,
AJ
Apart from the method being marked public...
i think you could also remove the asp:Literal altogether
example
your code
<p><asp:Literal runat="server" Text="<%= TestMethod() %>" /></p>
could be
<p><%= TestMethod() %></p>
However if you are intent on using the Literal then please rather set it on page load.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx
Regards.
I think you can get the same result by doing this
In your .aspx file
<asp:Literal runat="server" ID="ltr1" />
And in your aspx.cs file
ltr1.text = TestMethod();

Webpart Connections asp.net VB

Im having the following problem with vb.net asp.net webparts. Im trying to create a static connection between webparts but im running into a problem, namely:
Could not find the connection provider Web Part with ID 'Ucl_Diary_Summary1'
I have the following defined as my iterface:
Public Interface IDiaryPartsProvider
function Test as String
End Interface
I have the following as my Consumer (UserControl):
Partial Class UsrCtrls_Diary_ucl_DiaryAwaitingReview
Inherits System.Web.UI.UserControl
<ConnectionConsumer("Test", "myID")> _
Public Sub GetTextTransferInterface(ByVal provider As IDiaryPartsProvider)
Dim a As String = provider.Test()
UserMsgBox(a.ToString, Me.Page)
End Sub
End Class
I have the following defined as my Provider (UserControl):
Partial Class UsrCtrls_Diary_Diary_Summary
Inherits System.Web.UI.UserControl
Implements IWebPart, IDiaryPartsProvider
<ConnectionProvider("myID")> _
Public Function Test() As String Implements IDiaryPartsProvider.Test
Return "this is a test"
End Function
End Class
I have my default.aspx as follows:
<%# Register Src="UsrCtrls/Diary/ucl_Diary_Summary.ascx" TagName="ucl_Diary_Summary"
TagPrefix="uc4" %>
<%# Register Src="UsrCtrls/Diary/ucl_DiaryAwaitingReview.ascx" TagName="ucl_DiaryAwaitingReview"
TagPrefix="uc5" %>
<asp:WebPartManager ID="WebPartManager1" runat="server">
<StaticConnections>
<asp:WebPartConnection ID="cnn"
ConsumerID="Ucl_DiaryAwaitingReview1"
ProviderID="Ucl_Diary_Summary1"
/>
</StaticConnections>
</asp:WebPartManager>
<asp:WebPartZone ID="zoneDiaryTopLeft" runat="server" EmptyZoneText="Add WebPart Here" DragHighlightColor="#454777" HeaderText=" ">
<ZoneTemplate>
<asp:Panel ID="pnl1" runat="server" title="Claims Awaiting Review">
<asp:UpdatePanel ID="udp_TopLeft" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<uc5:ucl_DiaryAwaitingReview ID="Ucl_DiaryAwaitingReview1" runat="server" title="Claims Awaiting Review" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</ZoneTemplate>
</asp:WebPartZone>
<asp:WebPartZone ID="zoneDiaryTopRight" runat="server" EmptyZoneText="Add WebPart Here" DragHighlightColor="#454777" HeaderText=" ">
<ZoneTemplate>
<asp:Panel ID="PNL2" runat="server" title="Diary Summary">
<asp:UpdatePanel ID="udp_TopRight" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<uc4:ucl_Diary_Summary ID="Ucl_Diary_Summary1" runat="server" Title="Diary Summary" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</ZoneTemplate>
</asp:WebPartZone>
I can only assume its because I have my webparts - usercontrol wrapped in a panel (used for scrolling) and also an updatepanel which I use to refresh, so how do I get it to see the usercontrol?
Thanks in advance.
James.
I didn't have a chance to look at your message in detail but the issue appears to be with your provider. It should be returning an object that implements the interface used for communication to the consumer (usually a reference to itself).
Check out the following resource for more info:
Introducing ASP.NET Web Part Connections
I found out my problem/solution in the end.
Rob you are correct I needed to pass back an instance of the object implementing the interface but also, you cant reference a User Control for a static connection which is a child of two other controls, namely the panel and update panel. I was doing this all wrong. I changed it so that the update panel is inside the user control rather than on the default page. That way all the specific (web part) components are self contained.
Also, referring back to the original item of returning the instance, I replaced the following:
Public Function Test() As String Implements IDiaryPartsProvider.Test
Return "this is a test"
End Function
with:
<ConnectionProvider("myID")> _
Public Function Test() As IDiaryPartsProvider
Return me
End Function
Public ReadOnly Property Test() As String Implements IDiaryPartsProvider.Test
Get
Return "This is a test"
End Get
End Property
Hope this can help someone!
James.

Resources