我有一个带有隐藏字段的表单:
<form id="Form1" runat="server" style="width: 100%; height: 100%; overflow: hidden" onsubmit="return false;">
<div>
<input type="hidden" runat="server" id="TrackColors" value=""/>
<input type="hidden" runat="server" id="Relogin" value=""/>
</div>
</form>
在服务器端调用 Page_Load() 函数后:
protected void SomeFunction()
{
Dictionary<int, int> trackColors = new Dictionary<int, int>();
if (!String.IsNullOrEmpty(TrackColors.Value))
trackColors = ReadValues(TrackColors.Value);
//if value is null or empty it's assigned to a different
TrackColors.Attributes["value"] = FormValues(trackColors); //FormValues() return string
//change is visible
}
string FormValues(Dictionary<int, int> values)
{
string result = "";
if (values == null || values.Count == 0)
return result;
foreach (KeyValuePair<int, int> p in values)
result += p.Key + "@@" + p.Value + "^^";
result = result.TrimEnd('^');
return result;
}
如果我更改 ComboBox 的选定字段,则调用该函数:
<dx:ASPxTextBox ID="ColorTrackCarID" Visible="false" Text='<%# Eval("CarId") %>' />
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" SelectedIndex='<%# Eval("TrackColor") %>'
ValueType="System.String" Width="30" ShowImageInEditBox="true"
ondatabinding="ASPxComboBox1_DataBinding">
<ClientSideEvents SelectedIndexChanged="function (s,e) {
if (window.TrackColorChanged != null)TrackColorChanged(s,e); }" />
</dx:ASPxComboBox>
function TrackColorChanged(s, e) {
var TrackColors = document.getElementById('TrackColors');
if (TrackColors == null || TrackColors.value == "")
return values;
//values is always emply
}
我了解表单字段的值不会传递回客户端。问题是:如何将这些值传回来?
而如果我在Page_Load()中改变服务器端的值,那么客户端就可以看到一切了,也就是
protected void Page_Load(object sender, EventArgs e)
{
TrackColors.Attributes["value"] = "bla-bla-bla";
//All changes are visible on the client-side
}
感谢您的关注。