I am using asp.net file upload
<asp:FileUpload ID="ImageUploader" runat="server"/>
how can i detect that asp:FileUpload has a file using Jquery?
I am doing this
$("#ctl00_MainContentPlaceHolder_UCUpdOrgProfile1_ImageUploader").change(function (e) {
alert("hello")
});
But i dont know either file is selected or not.
check -
if (document.getElementById('<%= ImageUploader.ClientID %>').files.length === 0)
{
// File upload do not have file
}
else {
// File upload has file
}
Valid answers, or since ASP 4+ just set ClientIDMode as a page property:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" ClientIDMode="Static" %>
The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.
- reference
ie. this will force ASP to obey the ID as declared and not generate the "ctl00_" prefix or any other.
This is awesome as it is applied to the whole page and all asp controls contained on it, no extra code, no hacks just one awesome property.
So apply the same logic as #Microsoft_DN solution, but use static ID's.
Related
I cannot post my code but I want to know how I can write in resource file and other the link text in masterpage like and dropdownlist multilinguage in asp.net c# website.i need to use global app.global resources due to my desire to localize it in many languages to handle the files.i will be happy if you show me in asp.net website only i do not have much knowledge in mvc and web application and moreover what i started to do is in asp.net website.
<li>Buy</li>
<li>Rent</li>
<li>Estimate</li>
<% if (Session["user"] == null)
{ %>
<li>Sell</li>
<% }
else
{ %>
<li>Sell</li>
<%} %>
<asp:ListItem>Select the Category</asp:ListItem>
<asp:ListItem>Villa</asp:ListItem>
<asp:ListItem>Apartment and Condos</asp:ListItem>
<asp:ListItem>Farm</asp:ListItem>
<asp:ListItem>Office</asp:ListItem>
<asp:ListItem>Store</asp:ListItem>
<asp:ListItem>Storey House</asp:ListItem>
<asp:ListItem>Plot</asp:ListItem>
<asp:ListItem>Shop</asp:ListItem>
<asp:ListItem>Other commercial</asp:ListItem>
</asp:DropDownList>
Add resource files for the different languages, e.g. LocalizedText.resx (fallback language), LocalizedText.fr.resx (french), LocalizedText.de.resx (german), etc.
Add keys and values for the texts to be translated to these resource files, e.g. Villa, ApartmentAndCondos.
Then ASP.net will resolve the correct language version of the resource file to be used based on the language sent by the browser.
<asp:ListItem><%$ Resources:LocalizedText, Villa %></asp:ListItem>
<asp:ListItem><%$ Resources:LocalizedText, ApartmentAndCondos %></asp:ListItem>
You can use the <%$ Resources:{FileName}, {ResourceKey} %> command anywhere to return translations. E.g. in a link:
<%$ Resources:Resources:LocalizedText, Sell %>
See Walkthrough: Using Resources for Localization with ASP.NET.
I am using asp.net. I have noticed that we can configure page title (static and dynamic both) in two ways:
We have a Title attribute in the page directive:
<%# Page Language="C#" Inherits="_Default" Title="My Title" ......%>
We also have <title>tag in the page:
<title runat="server" id="MyTitle"> My Title</title>
Both can be accessed in code-behind file:
MyTitle.Text = "Title from Code behind";
Page.Title = "Page Title from CS";
And i have found the page directive overrides the html title. So Which one should we use and why ?
The biggest difference is that with MyTitle.Text you have to decorate Title element with an id AND runat attributes, and remember it's name so you can reference it. Also accessing this value isn't that easy from child pages when you're using Masterpage for instance..
On the other hand, Page.Title is common to every Page, so it's more universal in my opinion. Every new developer you'll work with won't have to learn anything new, just use the Page.Title format..
So my vote would go to the "traditional" Page.Title
Whichever you like to use, stick with it, so you won't mix various ways of setting the title. That way you won't have to worry about which event happens first or about your colleague overwriting your values.
Short answer : ( it depends on your needs). i'll explain.
it depends if your title should change at runtime.
Long answer :
Here is my observation(with a small test) :
I set title at the Page directive :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="Exampales_test" Title="ppppppppp" %>
I also set via html :
<head runat="server" ID="hhh">
...
<title runat="server">fffffffff</title>
</head>
I have this test code :
protected override void OnPreInit(EventArgs e)
{
// <----- breakpoint here (A)
base.OnPreInit(e);
// <----- breakpoint here (B)
}
protected void Page_Load(object sender, EventArgs e)
{ // <----- breakpoint here (C)
this.Title = "cccccccc";
}
Visual :
Now let's see :
When I press Run :
The Page does have the html title value (ffffff)
Also at the end of PreInit
Investigating the OnInit shows it has already changed (in the way {if you want to know exactly in what stage - this can be done}) to ppppppppp
And of course the last event ( among what we've talked about) is the page_load : which does change the value :
And the value is :
So which to choose ?
If your code is changing the Title dynamically (I mean at runtime), don't use the html markup at all nor the page directive.
e.g. if your code is (for example) has ASCX and that ACSX should change the title , then just use code ( not directive nor html markup).
As you noticed already - the value which wins is The one who occurs last(timeline)
It starts with the html markup value , but then the server side code begins to activate and changes values.
Here is a good article on the differences. They basically do the same thing. It's just WHAT you want to do that matters.
While the <title> can be set statically in an ASP.NET web page, in
many scenarios the title is dependent upon the data displayed in the
page. For example, a website might have a
ShowProduct.aspx?ID=productID page. Rather than using a static
<title>, the value of the <title> would ideally be the name of the
product being viewed (that is, the product whose ProductID equaled the
productID value passed through the querystring). Unfortunately, in
ASP.NET version 1.x, setting any HTML metadata elements (such as
<title>) required that the developer add a Literal control in the
proper place in the HTML markup and then set its value
programmatically in the ASP.NET page's code-behind class.
Copied from here
https://web.archive.org/web/20210422200927/https://www.4guysfromrolla.com/articles/051006-1.aspx
I know you can't use asp net server tags in an external javascript file. This is a bit of pain, because it forces you to declare your variables that need ClientID in the aspx page and then you refer to them in the external javascript file. Not very clean. Currently I use script manager's composite script to register my scripts... It would be nice if I could have the script injected and the server tags processed as if it was part of the page. Is this possible?
I know there is RegisterClientScript but this doesn't seem to honor the script tags either. I'm wondering if there is a solution someone has come up with to just pull the contents of the javascript file and shove them into the aspx page before it's processed so that the server tags can be processed. I've looked all over the web and don't see any good solution to this beyond using the server tags in the aspx page or generating the ids of controls, etc. server side and generating script.
I know you can't use asp net server tags in an external javascript
file
You can create an ASPX page to generate dynamic javascript
<%# Page Language="C#" AutoEventWireup="false"
CodeFile="script.aspx.cs" Inherits="scripts_script"
EnableViewState="false" StyleSheetTheme="" %>
function test() {
testinfo.innerHTML = "<%= MyVariable %>";
}
Make sure to set StyleSheetTheme="" otherwise the runtime will insert a <head> which you don't want
And in the code behind set the ContentType to application/x-javascript
using System;
public partial class scripts_script
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Response.ContentType = "application/x-javascript";
}
}
Now you can use this ASPX page as if it were a .js file.
So I'm trying to use C# to grab stored HTML in a database and then display it between a set HTML header and footer (I have a couple of these for different pages, so a masterpage would make life more complicated rather than less. I also come from a PHP background where this is sort of the way things work).
<!--#include virtual="header.html" -->
<% #Page Language="C#" Debug="true" %>
<% #Import Namespace="System.Data.Odbc" %>
<% #Import Namespace="System.Web.Configuration" %>
<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e) {
//Gets the HTML Successfully
//Response.Write(fetched_html);
}
</script>
<!--#include virtual="footer.html" -->
Unfortunately with the current workflow the code generates content before the header or footer are put on the page, putting all the stored HTML at the top of the page before the header. I've tried a couple different Page_Events and none of them seem to work, so I need to know what to do.
Is there an equally simple way to reference and insert a simple html header file that would render before any of the Page_Events? I've tried Page_PreLoad and others to no success. Either the code is not fetched at all or I run into the same issue.
Use a MasterPage with a ContentPlaceholder. Then, on the content page, use a Literal and put the HTML in there.
See "The difference between literal and label".
Try this:
Assuming you have data holding some where in memory like datatable or dataset or collection.
To display a html text on the page we generally use <asp:Literal/>. So place a asp literal control on the page. It works in between <body> </body> tag.
Now render the text to the control using its Text property. e.g.
ltl.text = dt.rows[0]["htmlcol"].tostring();
To dynamically add text inside <head> </head>, u need HtmlMeta,Page etc class.
i have an admin master page where i want to place a label which i can control with two functions (setErrorMessage and setSuccessMessage) both functions assign strings to label's text property and change CssClass property according to function type.
I want to use these functions from nested pages while the control remains centralized on master page so i can return to form in case of error so user could edit wrong input.
How would you suggest me to do that ? either VB or C#
thanks
You can use below in .aspx
<%# MasterType VirtualPath="~/MasterPages/Default.master" %>
and below in your code behind,
this.Master.yourMethod
this.Master.yourProperty
to access your controls in child page.
you must convert type of Master property in nested page:
((MyMasterPage)this.Master).lblMessage.Text = "Hi.";