How to get Textbox control in C# - c#

I have a HTML textbox
<script>
$(function () {
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true
});
});
</script>
<input type="text" size="10" name="datepicker" id="datepicker" />
Now i need to set the value of textbox to "abcd";
I tried datepicker.Text = "abcd"; Error is "the name datepicker does not exist in current context"...
I tried finding the Control and assigning the value but still could not do it.
Is there any other way to do it??
Thanks

You'll first need to make your <input> control a server side tag, using the runat="Server" attribute:
<input runat="server" type="text" size="10" name="datepicker" id="datepicker" />
Then, you can modify the value using C# code:
protected void Page_Load(object sender, EventArgs e)
{
datepicker.Text = "New Value"; // Initial value for input field
}
If you want to modify the value on the client side, using jQuery, I'd first suggest making the ID static:
<input runat="server" ClientIDMode="Static" type="text" size="10" name="datepicker" id="datepicker" />
Then you can do:
$("#datepicker").val('New Value');
Anywhere after the page has been loaded.

Related

Cant get values of inputs asp.net

My problem i always get null from my inputs or default value. Some how if i set value at page_load like Form_txt_Ad.Value="ExampleValue"; i can get it. But i cant get any value from inputs.
protected void Save_Button_Click(object sender, EventArgs e)
{
string exapmle = Form_txt_Ad.Value;
string example = Form_txt_Soyad.Value;4
}
<div class="input">
<input type="text" translate translate-attr-placeholder=".PLACEHOLDER_NAME" placeholder="Ad" id="Form_txt_Ad" runat="server" />
<span><i class="glyphicon glyphicon-user"></i></span>
</div>
<div class="col-md-12" style="text-align: center;">
<button type="button" runat="server" onserverclick="Save_Button_Click" class="btn btn-success btn-raised btn-lg" title="Kaydet"><i class="glyphicon glyphicon-floppy-saved icon-marginRight"></i>Kaydet</button>
</div>
Thx for help.
Make sure all your control elements are placed inside <form> ... </form> tag.
Since you have placed runat="server" you should be able to get the value by either using any of them
Form_txt_Ad.Value
(OR)
Form_txt_Ad.Text
Else use Request.Form["Form_txt_Ad"]
Not sure though why not use a server side control using <asp:TextBox ... which will allow you to get the textbox value directly using the Text property
Add the name attribute to your input and make sure it's inside a form element.
<form>
...
<input type="text" translate translate-attr-placeholder=".PLACEHOLDER_NAME" placeholder="Ad" id="Form_txt_Ad" name="Form_txt_Ad" runat="server" />
...
</form>

How to get the value of a textbox in code-behind

HTML:
<input type="text" runat="server" value="" placeholder="Search" id="searchB" class="styledTB searchB floatLeft" />
C#:
string strSMain;
protected void Page_Load(object sender, EventArgs e)
{
tbSearchMain = (System.Web.UI.WebControls.TextBox)sender;
strSMail = tbSearchMain.text; // gives me the following error: Exception Details: System.InvalidCastException: Unable to cast object of type 'ASP.site_master' to type 'System.Web.UI.WebControls.TextBox'.
strSMain = searchB.text; //.Text is not an option for me
}
Please help me resolve the issue.
I am creating a web application. And the control is in the MasterPage.
Use this:
<input id="searchB" x:Name="searchB" type="text" runat="server" value="" placeholder="Search" class="styledTB searchB floatLeft" />
The id attribute is used client side. x:Name is used for server side manipulation
I am not sure but no textbox will be sender for Page_Load() function.
Check about DataContext Property and how it works.
http://www.wpf-tutorial.com/data-binding/using-the-datacontext/

Defaulting hidden field value using inline server code

I have an ASP.NET page where I defined a property in the code behind as follows:
public int testProperty { get; set; }
In my page I define a hidden field and want to set the value using an inline server code as follows:
<asp:HiddenField ID="hftestProperty" runat="server" Value="<%= testProperty.ToString() %>" />
The problem I am having is that when the control renders in the browser, it renders the value the same way I defined it:
<input type="hidden" name="hftestProperty" value="<%= testProperty.ToString() %>">
Any idea why this is happening?
Try with this:
<input id="hftestProperty" type="hidden" value="<%=testProperty.ToString()%>" />
it render:
<input id="hftestProperty" type="hidden" value="0" />
You're going to have to set the value of the hidden field in the code behind, for example in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
hftestProperty.Value = testProperty.ToString();
}
}
You might need to cast testProperty to string for hidden field. If you are going to use the string value of testProperty at Server then testProperty.ToString() will always be available to you.
Try following code:
<input type="hidden" id="hftestProperty" value="<%= testProperty%>" />
For more info you may see this question.

Accessing <div> from both javascript and code-behind

