asp.net can't take the text of input text - c#

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' />

Related

Browser Remember password set username to other textboxes also

When I use browser remembered password, after login, some textboxes in my forms shows username.
I am confused, how it works?
Please provide solution to avoid this problem?
Set TextBox property TextMode as Password
or try this
TextBox1.Attributes.Add("autocomplete", "off");
<form id="Form1" method="post" runat="server" autocomplete="off"></form>
Using Javascript you can also delete history of textbox
function disableautocompletion(id){
var passwordControl = document.getElementById(id);
passwordControl.setAttribute("autocomplete", "off");
}
Try:
<asp:TextBox ID="MyTextBox" AutoCompleteType="Disabled" runat="server" />
this renders as:
<input id="MyTextBox" name="MyTextBox" type="text" autocomplete="off" />
for more info MSDN for autocomplete
And this is helpful link

onsubmit function wont fire while having a runat=server attribute

<%# 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");
});
});

<asp:PlaceHolder> is not showing on iphone

I have a code in my master page that I used placeholder to show and hide 2 different parts.It is completely works fine in all the Browsers, but it is not working on Iphone Even it is working on Android.
this is my code:
<div class="actions">
<asp:PlaceHolder ID="phLogout" runat="server">
<input TYPE="button" VALUE="Log Out" class="Button Orange" onclick="window.location.href = 'Logout.aspx'">
</asp:PlaceHolder>
<asp:PlaceHolder ID="phLogin" runat="server">
<p class="Title">Current users sign in:</p>
<asp:TextBox runat="server" ID="username" holder="Username"></asp:TextBox>
<asp:TextBox runat="server" ID="password" holder="Password" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Log In" class="Button Orange" OnClick="btn_login_Click" ></asp:Button>
<p class="Forgot">Forgot password?</p>
</asp:PlaceHolder>
</div>
I just found out that this part is not showing in the iphone with all the content inside the .
what I can use instead of tag?
I tried but all the styling gets off. Is the only tag replacing this so I may work more on style sheet or you are suggesting something else? Or even if the reason for not showing is the ?
I believe the "holder" attribute in your textboxes should be "placeholder" instead.

How can I access the textbox value

<asp:DataList id="ItemsList"
BorderColor="black"
CellPadding="5"
CellSpacing="5"
RepeatDirection="Vertical"
RepeatLayout="Table"
RepeatColumns="3"
runat="server">
<ItemTemplate>
<a href="#" data-inline="true" data-role="button"
data-icon="star" data-iconpos="right">
<input type="text" id="txtTry" style="width: 20%" runat="server"
value="" data-mini="true"
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
</a>
</ItemTemplate>
</asp:DataList>
how can I access the value of the text box of each button in the CS. How can I access it.This is not tough but I didn't use it yet.
You need to change HTML controls <input>
to the ASP.NET controls, for example:
<asp:TextBox ID="txtTry" runat="server"></asp:TextBox>
Then later in your .cs file you can change the value of the textbox:
...
txtTry.Text = "foo";
...
yes change the html to Asp.net text box. Then you can get via method mentioned by oleksii
Even with input html text try the below sample It should normally be done with
Request.Form["elementName"]
For example, if you have
<input type="text" name="try" /> then you can use Request.Form["try"] to access its value.

Form fields not retained on postback

I'm developing a website using EPiServer. I have a form which submits to itself.
On submit, I check if there are any fields missing. If yes, then error message is shown.
The problem is that my fields are reset when submitted.
I could check this using jQuery, but I'm not. I'm cheking this from code behind.
I've tried setting EnableViewState=true sevceral places, but no luck.
Here's part of my code:
<asp:Content ID="Content3" ContentPlaceHolderID="RightContentPlaceHolder" runat="server">
<asp:Panel ID="panelComplaint" runat="server">
<li>
<h3>Postnummer*</h3>
<input id="senderPostCode" name="postCode" type="text" size="20" enableviewstate="true" />
<asp:Label runat="server" id="lblPostCode" CssClass="missingField" Text="Mangler tekst" Visible="false" />
</li>
<li>
<h3>Post sted*</h3>
<input id="senderCity" name="city" type="text" size="100" />
<asp:Label runat="server" id="lblCity" CssClass="missingField" Text="Mangler tekst" Visible="false" />
</li>
<li>
<div class="spacer10px"></div>
<button type="submit" name="Send" >Send me</button>
</li>
</asp:Panel>
</asp:Content>
What do I need to do, in order to retain form fields?
Have you tried adding runat="server" to the input fields? As far as I'm aware, this is needed to ensure that asp.net round-trips data properly. I'm not sure if there's anything specific to EPiServer that would make that differ.
The enableviewstate attribute, unless it's specific to EPiServer, won't actually be doing anything.
If having the ID mashed up by ASP.net is a problem, consider upgrading to .NET 4.0, if you can, as this provides ways for you to control how the ID's are modified. If not, you could try placing a friendly translation into the page that you can then access via JQuery, for example:
<script language="javascript" type="text/javascript">
function getSenderPostCode() { return eval('<%=senderPostCode.ClientID%>'); }
var senderPostCodeField = JQuery('#getsenderPostCode');
alert(senderPostCodeField.length());
</script>
That said, having "getSenderPostCode()" to reference would mean that you could probably skip the step with JQuery of getting a reference to it, so the following would work:
alert(getSenderPostCode().length();
By adding runat="server" to your controls you then wouldn't need to use Request.Form["senderPostCode"].ToString() to access the content of that field, you could instead access it directly:
var contentOfSenderPostCode = senderPostCode.Value.ToString();

Categories

Resources