I need to declare the 'text value' of the dropdownlist 'dropCallbackReason' into the 'ValueHiddenField' ID of the so that I can then use it as a javascript variable.
I need to be able to declare the HiddenField through C# as aswell as declaring the Javascript Variable 'callBackReason' through c# as well, any ideas how to do this through C#?
.cs page.
protected void Page_Load(object sender, EventArgs e)
{
HiddenField hiddenField = new HiddenField { ID = "ValueHiddenField", Value = "test" };
theForm.Controls.Add(hiddenField);
string script = @"function updateCallBackReason() {
callBackReason = document.getElementById('<%=ValueHiddenField.ClientID %>').value;
return callBackReason;
}";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callBackReasonScript", script, true);
.aspx
<asp:label runat="server" ID="lblCallbackReason" AssociatedControlID="dropCallbackReason" CssClass="textLabel">Reason for callback:</asp:label>
<asp:DropDownList runat="server" ID="dropCallbackReason" onChange="updateCallBackReason" ClientIDMode="Static" >
<asp:ListItem Text="-- Select Reason --" Value="1"></asp:ListItem>
<asp:ListItem Text="Booking a Test Drive" Value="6"></asp:ListItem>
<asp:ListItem Text="Discussing a Purchase" Value="11"></asp:ListItem>
<asp:ListItem Text="Contract Hire Quotation" Value="45"></asp:ListItem>
</asp:DropDownList>
Here is how to add a HiddenField
control programmatically. Note that controls cannot be added into Page.Controls
directly - they should be placed into some container, like ContentPlaceholder
or Panel
:
HiddenField hiddenField = new HiddenField {ID = "ValueHiddenField", Value = "test"};
SomePanel.Controls.Add(hiddenField);
And here is how to register a script block:
string script = @"function updateCallBackReason() {
callBackReason = document.getElementById('<%=ValueHiddenField.ClientID %>').value;
return callBackReason;
}";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callBackReasonScript", script, true);
Good places to do this are either Page_Load
or Page_PreRender
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.