How To: Get the Value of a Control Without an instance of the Control
On my quest to write one blog a day in July, here is my first post regarding how to get the value of a control without the control instance. This can be very helpful for HTTP pipeline development or when the value of the control is needed very early in the page lifecycle.
public static string GetControlValueFromRequest(string controlId) { string requestValue = null; controlId = controlId.ToLower(); HttpRequest req = HttpContext.Current.Request; string eventTarget = req.Form["__EVENTTARGET"] ?? string.Empty; if (eventTarget.ToLower().EndsWith(controlId)) { requestValue = req.Form["__EVENTARGUMENT"]; } if (string.IsNullOrEmpty(requestValue)) { foreach (string id in req.Form.AllKeys) { if (id.ToLower().EndsWith(controlId)) { requestValue = req[id]; } } } return requestValue; }