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>
Related
I have datalist in field.ascx and i have a label and textbox in fields.ascx and i am binding textbox and label dynamically in datalist and display in webform.aspx.Now i want to get label text and textbox value in webform.aspx when clicking a button in webform.aspx using ajax.
field.ascx:
<asp:DataList ID="dtlist" runat="server" RepeatColumns="4" CellPadding="5" EnableViewState="True">
<ItemTemplate>
<data:Value ID="test" runat="server" />
<ItemTemplate>
</asp:DataList>
Fields.ascx:
<asp:Label ID="lbl" runat="server" />
<asp:TextBox id="tb4" runat="server" />
Webform.aspx
<button onclick="myFunction()">Click me</button>
[WebMethod]
public static string LoadUserControl()
{
using (Page page = new Page())
{
HtmlForm form = new HtmlForm();
UserControl userControl = (UserControl)page.LoadControl("UserControls/field.ascx");
form.Controls.Add(userControl);
using (StringWriter writer = new StringWriter())
{
page.Controls.Add(form);
HttpContext.Current.Server.Execute(page, writer, false);
return writer.ToString();
}
}
}
You are destroying the values in the user control within the following line of your ASP.NET AJAX page method:
using (Page page = new Page())
In reality, you don't have access to the Page object at all within an ASP.NET AJAX page method, because it is static and does not at all interact with the ASP.NET Page Lifecycle.
If you are only going to have one instance of your user control, then you could use jQuery selectors in your myFunction JavaScript function to pull out the values for the Label and TextBox controls (be aware of the container naming that ASP.NET uses for master pages, and the control hierarchy). You can then build up an object literal to pass to the ASP.NET AJAX Page Method via the jQuery .ajax() function.
If you are going to have multiple instances of the user control on your page, then you will be better off using a server-side solution, coupled with the ASP.NET UpdatePanel control to give the illusion of asynchronous calls to the server (read: less screen flickering, because of only a partial postback). In this case, you can have public properties of the values you want to pull out of the user control and your server-side code will have references to each of the user control instances by ID.
So bottom line is that if you do not need to actually use a client-side JavaScript function to make a call to the server, then just use the server-side button click handler to do the logic of getting values from the server-side controls. The water quickly gets muddied when trying to get values out of server-side controls that could have multiple instances.
(Before marking this question as duplicate, I've tried all the other questions, most of them have outdated links and doesn't solve my problem)
I'm trying to make a simple autocomplete function but the Code-Behind is never called.
Login.aspx:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server" autofocus="autofocus"></asp:TextBox>
<cc1:AutoCompleteExtender ID="ACE" runat="server" ServiceMethod="GetCompletionList"
ServicePath="~/App_Code/Common.cs"
TargetControlID="TextBox1"
MinimumPrefixLength="1"
CompletionSetCount="10" >
</cc1:AutoCompleteExtender>
</form>
Common.cs:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetCompletionList(string prefixText, int count)
{
return new string[] { "test1", "test2", "test3" }
}
Unfortunately, you cannot use an ASP.NET AJAX page method within a .cs class file unless it derives from the Page class or derives from another class that derives from the Page class (the Page class must be in the inheritance hierarchy). This is the reason that you cannot use ASP.NET AJAX page methods in ASP.NET master pages, because they inherit from the MasterPage class, which is not part of the Page class inheritance hierarchy.
You have, at least, 2 options:
1) Put the GetCompletionList method in your code-behind file Login.aspx.cs and then you can omit the ServicePath property from your auto-complete extender markup.
2) Create a Common.aspx page that will hold ASP.NET AJAX page methods that can be used across pages in your application. Since the only thing in this .aspx file will be static page methods nothing would be rendered if the user navigated to that page, but it does cause confusion for someone that does not know what ASP.NET AJAX page methods are and thinks they should delete a blank page. It could also be confusing for your users if they somehow type in that URL within the address bar of your application.
Now you can have the auto-complete extender's ServicePath property point to the page method in Common.aspx, like this:
ServicePath="Common.aspx"
Note: You can call across .aspx pages for ASP.NET AJAX page methods, which is what allows this Common.aspx approach to be usable.
I have implemented a CKeditor from the below link:-
CKEditor But the issue is that, As soon as I register the editor on my page, it gets reflected. I want the same editor just only for my asp.net textbox. What should I do and make change so that It can only be visible to my textbox only. Please help.
See my textbox
<asp:TextBox ID="txtPostdesc" CssClass="form-control" runat="server" ValidationGroup="AddNew" TextMode="MultiLine"></asp:TextBox>
As per the article CKEditor in ASP.Net it described the way of implementing CKEditor with dll.
You would require following things
1. Two dll : CkEditor.dll and CKEditor.NET.dll.
2. CKEditor folder containing all js, css and images.
Register the CKEditor control at the top of your .aspx page such as
<%# Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
Now you will be able to write the CKEditor server control markup such as below
<CKEditor:CKEditorControl ID="txtPostdesc" BasePath="/ckeditor/" runat="server">
</CKEditor:CKEditorControl>
In above I just change the ID as per your textarea ID. Now you can set and get its content via .Text Property in your code behind file i.e.
string str = txtPostdesc.Text;
Hope above explanation works for you.
I have a login page in my asp.net website (using C#) and it has a "login form" which has an email text box, a password text box and a login button.
In addition, I have a "search form" at the top of the web page which has a search text box and a search button.
*All of the controls are in the same form because of the asp.net limit for one runat="server" form.
The problem is that when I type something to search for and click ENTER (and not directly the button) it doesn't do anything, only runs the Page_Load again. Same thing when I click ENTER in the login section instead of directly on the login button.
I have tried different solutions but they were problematic because of the different functionality of the two "forms".
I have no idea how to solve this, any suggestions?
my web forms is a little rusty but I think something like this:
<asp:Panel ID="loginPanel" runat="server" DefaultButton="loginButton">
<%-- Login Stuff--%>
<asp:Button ID="loginButton" runat="server" />
</asp:Panel>
<asp:Panel ID="loginPanel" runat="server" DefaultButton="searchButton">
<%-- Search Stuff --%>
<asp:Button ID="searchButton" runat="server" />
</asp:Panel>
should work
Only one form with runat='server' available on aspx page. So, if you really need another form, you may use its without runat='server' and use for search jQuery ajax call.
I have a master page with a form element and the defaultbutton attribute set to a server-side ImageButton. On one of my pages I want to "override" the masterpage defaultbutton attribute by setting the Forms DefaultButton in the Page_Load event.
i.e
On mater page:
<form id="form1" runat="server" defaultbutton="btnSearch">....</from>
On the page Page_Load event that "override" the master page attribute:
this.Form.DefaultButton = this.ibRecalc.ID;
It errors with :
The DefaultButton of 'form1' must be the ID of a control of type IButtonControl
I am using image buttons which implements IButtonControl
Any ideas of what I might be doing wrong or a different way to approach the problem?
Thanks
Use UniqueId. Since you can have multiple server controls with the same server id, ie, in a GridView, the framework needs to the unique id to match up to.
this.Form.DefaultButton = this.ibRecalc.UniqueID;
You could try using the "DefaultButton" property of a Panel...
Place your button or whole page or div in asp:Panel
// start panel
asp:Panel ID="pnlOpsCallSummay" runat="server" DefaultButton="btnSearch"
............
//Controls of your requirement
..........
asp:Button ID="btnSearch" runat="server" Text="Search"
close the pannel
No need of Overriding the master page button
If you move the panel inside the login's template:-
<asp:login id="Login2" runat="server" loginbuttontype="Image">
<layouttemplate>
<asp:`enter code here`panel id="Panel1" runat="\
server"defaultbutton="LoginImageButton">
</asp:Panel>
</LayoutTemplate>
</asp:Login>
Then it will work without code.
You can set loginbuttontype="Image" or Link or button according to your requirement.