How to implement a "value" property for Web User Controls?

H

Harvey Triana

Hi--
Sample. When i write something like:
<input type="button" name="btnSend" value="Send"
onclick="callSomeTask(getElementById('myControl').value );return false;
/>

I can run some cliente code if "myControl" is an Asp Textbox;
getElementById('myControl').value get current Text of Textbox

If i write a Web User Control, i dont know how to implemente the property
"value", value runs for the cliente not in server.

Thanks,
<Harvey Triana />
 
R

Ray Costanzo

I'm not certain I understand what you're asking, but I'll take a guess. You
need the client ID an html element that asp.net generates, correct? There
is a .ClientID property that will expose that. So, you have to pull that
client ID from your code behind and inject it into your client-side code in
someway. You can build strings in your codebehind containing javascript
functions, but I personally find this to be a PITA. So, I use a Literal
that's hidden and put my client-side code in there, alter it as needed, and
then register the text of it. This makes for a better Visual Studio
experience. Here's a sample:




codebehind:
protected void Page_Load(object sender, EventArgs e)
{
litClientScript.Text = litClientScript.Text.Replace("%someTextBox%",
someTextBox.ClientID);
Page.RegisterClientScriptBlock("JS", litClientScript.Text);
}


..aspx:



<asp:textbox id="someTextBox" runat="server" text="here's some text" />
<input type="button" onclick="giveIt();" value="Click me" />

<asp:literal id="litClientScript" runat="server" visible="false">
<script type="text/javascript">
function giveIt() {
alert(document.getElementById('%someTextBox%').value);
}
</script>
</asp:literal>


Ray at work
 
B

bruce barker

this will depend on what dom elements your user control renders.

-- bruce (sqlwork.com)
 
H

Harvey Triana

this will depend on what dom elements your user control renders.
A sample, please...

<Harvey Triana />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top