passing clientid to javascript function
Hi,
I have the same problem as described in the link below http://www.velocityreviews.com/forum...-function.html
However the solutions given wont work for me as I am wanting to pass the client ids of various text boxes on a button click. Are there any other solutions?
Re: passing clientid to javascript function
Hello,
Can you give us a clue here?
Can you show some of the code that you are using, both client side and server side, and we can work from there.
Gary
Re: passing clientid to javascript function
do this on form load event in your code behind:
Code:
Const onClientClick As String = "Test('{0}', '{1}'); return false;"
btnSave.OnClientClick = String.Format(onClientClick, txt1.ClientID, txt2.ClientID)
So on the client click event the textbox 1 and textbox 2 clientids get passed to a javascript function called Test.
your js function will look like this:
Code:
function Test(t1, t2)
{
alert(t1);
}
Re: passing clientid to javascript function
Depending on the amount of TextBoxes we are talking about here, this type of approach "might" become a little bit unwieldy. You might want to think about a slightly different approach, for instance, return some JSON to the page with a list of all the TextBoxes Client ID's. As a sample of what you can do, take a look here:
http://encosia.com/using-jquery-to-c...-web-services/
Gary
Re: passing clientid to javascript function
Here is my code I call the same function many times on the button click for each textbox I want to format based on validation.
Code:
function Validate(elementid, validator) { Page_ClientValidate('EditLocationValidation'); if (typeof (Page_Validators) == "undefined") return; document.getElementById(elementid).className = document.getElementById(validator).isvalid ? "RequiredFieldValid" : "RequiredFieldInValid"; }
Code:
<asp:ImageButton ID="imgbtnSave" runat="server" ImageUrl="~/images/buttons/save.png" CausesValidation="True" OnClick="imgbtnSave_Click" TabIndex="12" ValidationGroup="EditLocationValidation" OnClientClick="Validate('maincontent_LocationsView_lblLocationCode', 'maincontent_LocationsView_valLocationCode'); Validate('maincontent_LocationsView_lblLocationName','maincontent_LocationsView_valLocationName'); Validate('maincontent_LocationsView_lblAddress1','maincontent_LocationsView_valAddress1'); Validate('maincontent_LocationsView_lblURL','maincontent_LocationsView_valURL'); Validate('maincontent_LocationsView_lblCity','maincontent_LocationsView_CityRequiredFieldValidator'); Validate('maincontent_LocationsView_lblCounty','maincontent_LocationsView_CountyRequiredFieldValidator'); Validate('maincontent_LocationsView_lblCountry','maincontent_LocationsView_CountryRequiredFieldValidator');" />
So the function being re-used expects a string of the client id to look up, however apart from manual coding I cannot seem to pass it the client id from my HTML /server code
Re: passing clientid to javascript function
Hello,
So, you have two options.
Firstly, you could do something "hacky" like this:
Code:
OnClientClick="Validate('<% YourControl.ClientID %>', 'maincontent_LocationsView_valLocationCode')
Where you are breaking into the server side in your ASPX markup and grabs the ClientID from the server.
Or, you can use the approach Nitesh described above.
Or, you can create the whole JavaScript function on the server, and then use RegisterClientScript in inject it onto the page.
Gary