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
Related
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.
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
I am populating a ListView with HTML from a database using a Literal with Text='<%#Eval("HTMLData")'%>. When I trigger a PostBack, changes to the loaded HTML are not being reflected in litRowData.Text.
ViewState is enabled for the page, the ListView, and the Literal in the ItemTemplate, and I am making sure to only populate the ListView with initial values from the database when if(!IsPostBack) is true in Page_Load.
<asp:ListView ID="lvForm" runat="server"
DataKeyNames="RowID" ItemPlaceholderID="phRow"
EnableViewState="true">
<LayoutTemplate>
<asp:PlaceHolder ID="phRow" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:Literal ID="litRowData" runat="server" Text='<%#Eval("HTMLData")%>'
EnableViewState="true"></asp:Literal>
</ItemTemplate>
</asp:ListView>
I need to be able to capture changes to the contents of the loaded HTML controls. Since this HTML comes from a database table, I can't just use ASP controls inside the ItemTemplate. Can anyone see something I'm missing, or suggest an alternative way to do this?
Edit:
To clarify a little more, I'm trying to load form input elements dynamically from a database, render them as HTML controls on the page, allow the user to modify their contents by entering text or selecting options, then capture the modified HTML and save it back to the database when the user clicks a save button.
The way postback works in .NET is actually a wrapper around the more basic idea of HTML forms. A basic example of HTML forms is:
<html>
<body>
<form action="" method="POST">
<input type="text" value="type here" />
<input type="submit" value="go" />
</form>
</body>
</html>
Roughly, what the .NET abstraction adds is:
<html>
<body>
<form action="" method="POST">
<input type="hidden" name="__VIEWSTATE" value="string-encoded-value" />
<input type="text" name="bob" value="type here" />
<input type="submit" value="go" />
</form>
</body>
</html>
Whereby on postback to your page, all input elements with names are mapped back into properties of your Page object, and the __VIEWSTATE hidden field is deserialized into all properties of objects that do not correspond to values of html input tags. For example, if Page.bob had a DateTime property associated with it, it would be stored in __VIEWSTATE possibly.
ASP.NET Literal tags in Page markup will get printed into the browser exactly as is, meaning that if you have <span>bob</span> as its value, that is how it will appear within the <form> tag. However, in plain HTML world, <form> tags when posted will only contain the values of certain form elements (aka not every div, span, p etc. gets posted back, only input, select, textarea and some others). So if your literal doesn't contain an input then it won't even get posted back meaning __VIEWSTATE will be used to restore the Value property of the Literal back to its initial state.
To fix this, you probably don't want to stick html into a Literal because even if you do it's not clear that it will get associated with the right property of your page. Instead, try a TextBox element or something else that gets written as an input element directly by the ASP.NET webforms code. Alternatively, try using javascript to allow modifications of flat text in divs if you don't need to persist the data.
This answer builds on the prior one now that you have a .NET TextBox control that is correctly posting back the value of edits. Right below it, you can add to code behind:
protected void Page_Load(object sender, EventArgs e)
{
litRowData.Attributes.Add("onKeyUp", "WriteText(this.value)");
}
Html:
<ItemTemplate>
<asp:TextBox ID="litRowData" runat="server" />
</ItemTemplate>
<div id="yourPreview"></div>
<script type="text/javascript">
function WriteText(val){
document.getElementById("yourPreview").innerHTML=val
}
</script>
I have asp.net page (Form = runat server )
2 textboxes:
<input type="text" id="tb1" />
<asp:TextBox ID="tb2" runat="server" />
and a button submit.
However when I put some values in them - and press the button - Only tb2 is getting back its value from the server.
I thought that inputs fields values are going from client to server and back always !
in what event ( in page life cycle) Does tb1 lose its value ?
<input type="text" id="tb1" /> is literal HTML.
There is no server-side code that puts the value back in.
That feature is part of server-side controls.
The POST will contain information entered in tb1 - so it can be read from the Request
if you change it to
<input type="text" id="tb1" runat="server" />
it will work as you want.
without the runat=server it does not "lose" its value - it will never be populated - ever, because it is simply data in the POST and not connected to a server side control.
one final point - to explicitly answer some of your comments - the POST data is processed just before the PreLoad event - this can be found from http://msdn.microsoft.com/en-us/library/ms178472.aspx
tb1 is not a server side control, it is a normal html dom object.
You should find its value after a post in the Request.Form value collection however.
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.