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

laihailin 2017-11-27 11:06:23


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
{
}
}
}

}

...全文
333 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Vito1993 2017-12-05
  • 打赏
  • 举报
回复
webform? updatepanel或者ajax
guanyelong 2017-12-05
  • 打赏
  • 举报
回复
你这应该是 提交后重新加载控件了。控件给的NUmbox的默认值。所以总是零。做个ajax无刷新吧。你后台alert 之后搞不好会改变了页面布局。
小贤820 2017-12-01
  • 打赏
  • 举报
回复
一个文本框,你咋就整的这么复杂;
Hello World, 2017-12-01
  • 打赏
  • 举报
回复
交给前端处理吧,写这么复杂,前端框架很容易处理的

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