How to get Span value on back end C# - c#

I created a client portal in asp.net, in which client give me feedback for that a ready paragraph is given and empty space is given for fill up using span
asp.net code :
<p style="font-size: larger; color:white; line-height:15pt; font-family:Arial;">
Hi my name is
<strong>
<span contenteditable="true" runat="server" id="Sname" class="text" data-placeholder="T.B. Giri" data-focused-advice="_____">
</span>
</strong>
</p>
i want this span tag value which may change by client side on button click...
thank you

Since you have made it runat=server you can access it on serverside. The span is a HtmlGenericControl at serverside:
string value = Sname.InnerText;
I would use a Label instead which is rendered as span.
However, in this case i'm fairly sure that you don't get the value that was changed at clientside since it is re-created from ViewState at serverside. If that's true you need to use a different approach like using a HiddenField and set it's value on clientside.

Use Hidden field to hold actual value and get the value from the hidden field, use span to display only

Related

Save the data values from jQuery textbox

I am working on an asp.net page where I am saving user input into my database using TextBox e.g ***<asp:TextBox ID="code" runat="server" Width="155px"></asp:TextBox>***
in this case I'm getting the ID of the TextBox as "code" so i can capture the values from that TextBox. Now i have a jQuery texbox <input type="text" id="date" required="required"/> where "date" is jQuery function ID. Which attribute or ID should i use to capture the user input and save them into my database using this <input type="text" id="date" required="required"/> textbox..?
you can change your plain textbox to asp textbox
<asp:TextBox Id="date" runat="server" required="required" />
if not than you can get values of plain Html elements in code behind.
Write a function to set the value of "date" textbox to a hidden
field
Than capture the value of hiddenfield in your code behind
Hope this helps
Why not simply turn that field into a asp.net control as well?
Would be:
<asp:TextBox Id="date" runat="server" required="required" />
It will be rendered as a input type="text" to the browser anyway and should work just fine.
In order to find the control (since ASP.NET sometimes have the odd interest in prefixing controls) you can just use $find in your script. Or, as someone pointed out, use class selectors (i.e assign a CssClass attribute to your TextBox) and use that as selector in your jQuery code.
That way the date value will be available both to server side operations and client side.
you can use Asp.Net textbox as jquery datepicker, just use class selector to convert into jquery ui date picker

Add DateTime Input to Database Asp.Net C#

I'm creating a datetime picker using code
<div class='input-group date'>
<input type='text' class="form-control" id='datetimepicker'/>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker').datetimepicker({
viewMode: 'years'
});
});
</script>
which allows me to create a datetime input. For the backend code, I am able to add fields to the database for asp:textbok's, asp:labels etc, but not for this input.
Here is an example of others I am adding
cmd.Parameters.AddWithValue("#jobList", jobList.Text);
cmd.Parameters.AddWithValue("#jobCriteria", jobCriterta.Text);
When I try to do this for datetime, the option for the ID=datetimepicker does not appear and I'm not able to add whatever datetime is in that input to the database.
Cheers in advance for any help I'm sure it's simple!
If you want to access an html control in the back end you need to give the attribute runat="server" along with the tag definition.
<input type='text' runat="server" class="form-control" id='datetimepicker'/>
Then change your script as like this:
$("#<%=datetimepicker.ClientID%>").datetimepicker({
viewMode: 'years'
});
After adding this to the tag you will get datetimepicker in the back end. and you can access it's value to do the rest of operations. and one more suggestion use .Parameters.Add() instead for Parameters.AddWithValue()
Replace the input tag with TextBox server control.
<asp:TextBox ID="datetimepicker" runat="server" ClientIDMode="Static" />
Remember to set ClientIDMode as "Static" to make sure jQuery code can find the textbox correctly. Then the backend C# code can also use this textbox as web control object.

Get the value of a textbox in codebehind (declared using HTML in an asp.net page)

I want to get the value of my input tag into my C#.
<div id="datetimepicker2" class="input-append">
<input data-format="MM/dd/yyyy HH:mm:ss PP" type="text"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar">
</i>
</span>
</div>// what should we use here?
The value is set by the user of the page.
I did't understand which control value you want to get. But If you want to get input element value into the code behind, you need to set runat="server" attribute, because this is a simple html element not a Asp control.
Add runat="server" and id="your_id" and you should have access to them.
for example:
<input type="text" value="Username" class="input-text autoclear"
runat="server" id="myTextBox" />
than you can simply get value of input box like this:
string myStringFromTheInput = myTextBox.Value;
For more options please See here
Try this
Add name for your input type
<input data-format="MM/dd/yyyy HH:mm:ss PP" name="txtBox1" type="text"></input>
and try this way for get value in codebehind
string value=Request.Form["txtBox1"];
You can access all your submitted form data at server side by looking into the form Request object.
Ex. Request.Form["txtDate"] OR Request["txtDate"].
Naming the html elements makes easier to look into form collection for specified element.
If what you posted is your actual code, you have an extra space in your closing tag
</asp: TextBox>
Should be
</asp:TextBox>
and then txt_todata.text should work

