laihailin
laihailin
2017-11-27 02:44

NumberBox(textbox)的值重新修改不了,总输出是0,在NumberBox中输入其他数字总是输出0,求解

  • 自定义textbox

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" meta:resourcekey="PageResource1" enableEventValidation="false" viewStateEncryptionMode="Never"%>

<%@ Register Assembly="B2CShop.Control" Namespace="B2CShop.Control" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



无标题页



</div>

<cc1:NumberBox ID="txtOrderIDAdd" runat="server" IsInt="True" />
  <asp:Button ID="btnAddSubmit" runat="server" meta:resourcekey="btnAddResource1" OnClick="btnAddSubmit_Click"
                                Text="确定" CssClass="button" />



</form>



后台代码:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

       // this.txtOrderIDAdd.Value = "3";
    }
}
protected void Button1_Click(object sender, EventArgs e)
{

}
protected void btnAddSubmit_Click(object sender, EventArgs e)
{

    System.Web.HttpContext.Current.Response.Write("<Script Language='JavaScript'>window.alert('" + this.txtOrderIDAdd.Value.ToString() + "');</script>"); 
}

}

自定义NumberBox控件类:
namespace B2CShop.Control
{
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

[DefaultProperty("Value"), ToolboxData("<{0}:NumberBox runat=server></{0}:NumberBox>")]
public class NumberBox : WebControl
{
    private HiddenField _ThisHidden01 = new HiddenField();

    public NumberBox()
    {
        this.ViewState["IsInt"] = false;
        this.ViewState["Value"] = "0";
        this.ViewState["ClientScriptOnChange"] = "";
        this.ViewState["Class"] = "";
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        ControlCommon.ResgiterJavascript(this.Page, base.GetType(), "Control_NumberBox");
    }

    protected override void RenderContents(HtmlTextWriter output)
    {
        HtmlInputText text = new HtmlInputText();
        this._ThisHidden01.ID = this.UniqueID + "_hid01";
        text.ID = this.UniqueID + "_ctl01";
        text.Value = this.Value;
        if (this.Width.Value != 0.0)
        {
            text.Style.Add("Width", this.Width.Value + ControlCommon.GetUnitTypeName(this.Width.Type));
        }
        if (this.Class != "")
        {
            text.Attributes.Add("Class", this.Class);
        }
        if (this.MaxLength != "")
        {
        }
        text.Style.Add("ime-Mode", "disabled");
        if (this.IsInt)
        {
            if (this.ClientScriptOnChange.Length > 0)
            {
                text.Attributes.Add("onblur", "return (IntBox_OnChange(" + this._ThisHidden01.ID + ") && " + this.ClientScriptOnChange + ");");
            }
            else
            {
                text.Attributes.Add("onblur", "return IntBox_OnChange(" + this._ThisHidden01.ID + ");");
            }
            text.Attributes.Add("onkeydown", "return IntBox_OnKeyDown();");
        }
        else
        {
            if (this.ClientScriptOnChange.Length > 0)
            {
                text.Attributes.Add("onblur", "return (NumberBox_OnChange(" + this._ThisHidden01.ID + ") && " + this.ClientScriptOnChange + ");");
            }
            else
            {
                text.Attributes.Add("onblur", "return NumberBox_OnChange(" + this._ThisHidden01.ID + ");");
            }
            text.Attributes.Add("onkeydown", "return NumberBox_OnKeyDown();");
        }
        text.Attributes.Add("maxlength", this.MaxLength);
        if (!this.Enabled)
        {
            text.Attributes.Add("readonly", "readonly");
        }
        this._ThisHidden01.RenderControl(output);
        text.RenderControl(output);
    }

    [Category("Appearance"), Localizable(true), Description("设置Textbox的Class属性。"), Bindable(true), DefaultValue("")]
    public string Class
    {
        get
        {
            return (string)this.ViewState["Class"];
        }
        set
        {
            this.ViewState["Class"] = value;
        }
    }

    [DefaultValue(""), Bindable(true), Category("Appearance"), Localizable(true), Description("设置或获取客户端脚本:内容变更事件.\n注意:但在控制内置变更事件之后触发,如果内置变更事件返回为false,则设置将不会被执行!")]
    public string ClientScriptOnChange
    {
        get
        {
            return (string)this.ViewState["ClientScriptOnChange"];
        }
        set
        {
            this.ViewState["ClientScriptOnChange"] = value;
        }
    }

    [DefaultValue(false), Description("是否只能输入整数.\nTrue : 只能输入整数\nFalse: 可以输入实数."), Category("Appearance"), Bindable(true), Localizable(true)]
    public bool IsInt
    {
        get
        {
            return (bool)this.ViewState["IsInt"];
        }
        set
        {
            this.ViewState["IsInt"] = value;
        }
    }

    [Localizable(true), Bindable(true), Description("设置或获取控件最大可输入的字符数!"), Category("Appearance"), DefaultValue("")]
    public string MaxLength
    {
        get
        {
            return ((this.ViewState["MaxLength"] == null) ? "8" : ((string)this.ViewState["MaxLength"]));
        }
        set
        {
            this.ViewState["MaxLength"] = value;
        }
    }

    [Description("设置或获取控件的值."), Bindable(true), Category("Appearance"), DefaultValue("0"), Localizable(true)]
    public string Value
    {
        get
        {
            string str = "";
            if (base.DesignMode)
            {
                str = (string)this.ViewState["Value"];
            }
            else if (HttpContext.Current.Request.Form[this.ID + "_hid01"] != null)
            {
                str = HttpContext.Current.Request.Form[this.ID + "_hid01"].ToString();
                this._ThisHidden01.Value = str;
            }
            else
            {
                str = this._ThisHidden01.Value;
            }
            return ((str == "") ? "0" : str);
        }
        set
        {
            this._ThisHidden01.Value = value;
            if (base.DesignMode)
            {
                this.ViewState["Value"] = value;
            }
        }
    }
}

}


关联类 :
ControlCommon.cs
namespace B2CShop.Control
{
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public static class ControlCommon
{
    public static void CopyStyle(CssStyleCollection Desc, CssStyleCollection Src)
    {
        foreach (string str in Src.Keys)
        {
            if (Desc[str] != null)
            {
                Desc[str] = Src[str];
            }
            else
            {
                Desc.Add(str, Src[str]);
            }
        }
    }

    public static string GetUnitTypeName(UnitType ut)
    {
        string[] strArray = new string[] { "", "px", "pt", "pc", "in", "mm", "cm", "%", "em", "ex" };
        try
        {
            return strArray[Convert.ToInt32(ut)];
        }
        catch
        {
            return "";
        }
    }

    public static void ResgiterJavascript(Page pg, Type tp, string Filename)
    {
        try
        {
            if (!pg.ClientScript.IsClientScriptIncludeRegistered("B2CShop_" + Filename))
            {
                pg.ClientScript.RegisterClientScriptInclude("B2CShop_" + Filename, pg.ClientScript.GetWebResourceUrl(tp, "B2CShop.Control.B2CShop.Control." + Filename + ".js"));
            }
        }
        catch
        {
        }
    }
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答