Assigning hidden field to a variable - asp.net

I have an aspx control as follows
<input type=""hidden"" value='EmployeeId' id=""employeeID""/>
I need some help in making the value of the above hidden field dynamically assigned to a integer variable called EmoployeeId somewhere above this line. Can anyone point out how to do this?
Thanks

Add runat="server" to the markup:
<input runat="server" type="hidden" value="" id="employeeID"/>
And on code behind, do this:
employeeID.Value=valueFromVariable.ToString();

If you've ASP.NET then use server control,
<asp:HiddenField id="empId" runat="server" value="10"/>
and in code behind,
int no=int.Parse(empId.Value);
//Or - use int.TryParse

You can assign a server-side variable to a client-side Javascript variable as so (Classic ASP):
<script type='test/javascript'>
var myVar = '<% =myServerVar %>' <--- a string
var myNum = <% =myServerVarNUm %> <--- a number
</script>

Related

how to copy text from an asp:textbox to another asp:textbox in asp.net webfroms aspx

I've been trying to find a solution for hours now.
asp:textbox 1:
<asp:TextBox runat="server" ID="code" placeholder="<%$ Resources:ErrorMessages, EnterCode %>" CssClass="enterCode"></asp:TextBox><br />
asp:textbox 2:
<asp:TextBox runat="server" ID="code2" placeholder="<%$ Resources:ErrorMessages, EnterCode %>" CssClass="enterCode"></asp:TextBox><br />
button:
<asp:Button CssClass="btn btn-primary" runat="server" ID="buttonEnable2" OnClick="buttonEnable_Click" Text="<%$ Resources:UserMessages, Enable %>" />
I'm trying to copy text from textbox 2 into textbox 1.
textbox 2 is embedded in a jquery accordion in a bootstrap popover.
I've tried:
the simple one within aspx.cs:
code.text = code2.text;
some jquery within a javascript file linked to the aspx file:
$("#buttonEnable2").click(function () {
$('#code').val($('#code2').val());
alert('test');
});
the code above doesn't seem to work at all, it doesn't even give me an alert.
The problem is that you're using an accordion inside of a popover which clones everything inside the popover, coping the ID of everything. So when you type a value into the textbox it attaches it to the clone of the textbox, not the actual textbox.
here's the solution:
function clickEnable2() {
var txtBox1 = this.parentElement.getElementsByClassName("enterCode")[0].value;
$("#MainContent_code").val(txtBox1);
$("#MainContent_buttonEnable").click();
}
$("#MainContent_buttonEnable2").click(clickEnable2);
This Code is in javascript
<script>
function Copy() {
var n1 = document.getElementById('code');
var n2 = document.getElementById('code2');
n2.value = n1.value;
}
</script>
and your button should be like
<asp:Button CssClass="btn btn-primary" runat="server" ID="buttonEnable2" OnClientClick="Copy()" OnClick="buttonEnable_Click" Text="<%$ Resources:UserMessages, Enable %>" />
Hope it will help.
Your textboxes are of asp.net which changes the ID of textboxes with respect to its ContentPlaceHolder. So jquery is unable to find the textboxes.
You can use ClientIDMode="Static" so that ID of textboxes will not change. Put this property for the other textbox as well.
<asp:TextBox runat="server" ID="code" ClientIDMode="Static" placeholder="<%$ Resources:ErrorMessages, EnterCode %>" CssClass="enterCode"></asp:TextBox>
Then use jquery to copy text from one textbox to other.
$('#code').val($('#code2').val());
If you are using master pages, update panels etc in asp.net, the ID of the text box will be automatically replaced with a dynamic ID, hence you may need to do the following, this will help if you do not want to change the ClientIDMode to Static
First declare a javascript variable and assign your textbox's dynamic id's to it, for which the JQuery will be
$(document).ready(function(){
var txtBox1 = <%=code.ClientId%>;
var txtBox2 = <%=code2.ClientId%>;
});
Now you can use these two variables to copy your text into one another
$("#buttonEnable2").click(function () {
$('#' + txtBox1).val($('#' + txtBox2).val());
});
You can also use the document.getElementById in the following way.
var n1 = document.getElementById(<%=code.ClientId%>);
var n2 = document.getElementById(<%=code2.ClientId%>);
I hope this will help you.

