I am using asp.net 3.5 with C#. I need to do a database lookup when a user enters ProductID in txtProductID. I guess doing javascript is out of the question since this will have to be server side call.
I wrote this code in the page_load event of the webpage:
protected void Page_Load(object sender, EventArgs e)
{
txtProductID.Attributes.Add("onblur", "LookupProduct()");
}
protected void LookupProduct()
{
//Lookup Product information on onBlur event;
}
I get an error message: Microsoft JScript runtime error: Object expected
How can I resolve this ?
onblur is a client-side event. LookupProduct is a server-side method. You can't reference one from the other - there's simply no association whatsoever between the two.
There's no quick fix for this - you have to either trigger a postback on the client event (using ClientScriptManager.GetPostBackEventReference) or implement an Ajax callback using a library like Microsoft ASP.NET Ajax.
Alternatively, if you don't really need to fire this event on every blur, and only when the text has changed, then you can simply use the server-side TextBox.OnChanged event and set the TextBox's AutoPostBack property to true. Make sure you remember to set AutoPostBack, otherwise this won't get you anywhere.
Use the TextBox.TextChanged event.
ASPX markup:
<asp:TextBox ID="txtProductID" runat="server" AutoPostBack="true" OnTextChanged="txtProductID_TextChanged" />
Codebehind:
protected void txtProductID_TextChanged(object sender, EventArgs e)
{
// do your database query here
}
This should do the trick, as referenced here: http://www.codedigest.com/CodeDigest/80-Calling-a-Serverside-Method-from-JavaScript-in-ASP-Net-AJAX---PageMethods.aspx
These are the controls
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" />
<asp:TextBox ID="txtTest" onblur="LookupProduct()" runat="server" />
This is the Javascript
<script language="javascript">
function LookupProduct()
{
PageMethods.LookupProduct('',OnSuccess, OnFailure);
}
function OnSuccess(result) {
if (result)
{
}
}
function OnFailure(error) {
}
</script>
This is the server sidewebmethod
[WebMethod]
public static bool LookupProduct()
{
return true;
}
Related
I have used this behind asp button click function. It works on local system but not after begin deployed on server. Why ?
public void EmployeeDeActivation()
{
hdnfieldSessionPersonalInfoID.Value = "0";
Session["ExtraPersonalInfoID"] = 0;
Response.Redirect("EmployeeInformation.aspx", false);
}
.aspx code:
<asp:Button ID="btnEmployeeActivated" runat="server" Visible="false" OnClick="btnEmployeeActivated_Click"
CssClass="btn btn-rounded pull-right btnEmployeeActivated" />
i.e. when i click button when on local system, it hits then button event and refrehes the page but when it doesn't work like then button click never hits.
Update:
protected void btnEmployeeActivated_Click(object sender, EventArgs e)
{
try
{
EmployeeDeActivation();
}
catch (Exception ex)
{
throw;
}
}
Doesn't this method need to accept an event handler? E.g.
protected virtual void OnClick(
EventArgs e
)
Also, the part of code where you set your hidden is not needed as you redirect after.
Also it has the wrong name as it doesn't match the onclick name
Try to enable Trace and log on every method in the page. Try to visualize what your code is doing during postback.
Another useful tool is Glimpse.
Hope it helps!
asp button property "Visible" is set to false in your code. how come button is rendering at first place ?
In my code a user can like a post when they click on the following code:
<li><%# Eval("Likes") %></li>
This will run the function LikePost in the codebehind:
public void LikePost(object sender, EventArgs e)
{
//like post whit given id using a database query
}
but how can I give that function a parameter, because it needs the postid from the post that the user is going to like.
Instead of an HTML link, use an asp:LinkButton which has a CommandArgument property. Something like this:
<asp:LinkButton
ID="LinkButton1"
Text='<%#Eval("Likes")%>'
CommandArgument='<%#Eval("ID")%>'
OnCommand="LikePost"
CssClass="icon fa-heart"
runat="server"/>
Then in your code-behind the signature takes a CommandEventArgs:
public void LikePost(Object sender, CommandEventArgs e)
{
// e.CommandArgument should contain the desired value
}
I have a button in ascx file, I want to call a method of my abc.aspx.cs file on clicking this button.
Is there a way to do this from ascx.cs file?
this is my button
<asp:ImageButton ID="ImageButton1" Style="padding-top: 80px; margin-right:10px;" runat="server" ImageUrl="~/App_Themes/Default/Images/Login/NH/DownloadButton.jpg" />
In order to do this in a clean way without coupling the page and the user control, you can create an event in the user control that the parent aspx form subscribes to.
For details on events in C#, see this link.
UserControl.ascx.cs
public class MyUserControl : UserContro
{
public event EventHandler ImageButtonClicked;
private void ImageButton1_Click(object sender, EventArgs e)
{
if (ImageButtonClicked != null) // Check against null as there may not be any subscribers
ImageButtonClicked(this, EventArgs.Empty);
}
// ...
}
WebForm.aspx
<!-- ... -->
<uc:MyUserControl ID="myUserCtrl" runat="server" ImageButtonClicked="myUserCtrl_ImageButtonClicked" />
<!-- ... -->
WebForm.aspx.cs
// ...
private void myUserCtrl_ImageButtonClicked(object sender, EventArgs e)
{
// Call method on page.
}
// ...
Please note that there an be no or many subscribers to the event. If you want to transmit data to the event handler, you need to create your own EventArgs implementation and use an instance of these instead of EventArgs.Empty. This also allows you to check whether the event has been handled by a subscriber, you can add a Handled boolean property to your EventArgs that is set by an event handler and evaluated in the user control afterwards.
There is not such a facility in asp.net But you can use generic handler(.ashx) instead of this
You can define a public method in aspx Page call it using this.Page
((YourASPXPage)(this.Page)).MyMethod();
In ASPX page
public void MyMethod()
{
//Your code
}
i have tried to close window(radWindow) using asp.net C#, but error on java script function is that 'object required' ,How can i solve this?
My java script and asp.net code is
<script type="text/JavaScript">
function Close() {
GetRadWindow().Close();
}
</script>
Asp.net code is
<asp:Button ID="btnSubmit" runat="server" Text="Submit Request" Height="27px" OnClick= "btnSubmit_Click" OnClientClick="Close();return false;" />
Optional try using C# for same operation as
C# code for button is
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Server code
ScriptManager.RegisterStartupScript(this,GetType(), "close", "Close();", true);
}
You call GetWindow() but you don't have this function .
Just add this code ,
function GetWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
This post may help you lot
http://www.telerik.com/community/forums/aspnet-ajax/window/getradwindow-close-cause-unspecified-error.aspx
i had same issues in the past but manged by the logic from the above post
Can AjaxManager_AjaxRequest only modify controls in its UpdatePanels or can it also send back JSON data in the response. Ideally just JSON data.
So in my ascx I have
protected void Page_Load(object sender, EventArgs e)
{
RadAjaxManager radAjaxManager = RadAjaxManager.GetCurrent(Page);
if (radAjaxManager != null)
{
radAjaxManager.AjaxRequest += AjaxManager_AjaxRequest;
}
}
private void AjaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
// Somehow tweak it so response is just a JSON object.
}
I just inherited some legacy code and this is the fastest way for me to accomplish my aim without creating a web service to do this properly.
Is there anyway to do what I'm asking?
Yeah, this possible. Actually there are exists two available solutions. The first one is to send ajax request with jQuery ajax method (you can access jQuery functionality with $telerik.$). The only lack of this approach is that target server-side method must be static and you can't access page's ViewState as well as server control's properties values. The alternative approach is to use ScriptManager's RegisterDataItem method in AjaxManager_AjaxRequest method to pass JSON-serialized object back to client and get it in Sys.WebForms.PageRequestManager's endRequest event handler on client.
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
function endRequestHandler(sender, args) {
var dataItems = args.get_dataItems();
if (dataItems && dataItems["<%= RadAjaxManager1.UniqueID %>"]) {
alert(dataItems["<%= RadAjaxManager1.UniqueID %>"].Response);
}
}
</script>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" />
void AjaxManager_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
{
ScriptManager1.RegisterDataItem(radAjaxManager , new JavaScriptSerializer().Serialize(new { Response = "Hello, RadAjaxManager!" }), true);
}