How do I read value from code behind on html page? Or any better way of doing that?

Solution: I end up creatting a WCF that accepts a get/post request, then place JQuery within the html page that retrieves the value and hands it off to the web service
I have a html page like below where I will be doing posting to a web site for registrations and my credentials are not suppose to show on the client side.
My question is how/what is the best way of reading the values from code behind or web service or any other way ?
<FORM NAME="web_form" ACTION="https://website.com/registration.php" METHOD="POST">
<TABLE WIDTH=961 BORDER=0 CELLPADDING=2 CELLSPACING=0>
<TR>
<TH WIDTH=380>
<P ALIGN=RIGHT><i>Encrypted Username:</i>
</P>
</TH>
<TD WIDTH=573>
<P><A NAME="username"></A><INPUT TYPE=TEXT NAME="username" VALUE="HOW TO GET VALUE???" SIZE=20 STYLE="width: 1.69in; height: 0.23in"></P>
</TD>
</TR>
<TR>
..............
.................
Switch the input box in to an ASP control?
<asp:TextBox runat="server" ID="username" ClientIDMode="Static" />
Then in your code behind:
this.username.Text
There's probably a little more that we need to know to make a perfect suggestion, but there's tons of different ways that you could do this.
If you have access to the registration page -- which would be ideal -- you could send a base64encoded string via the URL or via hidden input in the form field posted on submit to the reg page and then base64 decode it before passing in. Similarly, you could salt the value with any number of different methods and "un-salt" it at the application.
Merely hiding it in a hidden input field probably won't suffice here as that code is certainly available on a view source, unless the data inside it is sufficiently obfuscated.
There are javascript obfuscators that will do a sufficient job, but they'll be unavailable if the user has javascript turned off. It won't affect 99% of the users out there, but it is something to think about.
Perhaps you could set this up as a dynamic page (not HTML) and set some sort of a constant? Perhaps a session variable that you then call in at the registration page?
You can do it on client side with JavaScript:
INPUT TYPE="text" NAME="username" VALUE="some value" onclick="CopyValue()" />
function CopyValue()
{
var myValue = document.getElementById('username').value;
}
Not an answer but just a suggestion. In HTML always try to keep your values in quotes. Always a good programming practice. Will definitely help you if you are trying to move from HTML to XHTML or XML. Good Luck!

I am trying to hide text in a ASPX page that is tied to a textbox

So there is a a table and a text box in one of the cells
<td>
<asp:TextBox ID="tbSomeTextBox" Columns="5" runat="server"> %
</td>
This textbox gets shown if a certain selection is made in a drop down. The problem is that I would like the "%" character to also be hidden or shown with the textbox.
I have tried putting the whole textbox control inside a DIVand in my JQuery hiding the DIV at the same time I hid the textbox.
<td>
<div id="divSomeDIV"><asp:TextBox ID="tbSomeTextBox" Columns="5" runat="server"> % </div
</td>
But I get an error in my java script that id="divSomeDIV" doesn't exist in the current context.
$("#<%=divSomeDiv.ClientID%>").hide();
Wrapping that single character in a asp:Label seems like overkill.
Any suggestions?
divSomeDiv is running client-side (i.e. no "runat=server"), so there's no need for
$("#<%=divSomeDiv.ClientID%>").hide();
Just do
$("#divSomeDiv").hide();
create an asp.net label, set the label value to be %, and make them both visible or both not visible at the same time... ?
If you were getting "does not exist" in your Javascript it's probably because your Javascript is running before the div is created in the dom. In other words, this is incorrect.
<script>$("#divSomeDiv").hide()</script>
<div id="divSomeDiv">...</div>
You can either put the Javascript after the div
<div id="divSomeDiv">...</div>
<script>$("#divSomeDiv").hide()</script>
Or you can make your Javascript run after the DOM has loaded.
<script>
$(function() {
$("#divSomeDiv").hide()
});
</script>
<div id="divSomeDiv">...</div>

Categories

Resources