[英]ASP.net Recover JavaScript Created Attribute from PostBack
我創建了一個派生自TextBox的控件。 在該類上添加了對Javascript代碼的調用,當文本內容更改時,它將向控件添加名為“ post”且值為“ true”的屬性。 我希望收集這一價值。
到目前為止,我有這個:
均價
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="CWEB.Web.UI.USR.DefPage" %>
<%@ Register TagPrefix="Controls" Namespace="CWEB.Web.Controls" Assembly="Web" %>
<html>
<head>
<script type="text/javascript">
function SetPost(element){if(!element.hasAttribute('post')){z=document.createAttribute('post');z.value='true';element.setAttributeNode(z);}else{element.setAttribute('post','true');}}
</script>
</head>
<body>
<Controls:TextBox runat="server" ID="abc" />
</body>
</html>
CodeBehind(和TextBox派生類)
using SysWeb = global::System.Web.UI;
using CtrWeb = global::CWEB.Web.Controls;
namespace CWEB.Web
{
internal static class Binder
{
internal const string PostableKey = "post";
internal const string AuxFieldKey = "_ck";
internal static bool GetCheck(string ck) { return (ck != null && ck != "" && (ck == global::CWEB.Data.Fields.Boolean.ValueTrue || ck.Contains("t") || ck.Contains("v"))); }
private static bool GetCheckInput(CtrWeb.Controls.Generic.FieldControl Control, ref bool Found)
{
if (Control == null || Control.Page == null) { Found = false; }
else
{
string value = Control.Page.Request.Form[Control.ClientID + CtrWeb.Binder.AuxFieldKey]; //If then it's child is null, it means it's unchecked.
Found = (value != null && value != "");
return ((Found) ? (value == "1" || value == "on" || value.Contains("t")) : false);
}
return false;
}
internal static bool GetCheck(CtrWeb.Controls.Generic.FieldControl Control, bool OutSideInput, string ViewStateKey = CtrWeb.Binder.CheckAttribute)
{
string value = Control.Page.Request.Form[Control.UniqueID]; //If the main control exists then it's child shal too.
if (value == null || value == "") { return CtrWeb.Binder.GetCheck((string)Control.GetViewState()[ViewStateKey]); }
else if (OutSideInput)
{
bool Found = false;
return CtrWeb.Binder.GetCheckInput(Control, ref Found);
} else { return CtrWeb.Binder.GetCheck((string)Control.GetAttributes()[ViewStateKey]); }
}
internal static void SetCheck(CtrWeb.Controls.Generic.FieldControl Control, bool OutSideInput, string ViewStateKey = CtrWeb.Binder.CheckAttribute)
{
bool FValue = CtrWeb.Binder.GetCheck(Control, OutSideInput, ViewStateKey: ViewStateKey);
Control.GetViewState()[ViewStateKey] = ((FValue) ? "true" : "false");
}
}
namespace Controls
{
public interface FieldControl
{
string ClientID { get; }
string UniqueID { get; }
SysWeb.Page Page { get; }
SysWeb.AttributeCollection GetAttributes();
SysWeb.StateBag GetViewState();
}
public class TextBox : SysWeb.WebControls.TextBox, CtrWeb.FieldControl
{
public bool Postable
{
get { return CtrWeb.Binder.GetCheck(this, false, ViewStateKey: CtrWeb.Binder.PostableKey); }
set { CtrWeb.Binder.SetCheck(this, false, ViewStateKey: CtrWeb.Binder.PostableKey); }
}
protected override void LoadViewState(object savedState)
{
this.BaseLoadViewState(savedState);
this.Postable = CtrWeb.Binder.GetCheck(this, false, ViewStateKey: CtrWeb.Binder.PostableKey);
}
protected override void OnInit(global::System.EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.Attributes.Add("onchange", "SetPost(this)");
this.Attributes.Add(MdWeb.Binder.PostableKey, "false");
}
base.OnInit(e);
}
}
}
namespace UI.USR
{
public class DefPage : SysWeb.Page { protected CtrWeb.TextBox abc; }
}
}
一些代碼未在此處復制,因為它們與問題無關。
為了將該數據發布到服務器,您必須將其存儲在表單值中。
完整的HTML元素不會發布到服務器。 僅表單元素的鍵/值對是。 (WebForms試圖欺騙您以為整個頁面都已發布到服務器上,但這是一個謊言。)
向頁面添加一個隱藏的表單字段,就像這樣簡單:
<asp:Hidden id="someHiddenField" />
然后在JavaScript中,設置該字段的值:
document.getElementById('<%= someHiddenField.ClientID %>').value = 'true';
然后,當頁面回發到服務器時, 'true'
值將在該隱藏字段中:
someHiddenField.Value
asp:隱藏不存在。 只有表單元素可以實際回發數據。 如果設置了runat = server,則服務器端將獲取回發數據並將其加載到form元素中。
在標記或html中:
<input type="hidden" runat="server" ID="txtHiddenDestControl" />
javascript:
document.getElementById('<%= txtHiddenDestControl.ClientID %>').value = '1';
后面的代碼:
string postedVal = txtHiddenDestControl.Value.ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.