I have a div with style="display:none". The div should become visible on pressing an html button:
function JSAdd() {
document.getElementById('divDetail').style.display = "block";
}
<div style="float:left">
<div id="ctl00_MainContent_upnlLbRD">
<select size="4" name="ctl00$MainContent$lbRD" id="ctl00_MainContent_lbRD" style="width:188px;">
<option value="5">one</option>
<option value="1">two</option>
</select>
<input id="btnAdd" type="button" value="Добавить" onclick="JSAdd();" />
<input id="btnEdit" type="button" value="Редактировать" onclick="JSEdit();" />
</div>
<div id="ctl00_MainContent_divDetail" style="display:none" clientidmode="static">
<div id="ctl00_MainContent_upnlDescription">
<div>
<span id="ctl00_MainContent_lblDescription">Описание:</span>
<input name="ctl00$MainContent$txtDescription" type="text" id="ctl00_MainContent_txtDescription" />
<span id="ctl00_MainContent_txtDescriptionRequiredFieldValidator" style="color:Red;visibility:hidden;">Описание является обязательным для заполнения</span>
</div>
<input type="submit" name="ctl00$MainContent$btnSave" value="Сохранить" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$btnSave", "", true, "", "", false, false))" id="ctl00_MainContent_btnSave" />
I need to be able to make the div invisible again from code-behind. I cannot access the div unless it is runat="server". But when I add runat="server", the div doesn't become visible on pressing the button from the javascript function above. Could you please help me with this?
Thanks,
David
You can access a div in code-behind by adding the runat="server" attribute. Adding this attribute does change the way you access the element in JavaScript though:
var el = document.getElementById("<%=div1.ClientID%>");
if (el){
el.style.display = "none"; //hidden
}
There are two ways to adjust the visibility from code-behind, but since you're setting display:none in JavaScript, you'd probably want to use the same approach in code-behind:
div1.Style["display"] = "block"; //visible
In code-behind, you can also set the Visible property to false, but this is different because it will prevent the element from being rendered at all.
EDIT
If the div is still showing with display:none present, you probably have an unclosed tag or quote somewhere affecting the markup. Double check and make sure that the markup is valid.
Use a Panel, it renders as a classic div
<asp:Panel runat="server" ID="divDetail" ClientIDMode="Static" />
You have a few options, use ClientIDMode="Static" or use the dynamic ClientID at run-time. Both of these options give you server-side access to the object.
Dynamic:
<div id="divDetail" runat="server" />
//or
<asp:panel id="divDetail" runat="server" />
function JSAdd() {
document.getElementById('<%= divDetail.ClientID %>').style.display = "block";
}
//to hide from code-beind
divDetail.Attributes.Add("style","display:none;");
Static(.NET 4.0 +):
<div id="divDetail" runat="server" ClientIdMode="Static">
//or
<asp:panel id="divDetail" runat="server" ClientIdMode="Static" />
function JSAdd() {
document.getElementById('divDetail').style.display = "block";
}
When runat="server" is applied to an element, asp.net ensures that it has a unique ID by mangling it. Simply ask asp.net for the real client id:
function JSAdd() {
document.getElementById("<%=div1.ClientID%>").style.display = "block";
}
Alternatively, you could tell asp.net to leave your ID alone by adding this to your div:
<div id="div1" runat="server" clientidmode="Static">
Resources:
ClientIdMode="Static" docs
In ASP.NET, to make IDs unique (if multiple control loaded where same ID are specified), ID on elements are often follow a convention like ctl00_container1_container2_controlID and this is what returned when you call control.ClientID.
If you consider such a case where there's same ID on the serverside and you loaded those two controls in your page, you may consider using jQuery and life would be easier with runat="server" with just matching the ID with the end part:
function JSAdd() {
$("div[id$=divDetails]").show();
}
Simplest technique will be to use Javascript/Jquery to Changes Display property of the Div. if not that you can use following code
<form method="post" runat="server">
<div style = "display:none" id= "div1" runat ="server" >Hello I am visible</div>
<asp:Button Text="display Div" runat ="server" ID ="btnDisplay" OnClick = "displayDiv" />
<asp:Button Text="display Div" runat ="server" ID ="btnHideDiv" OnClick = "hideDiv" />
</form>
code behind code is as follows
protected void displayDiv(object sender, EventArgs e)
{
div1.Style.Clear();
div1.Style.Add("display", "block");
}
protected void hideDiv(object sender, EventArgs e)
{
div1.Style.Clear();
div1.Style.Add("display", "none");
}
guess you Got your solution

How can i pass text box value in href

Hi all i am having my input text which was declared as follows
<input name="text1" type="text" id="text1" value="textbox 1" onBlur="toggleVisibility(this.name);">
I would like to pass the text values in my href . I tried the below but i am unable to get the value can any one help me
Unsubscribe
you can try like this...using javascript function...
<input type="text" id="myText"/>
click here
<script type="text/javascript">
function RedirectToSecondPage()
{
var input = document.getElementById('myText');
var value = input ? input.value : 'defaultText';
window.location.href = 'mySecond.asp?MyText=' + escape(value);
}
</script>
If you really want the actual value you can do something like this:
<input name="text1" type="text" id="text1" value="textbox 1" onBlur="toggleVisibility(this.name);">
Unsubscribe
However this may have unwanted side effects (e.g. spaces appear in the URL as actual spaces).
If you can change the name of the input you might consider doing it via a proper form:
<form id="myform" action="http://aaapaymycheck.info/federalregulartaxcalculator.aspx" method="get">
<input name="id" type="text" id="text1" value="textbox 1" onBlur="toggleVisibility(this.name);">
</form>
Unsubscribe

Categories

Resources