I have a requirement where I have to validate for the empty text of a multiline textbox in aspx. I am using jquery for this purpose.
My aspx page would look like this:
<asp:TextBox runat="server" ID="txtClarification" ClientIDMode="Static" TextMode="MultiLine" Rows="8" Style="width: 780px;"></asp:TextBox>
and in my Jquery function:
var textbox = ('#txtClarification').val();
if (textbox.length == 0) {
//do something
}
But the statement which I retrieve the textbox value throws error:
Microsoft JScript runtime error: Object doesn't support property or method 'val'
Is there any difference in retrieving the value from a Multiline textbox?
Don't forget the $:
var textbox = $('#txtClarification').val();
//------------^
if (textbox.length == 0) {
//do something
}
Also I'm not an ASP.NET expert, but you may need to specify ClientID for your selector to work.
Related
I have a webform with a checkbox in it. I need to do two things differently based on an environment setting.
Add a class
Add the Text attribute so a label gets created
<% if setting == true) { %>
<asp:CheckBox ID="optionCheckbox" class="option-checkbox radio-checkbox" runat="server" Text="Label Text"/>
<% } else { %>
<asp:CheckBox ID="optionCheckbox" class="option-checkbox" runat="server"/>
<% } %>
The problem with this is the page won't render because the ids are the same, even though only one could ever get rendered. There is a lot of other processing with javascript and such so I don't want different ids for each scenario.
I was able to fix this by keeping only the "base" checkbox code below and then overriding the PreRender event to add the class and set the Text attribute there.
<asp:CheckBox ID="optionCheckbox" class="option-checkbox" runat="server"/>
Code Behind:
CheckBox optionCheckbox = this.optionCheckbox as CheckBox;
if (optionCheckbox != null)
{
optionCheckbox.Text = "Label Text";
optionCheckbox.Attributes.Add("class", "option-checkbox radio-checkbox");
}
I'd still like to know if there is a way to do this in the markup file though.
Sorry about this syntax , I don't use ASPNet webPage.
You can implement it logically..
Firstly , define class and text variable and set value for business..
Finaly set checkbox class and Text value by variable
var class = "option-checkbox";
var text = "";
if(setting == true)
{
class = "option-checkbox radio-checkbox";
text = "Label Text";
}
<asp:CheckBox ID="optionCheckbox" class="setClassProperty" runat="server" Text="SetTextProperty"/>
I have defined a variable in C# as the item selected in a drop down.
string parametername = ddlCarrier.SelectedItem.Text;
I now want to pass this variable in my URL to the next page. How do I do this in the href tag?
<asp:LinkButton href="Table.aspx?parameter=<%parametername%>" ID="btnSubmit" runat="server">Click Here</asp:LinkButton>
Purely Server-Side Approach
Instead of a LinkButton, you might want to consider using a HyperLink or <a> tag as you aren't going to be doing anything with your code-behind:
<asp:HyperLink ID="btnSubmit" runat="server" NavigateUrl="Table.aspx" Text="Navigate"></asp:HyperLink>
Then you can use the NavigateUrl property, which you might want to consider setting within your code-behind :
// This will set up your Navigation URL as expected
btnSubmit.NavigateUrl = String.Format("Table.aspx?parameter={0}",ddlCarrier.SelectedItem.Text);
If you use this approach, you may want to explicitly set that a PostBack occurs when your DropDownList changes so that this value will consistently be correct :
<asp:DropDownList ID="dllCarrier" runat="server" AutoPostBack="True" ...>
Client-Side Approach
However, if you are expecting to be able to change this to reflect the current value of your Carrier DropDownList without a PostBack, then you'll likely need to resort to Javascript to populate the value prior to actually navigating :
<!-- Set your base URL within the method and append the selected value when clicked -->
<asp:Button ID="Example" runat="server" OnClientClick="ClientSideNavigate('Table.aspx'); return false;" Text="Navigate"></asp:Button>
<script>
function ClientSideNavigate(url) {
// Get the selected element
var e = document.getElementById('<%= ddlCarrier.ClientID %>');
// Navigate
window.location.href = url + '?parameter=' + e.options[e.selectedIndex].value;
}
</script>
Or you could just avoid ASP.NET Controls altogether and just use an <button> tag :
<button onclick="ClientSideNavigate('Table.aspx'); return false;">Navigate</button>
<script>
function ClientSideNavigate(url) {
// Get the selected element
var e = document.getElementById('<%= ddlCarrier.ClientID %>');
// Navigate
window.location.href = url + '?parameter=' + e.options[e.selectedIndex].value;
}
</script>
You need to handle TextChanged or SelectedIndexChanged event for ddlCarrier and properly set href property of btnSubmit to include ddlCarrier.Text.
I have a hidden field like this:
<asp:HiddenField ID="showHideFlag" runat="server" />
I am assigning some value to this hidden field in java script as follows:
function controlSearchBar() {
if ($("#MainContent_ProjectListControl_searchBar").is(":hidden")) {
$("#MainContent_ProjectListControl_showHideFlag")[0].value = "showing";
} else {
$("#MainContent_ProjectListControl_showHideFlag")[0].value = "hiding";
}
}
I am trying to read this hidden field in ascx.cs page as follows:
string hdnValue = this.showHideFlag.Value;
But this hdnValue is not getting the value of that hidden field.
Can someone help on this?
Hidden as type="hidden"
$("#MainContent_ProjectListControl_searchBar").attr('type') == 'hidden'
Hidden as display: none
$("#MainContent_ProjectListControl_searchBar").is(":hidden")
Gets the control ID for HTML markup that is generated by ASP.NET.
<asp:Label ID="SelectedSport" runat="server" ClientIDMode="Static" ClientID="showHideFlag">
javascript
$("#showHideFlag").text("found");
You are saying that you can get the value in javascript So I think the the problem is with the hidden field. try to set value by Client id as follows-
var hd = document.getElementById('<%= showHideFlag.ClientID%>');
hd.value = "hi";
And my another question is in which event you are accessing value? because If you are setting value in javascript and accessing in Page Load event then it will not work because first of all Page load event gets fired and then Javascript function executes.
I know this question is asked before for assigning text to textbox, but they didn't have answers or the given answers didn't work for me. I have a static function for translations and I'm trying to use that to assign a placeholder to a Textbox. How can I do it in aspx page?
My code is:
<asp:TextBox ID="search" runat="server"
placeholder='<%# islem.DilGetir(7) %>'>
</asp:TextBox>
This one returns this sourcecode:
<input name="ctl00$search" type="text" id="ctl00_search">
You should set this attribute from the page code behind:
search.Attributes["placeholder"] = islem.DilGetir(7)
you can use ajax controll toolkit text box watermark I find it to be mnost usefull in aspx apps
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/TextBoxWatermark/TextBoxWatermark.aspx
<asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID ="search" WatermarkText="textthe my name" WatermarkCssClass="watermarked">
</asp:TextBoxWatermarkExtender>
on the back end
TextBoxWatermarkExtender1.WatermarkTex=islem.DilGetir(7);
I experienced the same problem. The exact solution: Take all placeholder's text to hiddenfield component(separated with , ) and with JS, take the inside array hiddenfield text and assign to input text's placeholder attr with jQuery or JS.
this code for multiselectbox choosen js and normal selectbox.
public void SelectFill(DataTable dtResult, HtmlSelect htmlSelect, string placeHolder, string textColumn, string valueColumn, string value)
{
htmlSelect.DataSource = dtResult;
htmlSelect.DataTextField = textColumn;
htmlSelect.DataValueField = valueColumn;
htmlSelect.DataBind();
bool isMultiple = false;
foreach (var item in htmlSelect.Attributes.Keys)
{
if (item.ToString() == "multiple")
{
isMultiple = true;
}
}
if (isMultiple)
{
htmlSelect.Attributes["data-placeholder"] = placeHolder;
}
else
{
ListItem placeHolderItem = new ListItem(placeHolder, "-1");//create placeholder option for non multible selectbox
placeHolderItem.Attributes.Add("disabled", "disabled");
htmlSelect.Items.Insert(0, placeHolderItem);
htmlSelect.Items[0].Selected = true;
}
}
I'd like to be able to get a ASP.NET control's ClientID inside of a JavaScript function. I also would like to be able to pass in a var containing a control name so that different controls can use the same function.
For example, if I wanted to use a control named Button1, this works fine:
function doStuffToButton1Control()
{
var buttonControl = document.getElementById('<%= Button1.ClientID %>');
//do stuff with buttonControl
}
But I get an error when I try to make the control name dynamic. I'm trying to string together the ClientID call, like so:
function doStuffToGenericControl(controlName)
{
var dynamicControl = document.getElementById('<%= ' + controlName + '.ClientID %>');
//do stuff with dynamicContorl
}
I've tried using double quotes or single quotes when creating the string, but the error is always "Too many characters in string literal" or "Too many characters in character literal". The control exists and the name passes correctly (as tested by an alert(name)).
Is there a better way to go about this, or am I missing something obvious?
Thanks!
Update: This function will be on a master page. Any number of child pages will be calling it. For example, one child page might have an ASP control that calls it like so:
<asp:LinkButton ID="Button2" runat="server" OnClientClick="doStuffToGenericControl('Button2')">...</asp:LinkButton>
But another page might have this ASP control that also calls it:
<asp:LinkButton ID="Button4" runat="server" OnClientClick="doStuffToGenericControl('Button4')">...</asp:LinkButton>
However, these controls' IDs often change by ASP.NET on render, so they end up looking, for example, like MainContent_Button2 instead of just Button2. To get around this, I traditionally just used <%= Button1.ClientID %>, but since I want any number of controls to be able to use this one function, I'm having a bit of trouble.
Update 2: I was able to use this along with a getAttribute call to get what I needed. (Obviously this will only work when the control you are referring to is the one you are clicking, but that is what I needed) Like so:
function doStuffToGenericControl(controlName)
{
var controlID = controlName.getAttribute('id');
var control = document.getElementById(controlID);
//do stuff with control
}
'<%= Button1.ClientID %>' is something that replaced in with control id on asp.net page render,
but when you pass variable in client side, you should pass id and use it without any % scops
so try this
function getGenericControl(control)
{
var dynamicControl = control;
alert(dynamicControl);
//do stuff with dynamicContorl
}
UPDATE
You can use <%= ClientID %> to generate parameters for the method above, so just change OnClientClicks like this and use method above
<asp:LinkButton ID="Button2" runat="server" OnClientClick="doStuffToGenericControl(this)">...</asp:LinkButton>
Rather than passing control name why don't you pass the control itself by passing this in the function like this:
<asp:LinkButton ID="Button2" runat="server" OnClientClick="doStuffToGenericControl(this)">...</asp:LinkButton>
and your function will be :
function getGenericControl(control)
{
var dynamicControl = control;
alert(dynamicControl.id);
//do stuff whatever you want with dynamicContorl
}
I would strongly suggest using a class if that is possible,
var elems = document.getElementsByTagName('*'), i;
for (i in elems) {
if((' ' + elems[i].className + ' ').indexOf(' ' + matchClass + ' ')
> -1) {
elems[i].innerHTML = content;
}
}