I have a JavaScript function as follows:
function getWindowSize() {
test.value = $(window).width();
test2.value = $(window).height();
}
window.onresize = getWindowSize;
And in the ASP.NET page:
<input type="text" id="test2" value="Test"/>
<input type="text" id="test" value="Test"/>
This populates the fields when the screen size changes. When I put the page elements to have runat="server" they don't populate the screen size and cannot capture it server side.
Any suggestions?
You need to use ClientID if you have server tag. You are not currently using getElementById but you should to get the DOM element for better browser compatibility. If you have framework 4 and above you can use ClientIDMode and do it without ClientID
Javascript
function getWindowSize() {
document.getElementById('<%= test.ClientID %>').value = $(window).width();
document.getElementById('<%= test2.ClientID %>').value = $(window).height();
}
window.onresize = getWindowSize;
HTML
<input type="text" id="test2" value="Test" runat="server" />
<input type="text" id="test" value="Test" runat="server" />
Since I cannot see the full code, I am going to assume ASP.NET replaces Id attributes with generated ones (something like _ctl$Container$test). As a workaround, if you use jQuery you can use CSS classes:
Javascript
function getWindowSize() {
$('.WidthValue') = $(window).width();
$('.HeightValue') = $(window).height();
}
HTML
<input type="text" id="test" value="Test" class="WidthValue" runat="server" />
<input type="text" id="test2" value="Test" class="HeightValue" runat="server" />
Are you using Masterpages? Because when adding ASP.NET form controls using masterpages they use a prefix to identity the controls:
<input id="ctl00_ContentPlaceHolder1_txtName" class="textfield" type="text" name="ctl00$ContentPlaceHolder1$txtName">
Where ctl00_ContentPlaceHolder1_ is the prefix of the masterpage.
This can be the cause why your javascript function doesn't populate the fields.
How do you identify your objects test and test2?
Related
I have:
<input name="input4" type="text" id="input4" disabled="disabled" class="jsx-3771882255" data-id="0">
I want get data of data-id by C#;
string s=input4.Attributes["data-id"].ToString(); //(can'tuse this!)
You have the disabled attribute set on your text box, this will not get submitted to the server, use the readonly attribute instead of the disabled attribute.
#vuong quang - Can you check my code and implement it's working for me and also I shared my code with a screenshot.
HTML Code
Add runat="server"
<input type="text" name="name" value="0" data-id="101" runat="server" id="txtvalue" />
HTML Code
Code behind
string id = txtvalue.Attributes["data-id"].ToString();
Check below screenshots
CS Page Code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" MasterPageFile="MasterPage.master" %>
<asp:Content ID="ContentPlaceHolder1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
<asp:Content ID="ContentPlaceHolder2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<br />
<div id="signupform">
<form class="signupform" name="formreg" runat="server" onsubmit="return valform()"> <br/>
<input dir="rtl" type="text" name="uname" class="uname"/> </br>
<input dir="rtl" type="text" name="fname" class="fname"/> </br>
<input dir="rtl" type="text" name="lname" class="lname"/> </br>
<input dir="rtl" type="password" name="pword" class="pword"/> </br>
<input dir="rtl" type="password" name="rpword" class="rpword"/> </br>
<input dir="rtl" type="text" name="mmail" class="mail"/> </br>
<input dir="rtl" type="text" name="rmail" class="rmail"/> </br>
<input dir="rtl" type="text" name="gil" class="gil"/> </br>
<input type="checkbox" name="anon" value="True" class="dropan"/></br>
<input type="submit" name="sub" class="subbutton" value=""/>
<%=registrationstatus %>
</form>
</div>
</asp:Content>
This is my code, My problem is that the onsubmit="return valform()" attribute on my form wont fire if the runat="server" attribute exists, they can't work together please help me I am clueless why these two attributes wont work together
This is because you can't use both together. As said in the docs -
<input type="text" id="Textbox1" runat="server">
Doing this will give you programmatic access to the HTML element on the server before the Web page is created and sent down to the client. The HTML element must contain an id attribute. This attribute serves as an identity for the element and enables you to program to elements by their specific IDs. In addition to this attribute, the HTML element must contain runat="server". This tells the processing server that the tag is processed on the server and is not to be considered a traditional HTML element.
Here is the reference.
http://msdn.microsoft.com/en-us/library/aa478973.aspx
under section - Using HTML Server Controls
So, HTML with run='server' will not be treated as a normal Html element and thus will always be processed by the server. If you want to override the default behavior, you have to bind your javascripts with after the html is loaded in the browser, may be using jQuery or something similar. With jQuery something like this would help -
$(function(){
$("#formreg").submit(function(){
return valform();
});
});
By examing your code, I reach on conclusion that you might be using another form tag on Master page and there is no javascript issue. Only one form tag(Server Side) can exists either on a master page or content page. They can not be nested. When you would use runat="Server" attribute on both of form tags then error occurs saying "A page can have only one server-side Form tag". So remove runat="server" attribute from one of the form tags, onsubmit would begin firing.
I solved it with very easy way.
if we have such a form
<form method="post" name="setting-form" >
<input type="text" id="UserName" name="UserName" value=""
placeholder="user name" >
<input type="password" id="Password" name="password" value="" placeholder="password" >
<div id="remember" class="checkbox">
<label>remember me</label>
<asp:CheckBox ID="RememberMe" runat="server" />
</div>
<input type="submit" value="login" id="login-btn"/>
</form>
You can now catch get that event before the form postback and stop it from postback and do all the ajax you want using this jquery.
$(document).ready(function () {
$("#login-btn").click(function (event) {
event.preventDefault();
alert("do what ever you want");
});
});
This is my aspx
<p>Date: <input type="text" id="datepicker" >
<asp:Button id="BookingForDate" runat="server" OnClick="BookingForDate_Click" Text="Search"/> </p>
<table id="DateBookingTable" runat="server" style="visibility:hidden">
<tr><th>ID</th><th>PlanTime</th></tr>
</table>
on my code in c# I can't see datepicker
why?
note please that i can take all the controllers from id except this one.
I already tried restart the visual studio
You have 2 options
add runat="server" to the input
<input type="text" id="datepicker" runat="server" />
or create an TextBox
<asp:TextBox runat="server" id="datepicker"></asp:TextBox>
You should add
runat="server"
to your input. No you are using an html control and not an asp.net server side control. Hence you can't access the value of the textbox the way you want.
So try the following and then you will be able to access the value of your textbox:
<asp:TextBox id="datepicker" runat="server"/>
Then in your code behind class you can access the value of this textbox
datepicker.Text
Another approach, as I pointed initially, it would be the following:
<input type="text" id="datepicker" runat="server">
You forgot to add the runat-attribute to your input-tag. Try this:
<input type="text" id="datepicker" runat="server">
With that change, you should be able to access the element from code behind
See MSDN
You have not given name to your control and also add runat="server"
<input type="text" id="datepicker" runat="server" name='datepicker' />
I am developing a website using asp.net c# and I want to put a form inside the page. Now as aspx pages have the form tag I do not want to nest another form inside this as it will invalidate my html. But I need this form to use GET rather than POST. I know I can change the postback url in the asp:button. Can this be done without using logic in the codbehind?
Change the method to GET just for this form not every thing on the page
change the target to _blank if possible.
Example in html of what I want.
<form action="http://maps.google.co.uk/maps" method="get">
<p><label for="saddr">Your postcode</label>
<input type="text" name="saddr" id="saddr" value="" />
<input type="submit" value="Go" />
<input type="hidden" name="daddr" value="[destination]" />
<input type="hidden" name="hl" value="en" /></p>
</form>
you can use jquery to accomplish this
$(document).ready(function() {
$("#buttonId").click(function() {
$("#formId").attr("method", "get");
});
});
the above snippet, fires when the button with id 'buttonId' is clicked. it changes the method attribute of the form with id 'formId'
You can have multiple forms in an html. ASP.NET page also supports multiple form tags, however only one of then can be server side form (runat="server").
So I will suggest that you add another form tag within your page - some thing like
...
<body>
<form runat="server">
... server controls etc
</form>
<!-- your form -->
<form action="http://maps.google.co.uk/maps" method="get">
<p><label for="saddr">Your postcode</label>
<input type="text" name="saddr" id="saddr" value="" />
<input type="submit" value="Go" />
<input type="hidden" name="daddr" value="[destination]" />
<input type="hidden" name="hl" value="en" /></p>
</form>
</body>
</html>
Note that you cannot put any server side control in your html tag. So you have to use html controls and manage them within page code using Request object.
i have
<input type ="text" id ="ID" value="" />
<input type ="text" id="Name" value ="" />
<input type="button" id="Submit" name="Submit" value="Submit" size ="45" />
when i click Submit button to pass values to .aspx page.
If you post a form to an aspx page you can get the submitted values through the Request.Form collection. From your example:
You'll need to set the action on your form to the aspx page:
<form action="mypage.aspx">
And set the name on your field:
<input type ="text" name="ID" id="ID" value="" />
You can get them on the aspx with:
string id = Request.Form["ID"];
Have you tried something like
<input type ="text" id ="ID" value="" runat="server"/>
Have a look at ASP.NET - Server Controls