How to transfer data between javascript and ASP.NET server program - c#

I am trying to capture data entered by a user through javascript running on my webpage, then transfer that data to ASP.NET server program that actually created that webpage.
So I created a asp:HiddenField as follows:
<asp:HiddenField ID="BusesFromDrawing" runat="server" />
And in the javascript I set its value (and make sure it's set by displaying it in a div).
Then in my C# server application I write this in a button handler - it is supposed to simply take the data from the hidden field and paste it in a textbox.
string allconfigs = BusesFromDrawing.Value;
tb3.Text = allconfigs;
and tb3 has the following declaration:
<asp:TextBox ID="tb3" Text="Default Text" runat="server" />
However, I always end up with an empty string replacing "Default Text" even though I put some text in the hidden field and it shows up in the javascript portion.

The problem seemed to be in the javascript portion, when setting the hiddenfield I was using:
BusesFromDrawingField.Value = somestring
I just needed to use a lower-case 'v':
BusesFromDrawingField.value = sometring
even though in C# I use an upper-case 'V' when asking for the value!

Related

Set value from server side code to an asp.net TextBox with type=“Time”?

I want to assign value to HTML5 text box of type="Time" from code behind of asp.net c#.
HTML Part :
<asp:TextBox ID="txtStartTime" runat="server" type="time"></asp:TextBox>
Code Behind :
txtStartTime.Text = myObj.StartTime;
StartTime is a string variable and always contains time as a string. After loading page text box shows no value. But using firebug it shows there are value.
I have checked this link.
you are using c# TextBox not HTML5 TextBox. in your HTML part you've used property type="time"
i think there is no property type. There is property TextMode. type is for HTML TextBox.

Read asp.net link button text from server side

net page with two controls
linkbutton
button
While running my application, I'm changing my link button text using javascript function.
Now I want to read that text when I press button. Button event is there in server side.
When I try to read like below
string s = linkButton.Text;
It is not giving my updated text.
How can I get it?
At first, declare this HiddenField in your markup
<asp:HiddenField ID="link" runat="server" />
Then in the function, you change the link button text, you should add the following code, in order the new text to the HiddenField been added.
document.getElementById(<%=link.ClientID%>).setAttribute("Value",newText);
Last, in your server side code you can get the value you want with the following way:
string s = link.Value;
You can use a HiddenField.
The LinkButton does not implement IPostBackDataHandler, therefore it doesn't load postback data.
You can write the HiddenField.Value on client- and read it on serverside.
Here's a tutorial-video: [How Do I:] Use a Hidden Field to Store and Manipulate Client-Side Information

TextBox value not assigning to variable in asp.net

Java Script
function outputtax()
{
var tamount = parseFloat(document.getElementById('<%=txtpsubtotal.ClientID%>').value);
var cash = parseFloat(document.getElementById('<%=txtpdiscount.ClientID%>').value);
if (isNaN(tamount) != true && isNaN(cash) != true && isNaN(tax) != true)
{
document.getElementById('<%=txtPtotalamout.ClientID%>').value =
Math.round(parseFloat(document.getElementById('<%=txtpsubtotal.ClientID%>').value)
- parseFloat(document.getElementById('<%=txtpdiscount.ClientID%>').value))
return false;
}
}
<asp:TextBox ID="txtPtotalamout" runat="server" ReadOnly="true">
</asp:TextBox>
.CS
objsupplyPL.totalamount = Convert.ToDouble(txtPtotalamout.Text.ToString());
Value is displaying on the textbox but when i click save button txtptotalamount is getting
null value.If I placed readonly="false" it's working fine.
From http://codecorner.galanter.net/2009/10/09/postback-disabled-textbox/
Let’s say in your ASP.NET application you set a TextBox control’s property ReadOnly to True (or Enabled to False) to prevent user from entering data directly. But you still want to update that text box’s value via client-side JavaScript. Which is happening, the value can be updated. But during postback to the server – surprise, surprise! – the new value doesn’t persist. This is due to security precaution – if the value wasn’t meant to be changed – the change is not allowed. But there is a way around this.
The trick is - to keep ReadOnly = False and Enabled = True and simulate their behavior. Add following line of to your server-side code:
TextBox1.Attributes["onclick"] = "this.blur();"
where TextBox1 is your textbox control. What this line does is adds client-side behavior to the textbox. As soon as user tries to click the textbox, focus immediately gets lost, preventing user from entering data, making the textbox essentially read-only. For further effect you can set the texbox’s background to something like “LightGray” making it appear disabled.
You want to be able to save the result from the "txtPtotalamout" but you don't want it to be editable.
You could just use
<div id="PTotalAmount"><asp:Label id="PTotalAmount" runat="server" /></div>
<asp:HiddenField ID="hPTotalAmount" runat="server" />
To display it, and update the contents of that DIV and the hidden field in the javascript.
Then you could display the total amount in that DIV when you load the form (and populate the hidden field). You could even format the DIV to look like a text box if you wanted.

How do I get the RadTextBox TextMode=Password to accept the Auto Filled value in the code behind?

Google Chrome and FireFox browsers have a feature that allows users to save user name and passwords to their local machine. This allows them to login the next time without much effort or remembering what that information is. The information is basically auto-filled. When those browsers do this it shows the * in the textbox. Showing that the browser successfully placed the value in the RadTextBox. However, when the c# code behind retrieves the value as an empty string.
My code is as follows
<telerik:RadTextBox ID="txtPassword" runat="server" ToolTip="Enter password"
Text="" MaxLength="40" EnableSingleInputRendering="False"
Wrap="False" TextMode="Password" CausesValidation="false" Skin="WebBlue">
</telerik:RadTextBox>
Code Behind:
objLogin.Password = txtPassword.Text;
Even odder is the fact that if you click the button more than once it will then the code behind will see the actual value and proceed to log you in. It seems that the RadTextBox Control needs to be rendered a second time or post back is required for the control to work properly.
What do I need to do to get the RadTextBox control to recognize the inputed value from the browser initially without having to click multiple times for the code behind to see the value?
Thanks for your help.
That is because your textboxes gets the value on the client side alone, while the underlying RadTextBoxes values do not.
I have experienced this issue before where values set to a RadTextBox by using jQuery xxx.val('...') do not get through to the server-side on postback.
The only way to get the value 'assigned' to the RadTextBox control's Text property from client-side is to use:
$find('<%= txtUsername.ClientID%>').set_value('newvalue');
In your case, most probably the browsers set (autofill) the values and only available on client side. Therefore, what you can do to actually assign those values is to intercept before the Login button postbacks, like so:
var un = $('#<%= txtUsername.ClientID%>').val();
var pw = $('#<%= txtPassword.ClientID%>').val();
$find('<%= txtUsername.ClientID%>').set_value(un);
$find('<%= txtPassword.ClientID%>').set_value(pw);
If you are using RadButton as the Login button, this can be done on the button's OnClientClicking/OnClientClicked event like so:
<telerik:RadButton ID="cmdSignin" runat="server" Text="Sign In"
SingleClick="true" SingleClickText="Signing In..."
OnClientClicking="OnClientClicking"></telerik:RadButton>
Hope that helps.

Display url in text box

I have a textbox as follows:
<asp:TextBox runat="server" ID="txtOtherApps" Height="400" Width="400"
TextMode="MultiLine" ontextchanged="txtOtherApps_TextChanged" ></asp:TextBox>
How to display link in this textbox?
The TextBox allows you to display text which the user can edit. It does not allow you to display anything but plain text. To display a URL in the TextBox, simply set its Text property:
txtOtherApps.Text = "http://www.example.com/";
It wont, however, be a "link". Clicking the URL will cause a text cursor to be placed, allowing for the user to edit the URL.
It is possible if you use JavaScript
Use JavaScript on your text element - such that:
<input type="text" name="t1" id="t1" value="http://www.google.com" onmouseover="this.style.cursor='pointer' ;" onClick="window.open(this.value);"/>
Only Java script can do what you are asking for.
You won't be able to click on the link, but you can just set the Text property of the TextBox to the URL.
ASP.NET will render TextBoxes as textareas (in your case, because it's multiline) or inputs. These are standard HTML fragments which are just plain text containers. You can style them, but you can't really linkify the contents of them.
If you really just want to put the text of a link into a box, do this:
// either from the server:
txtOtherApps.Text = YourLinkString;
// or from the client:
<script>
document.getElementById('<%=txtOtherApps.ClientID%>').value = YourJsLinkValue;
</script>
If you want something to happen with the user clicks on the text area, you can add an onclick handler to it...but this would be weird.
You will need a RichTextBox. The .NET one is not available for web applications, but there are a few third party solutions available.
http://www.richtextbox.com/ is one of them, you will have to see for yourself if there is any available that suits your needs better.

Categories

Resources