简体   繁体   中英

Master page containing User Control with AutoCompleteExtender

Brief of Application:

I am creating a website which has a master page , a sub master page and some content pages.

I have created some user controls to display information on the main master page. I have registered those controls directly on my main master page and have specified their position.

I have used some content pages and other master page in ContentPlaceHolder.

Description:

I have a textbox on one of my user control, which i want to use with AutoCompleteExtender to help the user in finding the specific text by displaying him the related results.

Here is the sample piece of code:

<asp:TextBox ID="TxtSource" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender
    ID="AutoCompleteSource" 
    TargetControlID = "TxtSource"
    MinimumPrefixLength="1"
    ServiceMethod="GetSourceList" 
    runat="server" UseContextKey="True">
</asp:AutoCompleteExtender>

Code Behind of Custom Control:

[System.Web.Services.WebMethod(), System.Web.Script.Services.ScriptMethod()]
public static string[] GetSourceList(string prefixText, int count, string contextKey)
{
    string[] SourceList = {"Star Wars", "Star Trek", "Superman", "Memento", "Shrek", "Shrek II"};  

    return (from m in SourceList where m.StartsWith(prefixText.ToUpper()) select m).ToArray(); 
}

I am not using the webservice but the codebehind to call the method.

Issue:

The function in codebehind is never called.

I am aware of the fact that i have to keep the function in aspx file, but with my bad luck i am not adding the control on aspx file but on master file and master page is again treated as a control and not the page.

One solution :

I can add the custom control on every content page.

Problem :

1) No code re-usability, which i want to achieve.

2) Page layout will be changed.

Can there be any other solution, with least code changes?

Try to set the ServicePath of your autocomplete extender.

<asp:TextBox ID="TxtSource" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender
    ID="AutoCompleteSource" 
    TargetControlID = "TxtSource"
    MinimumPrefixLength="1"
    ServiceMethod="GetSourceList"
    ServicePath="yourpage.cs" 
    runat="server" UseContextKey="True">
</asp:AutoCompleteExtender>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM