Clear <input> field from code behind - c#

To pass values from javascript to the code behind after a postback I use this code:
string strRowNumberTblOne = Request.Form["iRowNumberTblOne"];
<input type="hidden" id="iRowNumberTblOne" name="iRowNumberTblOne" value="" />
Is there a way to clear the input field from the code behind?
The Request.Form is read only.

Add runat="server". Then set its Value property.

Try this Instead of you can use Hidden TextBox , like this
<asp:TextBox ID="TextBox2" style="display:none" runat="server"></asp:TextBox>
In JavaScript
varResult = document.getElementById('<%= TextBox2.ClientID%>');

Related

how to get data-* attribute with asp.net ()

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

FindControl CodeBehind Issue

I have the following code in my aspx file:
<input type="text" runat="server" id="test" name="test" onBlur="OnBlurFunction()"/>
When I click a submit button:
<asp:Button runat="server" id="SubmitButton" OnClick="SubmitButton" Text="Submit" />
The function "SubmitButton" contains the following:
System.Diagnostics.Debug.WriteLine(this.FindControl("test"));
When I run this, it always prints out a blank value and I'm not sure why. If I set the value in the HTML line as follows:
<input type="text" runat="server" id="test" name="test" value="hello" onBlur="OnBlurFunction()"/>
It prints "hello" with no issues. But when I manually change the value in the text box to something else, it always to print the first value out. It's as if something isn't dynamic. Am I doing something fundamentally wrong here?
Try This -
HtmlInputText tb1 = this.test;
System.Diagnostics.Debug.WriteLine(tb1.Value);
This is the thread - find control and html tags
You must use parent control(like asp:panel or runat server div) instead of this to call FindControl method.
Like this:
pnl1.FindControl("test")

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

How to reference code behind variable in ASPX?

I'm not sure why when referencing a code behind variable in an asp.net control, I get the text of the reference:
<%=this.Person.Contact.Emails[0].EmailAddress%>
This outputs the literal reference text:
<asp:TextBox ID="EmailAddress" runat="server" Text="<%=this.Person.Contact.Emails[0].EmailAddress%>"></asp:TextBox>
This renders the variable value:
<input id="testfield" type="text" value="<%=this.Person.Contact.Emails[0].EmailAddress%>" />
Any ideas how I can get the variable value in the asp.net control?
You could say:
EmailAddress.Text = this.Person.Contact.Emails[0].EmailAddress
in your code behind
I prefer the solution in Code Behind in hunter's solution but another option would be to use data binding with #:
<asp:TextBox ID="EmailAddress" runat="server" Text="<%# this.Person.Contact.Emails[0].EmailAddress%>" />
But then you have to bind the server control in code-behind:
EmailAdress.DataBind();
The = sign is like a call to Response.Write() at this place and just outputs whatever follows as text.

how to pass a value to the code behind asp?

Erm i am new to this. How do i pass the value the user enters into a text box to the vb code behind?
<input type="text" runat="server" id="amount" name="amount" size="15" />
No need to pass the value As its RunAt = server you can directly access value of the text box using its Text property
Example
amount.Value
or you can make use of Request collection to get the value of the textbox Request.Form["amount"]
Use the ASP.NET-TextBox Control.
<asp:TextBox ID="TextBox1" runat="server"/>
Then you can access it from codebehind via it's ID
Dim Textbox1Text As String = Me.TextBox1.Text
The Text will automatically persisted in ViewState across postbacks by default.

Categories

Resources