Quick and silly question perhaps, but how do I pass a label's text into a javascript confirm or alert function?
Here is what I've done but it's not good:
<asp:Label ID="lblGVDeleteMessage" runat="server" Text="Show me!" style="display:none;" ></asp:Label>
<asp:ImageButton ID="btnDelete" runat="server" AlternateText="Delete"
CommandName="Delete" Height="15px" ImageUrl="~/Images/document_delete.png"
OnClientClick="return confirm('#<%=lblGVDeleteMessage.ClientID%>')"
Width="15px" />
Thanks!
You need to grab the element by it's ID, then read it's innerHTML property:
OnClientClick="return confirm(document.getElementById('<%=lblGVDeleteMessage.ClientID%>').innerHTML)"
But you might want to consider moving the logic out into another function:
Script:
function showConfirm(id) {
var elem = document.getElementById(id);
return confirm(elem.innerHTML);
}
ASPX:
OnClientClick="return showConfirm('<%=lblGVDeleteMessage.ClientID%>')"
You probably want this to be:
"return confirm(document.getElementById('#<%=lblGVDeleteMessage.ClientID%>').innerHTML)"
Based off the comment below and the fact that you must be dynamically setting the text to the label in the first place, can you not just set the text dynamically into the call instead? Bit neater than outputting an extra dom element that will always be hidden, e.g.:
"return confirm('<%# SomeFunctionTosetTheText() %>')"
The only way I made it work was to set ClientIDMode="Static" on the label and on the OnClientClick event do this: "return showConfirm('lblGVDeleteMessage');"
Related
ASP.NET coding in VB.NET -- web application.
I am asking a few questions about the "proper way" to set the OnClientClick phrase in the aspx button declaration.
Option 1: leading with 'javascript:' and surrounding the js in {...}
<asp:Button ID="btnDelete" runat="server" CausesValidation="False" SkinID="cmdButton" style="margin-left: 2em;"
CommandName="Delete" Text="Delete"
OnClientClick = "javascript:{(!confirm('Do you want to delete this record?'); return false;}"
Option 2: Not leading with 'javascript:' and NOT surrounding the js in {...}
<asp:Button ID="btnDelete" runat="server" CausesValidation="False" SkinID="cmdButton" style="margin-left: 2em;"
CommandName="Delete" Text="Delete"
OnClientClick="if (!confirm('Do you want to delete this record?')) return false;"
Both options appear to work properly in IE-10, but have not tried other browsers. So I am curious about the different phrases.
What about the leading of 'javascript:'? -- what affect does this have?
What about the surrounding of the js with {...} -- what affect does this have?
Your comments are welcome -- what is the best way to construct the phrase.
Any other syntax (or style) or suggestions will be welcome.
Thanks, in advance...John
OnClientClick renders into the standard HTML onclick attribute, so put whatever works with it.
I.e. - no javascript prefix required, no brackets required as well.
I want to pass my dynamic value to a javascript function from an imagebutton onclientclick event.
It works fine if I pass the static value like this
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('1')" />
However it doesn't work if I try to pass dynamic values as below.
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent" BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('"+<%#Eval("PujaType") %>+"')" />
I tried all possible combinations however it says tag is not well formatted.
Can anyone help on this?
Don't know ASP, but in JSP the expression has to be the entire value. Try:
OnClientClick="<%# "javascript:return openWindow('" + Eval("PujaType") + "')" %>" />
You don't need string concatenation around your Eval statement. Doing so is creating invalid markup.
Change
"javascript:return openWindow('"+<%#Eval("PujaType") %>+"')"
to
"javascript:return openWindow('<%#Eval("PujaType") %>')"
<asp:ImageButton
ImageUrl="~/Images/control_play_blue.png"ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick="javascript:return openWindow('<%#Eval("PujaType") %>')" />
Its been awhile since I have worked with ASP.NET and currently cannot test this, but I seem to recall it requiring single quotes when wrapping the <%# %>.
Something like this:
<asp:ImageButton ImageUrl="~/Images/control_play_blue.png" ForeColor="Transparent"
BorderStyle="None" ToolTip="Chat" runat="server" ID="btnChat"
OnClientClick='<%# "javascript:return openWindow('" + Eval("PujaType") + "')" %>' />
Some of the answers on this question suggest the same.
An alternative would be setting the value in the code behind. Assuming you are using this control inside a repeator or something similar, you should be able to set the value during data binding like this:
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
String pujaType = ((YourType)e.Item.DataItem).PujaType;
((ImageButton)e.Item.FindControl("btnChat")).OnClientClick = "javascript:return openWindow('" + pujaType + "')";
}
}
Repeater.ItemDataBound Event
Another alternative, since you are not making use of any serverside events, would be to use regular html instead of an ASP.NET control. This could be done with an anchor and img tag like so:
<a href="#" onclick="openWindow('<%# Eval("pujaType") %>'); return false;">
<img src='<%= ResolveUrl("~/Images/control_play_blue.png") %>' />
</a>
I want to set the value of a asp.net textbox using javascript
My JS Code is:
document.getElementById('<%=txtFlag.ClientID %>').value = "Track";
My textbox is:
<asp:TextBox ID="txtFlag" runat="server" Visible="False"></asp:TextBox>
But it gives me an error document.getElementById(...)' is null or not an object
I dont understand what is wrong.
Please help.
<asp:TextBox ID="txtFlag" runat="server" Visible="False"></asp:TextBox>
Setting visible=false will cause this textbox to not appear in the rendered page. Remove this, and add display:none;
<asp:TextBox ID="txtFlag" runat="server" style="display:none;"></asp:TextBox>
Try including the ClientIDMode property in your textbox
<asp:TextBox ID="txtFlag" runat="server" Visible="False"
ClientIDMode="Static"></asp:TextBox>
You are calling javascript before complete document load. Please write your javascript code on document.ready function like this
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
document.getElementById('<%=txtFlag.ClientID %>').value = "Track";
});
</script>
And second thing is that use display none in place of visible false or use hidden field control
<asp:TextBox ID="txtFlag" runat="server" style="display:none;"></asp:TextBox>
Solution 1:
Make that textbox as visible=true and try,
When you make a control as visible false, that control will not be loaded in client side and as you knew javascript will be executed on client side itself.
Solution 2:
Add this javascript at the end of the page.
document.getElementById('txtFlag').value='Track'
try this
im stuck with Javascript in Asp.net...
I made a TextBox named tbValidFrom and another named tbValidTo.
I also made two ModalPopups.
Then i try to open the ModalPopupExtenders when the TextBoxes get focus:
<script type="text/javascript">
$('#tbValidTo').focus(function () {
$find('ModalPopupExtenderNV1').show();
})
$('#tbValidFrom').focus(function () {
$find('ModalPopupExtenderNV2').show();
})
</script>
but it doesnt finds tbValidTo or ModalPopUpExtender ?
Runtime-Error in Microsoft JScript: Object expected
Here is one of the two ModalPopupExtenders and TextBoxes:
<asp:TextBox ID="tbValidFrom" runat="server"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanelNV2" runat="server" RenderMode="Inline" UpdateMode="Conditional">
<ContentTemplate>
<cc1:ModalPopupExtender ID="ModalPopupExtenderNV2" runat="server" TargetControlID="HiddenField6"
PopupControlID="PanelNewVersion" BackgroundCssClass="modalBackground" BehaviorID="ModalPopupExtenderNV2"
Enabled="True" />
<asp:HiddenField ID="HiddenField6" runat="server" />
</ContentTemplate
</asp:UpdatePanel>
The same as above is with the other ModalPopupExtender and TextBox...
Help would be really nice.
Thanks
Edit: Yes i use a masterpage!
Fails where it is marked yellow.
your jQuery syntax is wrong.
change your script to:
<script type="text/javascript">
$('#tbValidTo').focus(function () {
$(this).find('#<%=ModalPopupExtenderNV1.ClientID%>').show();
});
$('#tbValidFrom').focus(function () {
$(this).find('#<%=ModalPopupExtenderNV2.ClientID%>').show();
})
</script>
elements that has runat="server" will eventually end with a different ID than you entered in the aspx file.
i used the <%= %> to get the 'final' id.
hope that helps
Seeing that markup there is two chances for the issues.
With the ModalPopupExtender.
Where you have given the panel "PanelNewVersion" . Seems like its missing in the markup.
In the Javascript code. If you want to hide that Popup, give the name of the panel. NOt the name of the ModalPopupextender.
Also you have given an update panel there in page, so you can easily handle the modalpopup in any srever side event of your textbox.
Otherwise, if you want to use the Modal popup ijn client side, use jQuery Modalpopup. That will the better option.
ASP.NET Can modify the ID's of elements, if you use a Masterpage the element id's will be modified to something like the following : ctl001_ContentPlaceHolder1_tbValidFrom
To get around this ASP.NET modification of your elements you can use ASP.NET Inline Expressions to get the rendered ID from the object.
See the answer to the following question for more information on ASP.NET Inline Expressions
You can look at changing the Client ID Mode (The way ASP.NET Modifies IDs when the page is rendered) here :
ASP.NET Client ID Modes
EDIT :
The AjaxControlToolkit's ModalPopupExtender is not rendered on the page as a html element, therefore you cannot find this item using the ClientID of the ModalPopupExtender.
You must use the BehaviourID to call the show function on.
The below code should work correctly :
$('#<%= tbValidTo.ClientID %>').focus(function () {
$find('ModalPopupExtenderNV1').show();
})
$('#<%= tbValidFrom.ClientID %>').focus(function () {
$find('ModalPopupExtenderNV2').show();
});
This will find the textbox object using the ASP.NET Inline expressions to find the client ID and will then find the behaviour ID and execute the Show method.
Example Markup :
<script type="text/javascript">
$(document).ready(function() {
$('#<%= tbValidFrom.ClientID %>').focus(function () {
$find('ModalPopupExtenderNV2').show();
});
});
</script>
<asp:TextBox ID="tbValidFrom" runat="server"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanelNV2" runat="server" RenderMode="Inline" UpdateMode="Conditional">
<ContentTemplate>
<cc1:modalpopupextender id="ModalPopupExtenderNV2" runat="server" targetcontrolid="HiddenField6"
popupcontrolid="PanelNewVersion" backgroundcssclass="modalBackground" behaviorid="ModalPopupExtenderNV2"
enabled="True" />
<asp:HiddenField ID="HiddenField6" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Panel ID="PanelNewVersion" runat="server">
testing panel
</asp:Panel>
I have a dropdownlist defined:
<asp:DropDownList Width="300px" ID="PlaceHoldersDropDownList" runat="server"
AppendDataBoundItems="True" TabIndex="3" onchange="PasteTextInEditor
(this.options[this.selectedIndex].value), '<%=
SubjectTextBox.ClientID %>'" >
So, I pass the selected text and a textbox ID to a javascript function. The text box is defined as:
<asp:TextBox Width="660px" ID="SubjectTextBox" Text='<%# Bind( "Subject") %>'
runat="server" TabIndex="4" MaxLength="100">
</asp:TextBox>
So, in script function, when i put alert like this :
alert(text); //shows selected value
alert(editor); // shows undefined
So editor value is turning up undefined. So, can you please let me know the mistake I've been doing. I would like to save that value selected from dropdown into textbox.Please help me fixing th eproblem. Thank You
It looks like your bracket is in the wrong place in your "onchange": you're only passing one parameter and not the client id.
But regardless, the ClientID is not going to be parsed here. Instead, I suggest you add your onclick in the code behind, either in your Page_Load or Page_PreRender:
PlaceHoldersDropDownList.Attributes.Add("onchange", "PasteTextInEditor(this.options[this.selectedIndex].value, '" + SubjectTextBox.ClientID + "');")