using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Specialized; namespace WebBlocks { public class ListInput : BlockBase, IPostBackDataHandler { private string RawText { get { return (ViewState["RawText"] == null ? "" : (string)ViewState["RawText"]); } set { ViewState["RawText"] = value; } } public string CssClass { get { return (ViewState["CssClass"] == null ? "" : (string)ViewState["CssClass"]); } set { ViewState["CssClass"] = value; } } public string[] Items { get { return RawText.Split('|'); } } public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) { string postedValue = postCollection[postDataKey]; if (postedValue == null || !postedValue.Equals(RawText)) { RawText = postedValue; return true; } return false; } public virtual void RaisePostDataChangedEvent() { // nothin } protected override void OnLoad(EventArgs e) { if (this.Visible) { if (!Page.ClientScript.IsClientScriptBlockRegistered("ListInputJS")) { #region Javascript Page.ClientScript.RegisterClientScriptBlock(typeof(string), "ListInputJS", @" function listInput_keydown(e) { var keynum; if(!e) // IE { e = window.event; keynum = e.keyCode; } else keynum = e.which; var target = e.target || e.srcElement; if (keynum == 13) { var parentTable = target.parentNode.parentNode.parentNode.parentNode; // add row if this input box is the last in the table if (target == parentTable.firstChild.lastChild.firstChild.firstChild) listInput_addRow(parentTable); } } function listInput_onkeypress(e) { var keynum; if(!e) // IE { e = window.event; keynum = e.keyCode; } else keynum = e.which; if (keynum == 13) return false; else return true; } function listInput_onblur(e) { if(!e) // IE e = window.event; var target = e.target || e.srcElement; var parentTable = target.parentNode.parentNode.parentNode.parentNode; if (target.value == '' && (target != parentTable.firstChild.childNodes.length > 2 && target != parentTable.firstChild.lastChild.firstChild.firstChild)) { // remove this item parentTable.firstChild.removeChild(target.parentNode.parentNode); } } function listInput_addRow(targetTable) { var lastNode = targetTable.firstChild.lastChild; var lastInput = targetTable.firstChild.childNodes[targetTable.firstChild.childNodes.length-2].firstChild.firstChild; var underscorePos = lastInput.id.indexOf('_'); var lastIndex = parseInt(lastInput.id.substring(0,underscorePos)); var idPostfix = lastInput.id.substring(underscorePos) lastIndex++; var newTR = document.createElement('tr'); var newTD = document.createElement('td'); var newInput = document.createElement('input'); newInput.type = 'text'; newInput.id = lastIndex + idPostfix; newInput.onkeydown = function(e){listInput_keydown(e);}; newInput.onblur = function(e){return listInput_onblur(e);}; newInput.onkeypress = function(e){return listInput_onkeypress(e);}; newTD.appendChild(newInput); newTR.appendChild(newTD); targetTable.firstChild.insertBefore(newTR, lastNode); newInput.focus(); } function listInput_add(e) { if(!e) // IE e = window.event; var target = e.target || e.srcElement; listInput_addRow(target.parentNode.parentNode.parentNode.parentNode); return false; } function listInput_save(fieldname, tablename) { var field = document.getElementById(fieldname); var table = document.getElementById(tablename); var val = ''; var len = table.firstChild.childNodes.length-1; for(var i = 0; i < len; i++) val += table.firstChild.childNodes[i].firstChild.firstChild.value + '|'; // set the return value, and chop off the last '|' if (val.length > 0) field.value = val.substring(0, val.length-1); } ", true); #endregion } // set the onsubmit statement Page.ClientScript.RegisterOnSubmitStatement(typeof(string), "ListInputJS_OnSubmit" + ClientID, "listInput_save('" + ClientID + "','tbl" + ClientID + "');"); } base.OnLoad(e); } protected override void Render(HtmlTextWriter output) { output.Write(""); output.Write("
| Add More |