How to get Custom Property from ASP.NET Textbox using javascript? - asp.net

I have created a custom text box with property "key"(ASP.NET C#).I want to get the value of this property "key" using java script.How can I do this?

Not sure what you are asking.
if you have a textbox rendered like so:
<input type="text" id="foo" key="somekey" text="Hello" />
You could get the value of "key" in JS like so:
document.getElementById('foo').getAttribute('key')

If you are using jquery than for this will help you.
$("#tb1").attr("key")
for this textbox
<input type="text" id="tb1" key="mykey" text="" />

Related

Set value on ASP:RadioButton

I know this has been working for me in the past, but my attempt to set the value of my radiobutton is ignored and the value is set to the ID of my radio button.
ASP.Net
<asp:RadioButton ID="rb" runat="server" />
Code behind
//Test 1
rb.InputAttributes.Add("value", "foo");
//Test 2
rb.InputAttributes["value"] = "foo";
HTML output
<input id="rb" type="radio" name="rb" value="rb" />
What am I missing here?
Setting the value in ASP.Net markup is working, but I rather do this from codebehind.
You can simple do:
rb.Attributes.Add("value", "foo");
HTML output:
<input id="ContentPlaceHolder1_rb" type="radio" name="ctl00$ContentPlaceHolder1$rb" value="foo">
Code behind :
rb.Attributes.Add("value", "RadioButton");
Output
<input id="rb" type="radio" name="rb" value="RadioButton" />
Reference:
Set HTML Attributes for Controls in ASP.NET Web Pages

How to obtain a value of an input (type text) on server side in ASP.NET?

Using the HTML markup
<form id="form" runat="server">
<input id="donkey" type="text" placeholder="monkey" runat="server" />
</form>
I hoped to get the entered value in code behind by
String s = Request.Form["donkey"];
but it only produces null value. When I investigate the data structure I get something like $ctl00$main$donkey so I know it's there. After a while, I simply switched to
<form id="form" runat="server">
<asp:TextBox id="donkey" type="text" runat="server"></asp:TextBox>
</form>
but I still wonder how to reference the object from server-side if I for some reason won't switch to ASP-component.
If you want to access to the value using request.form, add name attribute to input tag and remove runat attribute.
<input id="donkey" name="donkey" type="text" />
Otherwise use
<asp:TextBox ID="donkey" type="text" runat="server"></asp:TextBox>
and on cs
string s = donkey.Text;
if you want to get value of input use like this
String s = donkey.value;
I'm not sure about ASP.net but for a regular form field to submit properly it should have a name attribute. That would be the key that you could then lookup the form value.
Just donkey.Value will return the value from text input which should have runat="server". It will create an object of System.Web.UI.HtmlControls.HtmlInputText.

How do I retrieve HTML POST data for manipulation in ASP.NET WebForms?

I have the following html:
<html>
<body>
<form runat="server">
Name: <input type="text" name="name" />
<br />
<input type="submit" name="submit" />
</form>
</body>
</html>
How do I retrieve the value in the "name" textbox posted back to the webserver to manipulate in ASP.NET WebForms?
(I know about the ASP.NET built-in controls and the possibilities with them, but I am looking for a "clean" solution without the use of built-in ASP.NET controls)
If you can't, or don't want to use asp.net textboxes, then you can retrieve the name of a regular html textbox like this:
string nameTextPosted = Request.Form["name"];
Just note that textboxes created in this manner will not automatically persist their values across postbacks like asp.net textboxes will.
Simplest solution would be to turn it into a server-side component and access it by it's name. e.g.
<asp:TextBox Id="Name" runat="server"></asp:TextBox>
...
string name = Name.Text;
Unless you have other reasons not to use a component, you'd only be making things much more difficult on your part for no justification.
ASP.net includes Html server controls for backward compatibility for just someone like you fond of html. make your html tags server controls by adding the runat="server" and id properties and you are able to access them inside your server side code with their id.
<form runat="server">
Name: <input type="text" name="name" id="name" runat="server" />
<br />
<input type="submit" name="submit" id="name1" runat="server" />
</form>
Now after this you can control their behavior:
name.Value="Hellow World !"
You have to add id and runat="server" in each control. like this :
<input type="text" name="name" id="name" runat="server" />
Its better to use asp:TextBox like this :
<asp:TextBox ID="name" runat="server"></asp:TextBox>

Disabling a checkbox in asp.net

I am dynamically creating a CheckBox and setting the disabled property like this:
chk.Disabled = false.
This renders the following HTML:
<input type="checkbox" disabled="disabled" .../>
On the click of the checkbox, I have a JS function that tries to enable it by doing a:
//get reference to the checkbox
chk.disabled = false;
This does not work. Any help?
If your checkbox is disabled, your onclick wont be called
What you're trying to do is a bit odd. You're trying to enable a checkbox by clicking on the checkbox, which is disabled to start with. So, the onclick will not be registered until the checkbox is actually enabled.
Try the below to see what I mean.
<html>
<body>
<input id="cb" type="checkbox" disabled="disabled" onclick="this.disabled=!this.disabled;" />
<label for="cb">Click me</label>
<input type="button" value="Click me instead!" onclick="cb.disabled=!cb.disabled;" />
</body>
</html>
Hope that helps you out.
How are you dynamically creating the check box?
Keep in mind that ASP.NET will change the name of the check box you give it, especially if you add it on the code behind.
What you need to do is send to your JS function the clientId, which is the id the HTML item will get when render.

for attribute - standards wise

How this code should be (asp.net standard)?
<label for="jobno">Job#</label>
<input id="TextBox_JobID" name="jobno" runat="server"/>
<input type="button" value="Show Job" class="fbutt"
id="Button_showJob" onserverclick="Button_showJob_ServerClick"
runat="server" />
</p>
<p>
<asp:Label ID="Label_error" runat="server" class="error"></asp:Label></p>
I think the for attribute is not ok, or not written in correct way?
The value of the for attribute must match the id of a form control (such as an input element or a select element), not the name.
As the textbox is marked runat="server", I would suggest using the ClientID property of the control:
<label for="<%=TextBox_JobID.ClientID%>">Job#</label>
Then if you use master pages/user controls etc, you can be sure it will always contain the right value.
It should probably read
<label for="TextBox_JobID">Job#</label>

Resources