UserControl

  • Thread starter Thread starter dotbet beginner
  • Start date Start date
D

dotbet beginner

I have a hidden field and a usercontrol in a webform. And
I have a button in the user control. When I click this
button I want to access the value of the hidden field on
the form by means of a client-side javascript function.

Any help is appreciated.

Thanks,
Sudhir.
 
Assuming you are using a HTML hidden textbox and a HTML button here is on
way to do it.

set the id and name of the textbox to tbxHidden
add the attribute runat="server" to tbxHidden

set the id and name of the button to btnMyButton
add the attribute runat="server" to btnMyButton

save
double click the webform (this will create object references in the code)

add this line to page_load
btnMyButton.Attributes.Add("OnClick",
"javascript:myButtonClick(document.all." + tbxHidden.ClientID + ")");

javascript can look loke this

function myButtonClick(hiddenTextBox){
alert(hiddenTextBox.value);
}

if this need to run outside of IE make sure you set ID and Name also change
document.all to document.forms(0)

Hope this helps

Jamey...
 
Hi
I have a button in the user control. When I click this
button I want to access the value of the hidden field on
the form by means of a client-side javascript function.

With this code you can access the hidden field value:

HTML page:
<script language="jscript">
function getFieldValue(fieldName)
{
alert(document.forms[0].item(fieldName).value);
}
</script>

Codebehind:
private void Page_Load(object sender, System.EventArgs e)
{
this.Button1.Attributes.Add("OnClick", "javasc" +
"ript:getFieldValue('hidden')");
}

Obviously you can manage the value only in client-code.

If you want to use the value in server code you must define runat=server for
your hidden control,
therefore in the codebehind:
protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenServer;

private void Page_Load(object sender, System.EventArgs e)
{
string value = hiddenServer.Value;
}

Ciao
Giorgio
 
Back
Top