I have a user control named lets say ucName that has a property named InitialValue. Everything's fine , Im rendering the control and setting the initial value. In my aspx page, Im doing like this:
<td>
<tagprefixName:ucName ID="Editor" runat="server" InitialValue="bla bla"/>
</td>
I have a button that will post back the page, but before post backing, im doing:
<asp:Button ID="SaveButton" runat="server" Text="Save" OnClick="SaveRes" OnClientClick="SaveValue()" />.
In javascript function I want to get the value of the initial value of my user control: I tried many solutions, but they are either returning null or undefined.
function SaveValue() {
var v= $("[id$=Editor]").attr("InitialValue");
var v = $("#Editor").attr("InitialValue");
var v= $("input[id$=Editor]").attr("InitialValue");
var v= $("span[id$=Editor]").attr("InitialValue");//since the uc is rendered as span
}
How can I get the InitalValue of my uc ?? Thanks alot
EDIT This is my html:
<span id="ctl00_PlaceHolderMain_Editor"><input name="ctl00$PlaceHol开发者_运维问答derMain$2f1445d6-b13d-4f8f-9497-9c14e5d2b076" type="text" value="fgerf" id="ctl00_PlaceHolderMain_2f1445d6-b13d-4f8f-9497-9c14e5d2b076" /></span>
NB: I tried using .val() .value() but still getting an empty
I'm presuming that the markup gets parsed on the server, and the resulting HTML code does not have this InitialValue property. Try viewing source to see what the actual code is and then paste it into the question.
If all the control does is set the contents of a span, then the following should work:
$("span#Editor").text();
you could retrieve it in the aspx markup page using an expression like this
var initialValue = <%= Editor.InitialValue %>;
The uc is rendered as an input inside a span has the uc's id, so the javascript code will be:
$("span[id$=Editor] input[type=text]").val();
Edit: updated to use object names now that you've included the actual HTML in your page. The only thing I'm still unsure of is whether ctl00_PlaceHolderMain_Editor
is the same every time or not. If it's not the same every time, then you need to find a way (server-side) to assign an id that is constant so you have a reliable way to reference the object in the web page.
Adding this code will "save" the initial value to an attribute of the editor object when the page is first loaded.
$(document).ready(function(){
var $editor = $("ctl00_PlaceHolderMain_Editor input");
$editor.attr("data-initialValue", $editor.value());
});
And, then you can read back the initial value anytime later with this code:
$("ctl00_PlaceHolderMain_Editor input").attr("data-initialValue");
If, for any reason this code isn't working, then (in the ready handler), you need to find out whether it's not working because $("ctl00_PlaceHolderMain_Editor input")
isn't being found or whether it's because it isn't able to save the data value on the object.
精彩评论