ClientIDMode in CheckBox

What is use of ClientIDMode attribute in asp:CheckBox control? I found the values AutoID, Inherit, Predictable, and Static for that.
Mainly I am looking for how it is rendered in HTML? Can anyone explain about the attribute and the values?
It is how MS calculates ID of a .net control name itself. MSDN
Since ASP.net pages are put together on a server and sent to the client MS Will name the ids, based off of a variable amount of conditions, are they in a master page , are they in a user controller, a repeater, and so on.
Since you cannot have duplicate ids what do you do with a .net control that is put inside of a repeater? You need to have a naming algorithm for it. Some people foolishly use the rendered ID (ct100_*) instead of using getElementID to manipulate the element through javascript. I dont know why they exposed these algorithms instead of just saying everything is going to be 1 algorithm. Maybe someone has the answer to why they exposed the different algorithms.
Example of id generation:
Web form:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
lstMain.DataSource = new string[] { "a", "b" };
lstMain.DataBind();
}
</script>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ListView ID="lstMain" runat="server">
<ItemTemplate>
<asp:CheckBox ID="chkFour" runat="server" ClientIDMode="AutoID" />
<asp:CheckBox ID="chkFive" runat="server" ClientIDMode="Predictable" />
<asp:CheckBox ID="chkSix" runat="server" ClientIDMode="Static" />
</ItemTemplate>
</asp:ListView>
</asp:Content>
Result:
<input id="ctl00_ContentPlaceHolder1_lstMain_ctrl0_chkFour" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkFour" />
<input id="ctl00_ContentPlaceHolder1_lstMain_ctrl0_chkFive_0" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkFive" />
<input id="chkSix" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkSix" />
<input id="ctl00_ContentPlaceHolder1_lstMain_ctrl1_chkFour" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkFour" />
<input id="ctl00_ContentPlaceHolder1_lstMain_ctrl1_chkFive_1" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkFive" />
<input id="chkSix" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkSix" />
Pass id to JS:
<script type="text/javascript">
var autoId = '<%=someControl.ClientID%>';
var predictableId = '<%=someControl.ClientID%>';
var staticId = 'someControl';
</script>

find control and html tags

i have this code in my default aspx file :
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="holder1" runat="server">
<asp:Label ID="label1" runat="server" Text="Label">
</asp:Label>
<input type="text" ID="txt" runat="server"/>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:Button Text="Ok" ID="btnOk" runat="server" onclick="btnOk_Click" />
</asp:PlaceHolder>
</div>
</form>
</body>
and my code behind is :
TextBox tb1 = holder1.FindControl("txt") as TextBox;
Response.Write(tb1.Text);
TextBox tb2 = holder1.FindControl("txt2") as TextBox;
Response.Write(tb2.Text);
my problem is here that findcontrol ("txt") return null value!!! because i used <input> ,how can i handle this control ?
Firstly, you don't need the holder1.FindControl as you can access the controls directly.
To get the input control, use the code;
HtmlInputText tb1 = this.txt;
Response.Write(tb1.Value);
You might need to import System.Web.UI.HtmlControls.
Using System.Web.UI.HtmlControls;
Edit
To find controls which have been dynamically added via Javascript, you will need to use the Request object.
string theValue = Request.Form["txt"].ToString();
An input type="text" is not a TextBox. So you either should cast it to HtmlInputText or use a TextBox instead.
var txt = (HtmlInputText)row.FindControl("txt");
Note that you need to add using System.Web.UI.HtmlControls,
MSDN :
Control.FindControl : Method Searches the current naming container for
the specified server control.
as is not server control it is not pissible to find it ! any Other Way to handle controls that are not server side ?
what happen if any one want to get text of text box that is not run at server .

how to set Name attribute for HiddenField control server-side?

