为了实现您的目标,分配ID
给_ddl
并将其作为参数传递给GetPostBackEventReference
.
DropDownList _ddl = new DropDownList();
_ddl.ID = "MyDropDownList";
_ddl.Attributes.Add(HtmlTextWriterAttribute.Onchange.ToString()
, this.Page.ClientScript.GetPostBackEventReference(this, _ddl.ID));
然后,RaisePostBackEvent
您需要通过其ID
提供的控件找到您的控件,eventArgument
并以这种方式获取SelectedValue
.
public void RaisePostBackEvent(string eventArgument)
{
DropDownList _ddl = FindControl(eventArgument) as DropDownList;
if (_ddl == null) return;
string selectedValue = _ddl.SelectedValue;
// do whatever you need with value
}
为什么不能使用 JavaScript this.value
?不支持 JavaScript 调用,如果您查看生成的 HTML,您将看到:
__doPostBack('ctl02','MyDropDownList');
其中__doPostBack
函数如下:
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
如您所见,参数recipient
等于用户控件的参数。当你通过电话时,它就到了。将值分配给隐藏字段,然后与表单一起提交。这是调用的第二个参数。ctl02
UniqueID
this
GetPostBackEventReference
eventArgument
__EVENTARGUMENT
GetPostBackEventReference
所以第二个参数GetPostBackEventReference
总是被内部类 System.Web.UI.Util.QuoteJScriptString
方法编码为字符串。