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.
Related
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
There is a complex function written in jquery that runs if the href below is clicked by the user. Is there any way I can simulate this click in the code behind of the asp form? In other words, I need to "click" the href called "shipping_question_ID_product_received_as_ordered_link" from my code behind to prepopulate this input. Can anyone assist?
<div class="no"><label class="label-1" for="shipping_question_ID_product_received_as_ordered_not_acceptable">Not Acceptable</label></div>
<div class="yes"><label class="label-2" for="shipping_question_ID_product_received_as_ordered_acceptable">Acceptable</label></div>
<label class="universal-label"></label>
<input type="radio" id="shipping_question_ID_product_received_as_ordered_not_acceptable" name="shipping-question-ID-product-received-as-ordered" value="Not Acceptable" runat="server">
<input type="radio" id="shipping_question_ID_product_received_as_ordered_acceptable" name="shipping-question-ID-product-received-as-ordered" value="Acceptable" checked="true" runat="server">
#ArindamNayak had the right idea, but there may be issues with his implementation. If you add the below script to your code-behind, you will achieve what I believe is your desired result:
var autoRunScript = string.Format("<script>$(function() {{ $('#{0}').click(); }} );</script>", shipping_question_ID_product_received_as_ordered_link.ClientID);
Page.RegisterClientScriptBlock("keyClientBlock", autoRunScript);
The difference here is that without wrapping your $(link).click() in a ready handler, jQuery may not process the command correctly. Additionally, you have to be careful with the ID that you use as it could be modified to include containers.
You can use following.
string jScript;
jScript="<script>$('#linkid').click()</script>";
Page.RegisterClientScriptBlock("keyClientBlock",jScript);
So this will add a JS code block programatically to aspx and this JS will execute to click the a href ( having id = "linkid"
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 can't manage to set a date and to get a date of Datepicker in code behind(C#)
does anyone have informations to give me about this ? thanks in advance!
<input id="datepicker" value="01/01/2011" type="text"/>
$(document).ready(function () {
$("#datepicker").datepicker();
});
<input id="datepicker0" type="text" name="datepickernm"/>
You can get the value from below code in the codebehind:
String text = Page.Request.Form["datepickernm"]
Use an ASP textbox control:
<asp:textbox runat="server" id="txtDate"/>
<script type="text/javascript">
$('#<%= txtDate.ClientID %>').datepicker();
</script>
This way you could get and set the value from code behind.
The Calendar control in asp.net will provide you with the functionality you need. Drag the control from the Toolbox in Visual Studio
then access the Calendar control from codebehind using the ID
The above method does not allow me to set the value from the code behind. I am only able to get the date from the code behind.
When i remove the javascript form the html header, it seems to works.. i think that the javascript is overriding the value in the textbox.