I want to set the "name" attribute for HiddenField control of ASP.NET from code behind, but I cannot find the "Attributes" property. Is it not there for a purpose? How do I add the attribute?
thanks
The name attribute is automatically computed from the ID properties of the hidden field and its ancestors in the naming container chain. You don't get to set it yourself. You can only access it through the UniqueID of the control.
A possible solution, without knowing a bit more about your code, is to use a server side Html control rather than an ASP.NET web control by adding the runat="server" attribute to the Html markup:
<input type="hidden" id="myHiddenField" runat="server" />
You can then specify the id dynamically in the code behind at runtime from which the name attribute is inferred from:
myHiddenField.ID = "CodebehindName";
myHiddenField.Value = "myValue";
This will result in the following output:
<input name="CodebehindName" type="hidden" id="CodebehindName" value="myValue" />
Another unorthodox method to deal with it is to set the name attribute client side. This is useful if you are posting to a third party such as PayPal.
jQuery EG:
<script type="text/javascript">
$(function () {
$('#BusinessHid').prop('name', 'business')
$('#CurrencyHid').prop('name', 'currency_code')
$('#InvoiceHid').prop('name', 'invoice')
$('#AmountHid').prop('name', 'amount')
})
</script>
<asp:HiddenField ID="BusinessHid" runat="server" ClientIDMode="Static" />
<asp:HiddenField ID="CurrencyHid" runat="server" ClientIDMode="Static" />
<asp:HiddenField ID="InvoiceHid" runat="server" ClientIDMode="Static" />
<asp:HiddenField ID="AmountHid" runat="server" ClientIDMode="Static" />
Forget about the HiddenField control and use a Label instead, give it a name (an id), make it invisible, and store your text into it:
label = new System.Web.UI.WebControls.Label() {
Text = "Here my hidden text",
};
label.Attributes.Add("id", "MyHiddenFieldID");
label.Attributes.Add("style", "display:none;");
myParentControl.Controls.Add(label);
Get your hidden field in your javascript with:
var myHiddenField = document.getElementById("MyHiddenFieldID");
The way I ended up doing this was to set ClientIDMode="Static" on the HiddenField and then set the ID to what I want my name to be.
I ended up with ugly IDs but this was a small price to pay to get this to work.

ASP.NET : Reading form variable values in the action page of search form

I have a website where i want to implement search functionality.So i added the below code to have a search box in my html page
<form id="search" method="post" action="Results.aspx">
<input id="txtSearchKey" type="text" name="txtSearchKey" />
<input id="Submit1" type="submit" value="submit" /><br />
<br />
</form>
In Results.aspx, I want to read the value user has entered in the txtSearchKey text box. What is the ideal way to do this ? I used
string strKey = Request.Form["txtSearchKey"].ToString();
But it throw a null reference exception.
I don't want to have all pages in ASP.NET.I want to have only the result page as ASP.NET
Could be because you do not have a NAME attribute on the textbox field. That's the value that is used as the key in the Request.Form collection. An input field without a name attribute will not be submitted, I think.
e.g.:
<input id="txtSearchKey" type="text" name="txtSearchKey" />
You can get your txtSearchKey field by this :
string strKey = PreviousPage.Request.Form["txtSearchKey"].ToString();
But, instead of using form action to forward your search to another page, you can use a button with PostBackUrl property like that :
<asp:Button runat="server" ID="btnSearch" PostBackUrl="Search.aspx" />
Because in ASP.NET, to have more then one form is not allowed.
Is there any reason you don't use
form runat="server"
and then drag a TextField and a Button in this form. Then doubleclick the button and write code you want.
If you want to do it your way you need to give your s a name="txtMySearchKey" for it to work
The way you are going about things is not really the way you work in ASP.NET web forms. The preferred way is to use asp.net server controls and events to abstract the process you are trying to achieve. For instance, your form should really be something like this (note the runat="server" attribute that enables you to reference the controls programmatically):
<form id="form1" runat="server">
<div>
<asp:Panel ID="PanelSearch" runat="server" DefaultButton="ButtonSubmit">
<asp:TextBox ID="TxtSearchKey" runat="server" /><br />
<asp:Button ID="ButtonSubmit" Text="Submit" runat="server"
onclick="ButtonSubmit_Click" /><br />
</asp:Panel>
</div>
</form>
Then, in your code behind you would handle the ButtonSubmit_Click event like this to enable you to get the value from the TxtSearchKey textbox:
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
string strKey = TxtSearchKey.Text;
}
See the Quickstart example for the TextBox control for more info.
Just do not use .toString() after the Request.form... it will not give a null reference after that.

Resources