laihailin 2017-11-27 02:44 采纳率: 0%
浏览 1473

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

<%@ 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条回答 默认 最新

  • kaixing1218 2018-02-02 08:49
    关注

    断点调试吧,应该是赋值过程中有问题

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题