2013-05-14 12 views
9

JavaScript kullanmadan ASP.NET'te sayısal bir artış kullanmanın bir yolu var mı?ASP.NET için sayısal bir UpDown denetimi var mı?

Ve değilse, bir alternatif var mı?

+0

alternatif hangi problemi nasıl? Javascript kullanmadan ne demek istiyorsun? düz HTML? bu tür almanın http://tjvantoll.com/2012/07/15/native-html5-number-picker-vs-jquery-uis-spinner-which-to-use/ –

+0

@CasparKleijne alternatif yolu: yok yok Javascript kullanmadan kontrol – LukeHennerley

+0

Web formumda Quantity adında bir alanım var. Bir metin kutusu sayısal olduğundan emin olmak için pek çok doğrulayıcıya ihtiyaç duyuyordu, dolayısıyla sayısal bir updown nesnesinin arayüz ve kod için daha kullanıcı dostu olduğunu düşündüm. Updown nesnesine herhangi bir alternatif var mı? bir metin kutusundan başka? – HelpASisterOut

cevap

4

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/NumericUpDown/NumericUpDown.aspx

<ajaxToolkit:NumericUpDownExtender ID="NUD1" runat="server" 
    TargetControlID="TextBox1" 
    Width="100" 
    RefValues="January;February;March;April" 
    TargetButtonDownID="Button1" 
    TargetButtonUpID="Button2" 
    ServiceDownPath="WebService1.asmx" 
    ServiceDownMethod="PrevValue" 
    ServiceUpPath="WebService1.asmx" 
    ServiceUpMethod="NextValue" 
    Tag="1" /> 

Ayrıca PM> Install-Package AjaxControlToolkit

+0

-1 OP'nin javascript kullanmadan bir çözüme ihtiyacı var. Bu çözüm javascript gerektirir. –

+2

@CasparKleijne Yukarıdaki kod hangi noktada javascript kullanır? Saf HTML ve muhtemelen kendisi için mevcut en iyi/tek çözümdür. Temel kod javascript kullanabilir, ancak çözüm yapmaz, OP'nin javascript kullanmak istememesinin ana sebebi, bunu kullanamadıkları için anlamıyor olmalarıdır. – LukeHennerley

0

aşağıdaki html sorunuza cevap düşünüyorum kullanarak NuGet ile referans eklemeyi düşünün Ajax Control Toolkit içine bakınız:

<head runat="server"> 
    <title>Numeric Up Down</title> 
    <script type="text/jscript"> 
     function Load() { 
      /*numericUpDown1.value = or*/ document.getElementById("numericUpDown1").value = parseFloat(document.getElementById("<%=NumericUpDown1.ClientID%>").value); 
     } 
     function Change() { 
      document.getElementById("<%=NumericUpDown1.ClientID%>").value = document.getElementById("numericUpDown1").value; //or numericUpDown1.value 
     } 
    </script> 
</head> 
<body onload="Load()"> 
    <form id="form1" runat="server"> 
    <div> 
     <input type="number" id="numericUpDown1" onchange="Change()" /> 
     <asp:HiddenField ID="NumericUpDown1" runat="server" /> 
    </div> 
</form> 
</body> 

Ve sonra C# veya Visual Basic asp sunucu tarafı kodu, sen NumericUpDown olarak bu HiddenField tedavi, ancak onun değerini System.Windows.Forms.NumericUpDown kontrolü gibi, dize ve değil ondalık sayıdır not veya şamandıra veya çifte veya int, bu yüzden en çok ihtiyacınız olan şey için bu türlerden birine ayırmanız gerekir.

biçeminde stilini yukarı aşağı, sonra da javascript'te basittir. . Sadece document.getElementById ("numericUpDown1") tarzı ayarlamak, ancak C# veya Visual Basic asp sunucu tarafı kodu ile bunu yapmak istiyorum, o zaman html farklı olmalıdır:

<head runat="server"> 
    <title>Numeric Up Down</title> 
    <script type="text/jscript"> 
     function Change() { 
      document.getElementById("<%=NumericUpDown1.ClientID%>").value = document.getElementById("numericUpDown1").value; //or numericUpDown1.value 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <% this.Response.Write(string.Format("<input type='number' id='numericUpDown1' value='{0}' onchange='Change()' style='{1}' />", this.NumericUpDown1.Value, this.numericUpDown1Style)); %> 
     <asp:HiddenField ID="NumericUpDown1" runat="server" /> 
    </div> 
</form> 
</body> 

numericUpDown1Style, C# veya Visual Basic'te asp sunucu tarafı kodunda tanımlanan dize türüne sahip korumalı bir alandır. bunu değil stilini için sınıfını vermek istiyorsanız

ardından html olmalıdır:

<head runat="server"> 
    <title>Numeric Up Down</title> 
    <script type="text/jscript"> 
     function Change() { 
      document.getElementById("<%=NumericUpDown1.ClientID%>").value = document.getElementById("numericUpDown1").value; //or numericUpDown1.value 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <% this.Response.Write(string.Format("<input type='number' id='numericUpDown1' value='{0}' onchange='Change()' class='{1}' />", this.NumericUpDown1.Value, this.numericUpDown1CssClass)); %> 
     <asp:HiddenField ID="NumericUpDown1" runat="server" /> 
    </div> 
</form> 
</body> 

numericUpDown1CssClass olan tip asp tanımlanan dize olan korumalı bir alandır C# veya Visual Basic'te sunucu tarafı kodu.

bu verilerin stilini ve bir sınıf vermek istiyorsanız, o zaman html html 2. veya html 3. gibidir, ancak tek değişiklik aşağıdaki uyumludur:

<% this.Response.Write(string.Format("<input type='number' id='numericUpDown1' value='{0}' onchange='Change()' style='{1}' class='{2}' />", this.NumericUpDown1.Value, this.numericUpDown1Style, this.numericUpDown1CssClass)); %> 

Bilmen ne düşündüğünü

ise: numericUpDown1Style ve numericUpDown1CssClass 2. den ve # 3

ÖNERİLEN TIP olduğunu Web sitenizde, asp sunucu taraf kodunuzda kullanılan sayısal yukarıdan aşağıya doğru kontroller vardır ve bu, hepsini bu şekilde oluşturmak için dezavantajlıdır, daha sonra web sitenize yeni "Web Kullanıcı Kontrolü" öğesi ekleyebilir ve adlandırabilirsiniz " NumericUpDown".Ardından, kaynak html'sinde, yukarda yayınladığım html # 1 veya html # 2 veya html # 3 veya html # 4 kodunu kopyalayabilirsiniz (sayıları aşağı doğru çizmek veya bir sınıf oluşturmak istiyorsanız veya bir sınıf vermemeniz gerekir) "WebForm", ancak "Web Kullanıcı Denetimi" ve asp sunucu tarafı kodunda aşağıdaki özellikleri yapın (bunlar C# biçimindedir, ancak Visual Basic kullanıyorsanız

public decimal Value { get { return decimal.Parse(this.HiddenField.Value); } set { this.HiddenField.Value = value.ToString(); } } //Like the System.Windows.Forms.NumericUpDown.Value, but if you dislike 'decimal', and you want other type, then you can change the return type and the type Parse. //Note that the ID of the HiddenField is simply "HiddenField", and not "NumericUpDown1", so make sure in the Source html to rename "NumericUpDown1" to "HiddenField", but probably you would like a different ID, so if you gave it different ID, then ensure that in the code you refer this HiddenField with the ID you chose, and not "HiddenField" or "NumericUpDown1". //The following properties are for only if you want to style your Numeric Up Down: protected string style; public string Style { get { return this.style; } set { this.style = value; } } //If you chose, copied, pasted and changed html #2 or html #4, then don't forget to replace this.numericUpDown1Style to this.Style in the source html of the Web User Control. //Optional public Unit Width { get { int startIndex = this.style.IndexOf("width") + 6; if (index != -1) { int endIndex = this.style.IndexOf(';', startIndex); return Unit.Parse(this.style.Substring(startIndex, endIndex - startIndex)); } return Unit.Empty; } set { if (this.style.Contains("width")) { this.style = this.style.Replace("width:" + this.Width.ToString() + ';', "width:" + value.ToString() + ';'); } else { this.style += "width:" + value.ToString() + ';'; } } } //The same you can do with the Height property. //You can replace all the style code with the CssClass code instead, or just add it: protected string cssClass; public string CssClass { get { return this.cssClass; } set { this.cssClass = value; } } //If you chose, copied, pasted and changed html #3 or html #4, then don't forget to replace this.numericUpDown1CssClass to this.CssClass in the source html of the Web User Control. 

Eğer NumericUpDown stil, bu yüzden her ASP.NET kontrolünde de biliyorsanız

, onların kimliğinin arkasına yazabilirsiniz: Ben size kod çevirmek için bir sorun) olacaktır sanmıyorum. Stil ["stil"] = "değer". Eğer MyStyle

için dizekorunmuş alanda tarzı türünü, sonra da NumericUpDown ile bunu değiştirmek mümkün olmasını istiyorsanız

MyStyle tanımı vardır:

public class MyStyle 
{ 
    internal string style; 
    public string this[string style] 
    { 
     get 
     { 
      int startIndex = this.style.IndexOf(style) + style.Length + 1; 
      int endIndex = this.style.IndexOf(';', startIndex); 
      return this.style.Substring(startIndex, endIndex - startIndex) 
     } 
     set 
     { 
      this.style = this.style.Replace(style + ':' + this[style] + ';', style + ':' + value + ';') 
     } 
    } 
} 

Stil özelliğini Web Kullanıcı Kontrol Kanununa bu sınıf eklemek ve değiştirin:

public string Styles 
{ 
    get 
    { 
     return this.style.style; 
    } 
    set 
    { 
     this.style.style = value; 
    } 
} 

ve ardından özelliği ekleyin:

public MyStyle Style 
{ 
    get 
    { 
     return this.style; 
    } 
} 

ve gelen satırı değiştirin: kaynak html unutmayın

protected readonly MyStyle style = new MyStyle(); 

:

protected string style; 

için Bunu değiştirmek için Web Kullanıcı Denetimi. Stile bu.Styles.

NOT: Kodu tek başıma sınamak için sabrım yoktu, bu yüzden işe yaramayabilir, bu yüzden bunu kendiniz düzeltmeniz gerekir. En azından fikrimi anladın.

Düzeltmelerden sonra yanıtımı düzenleyebilir ve yanlış kodu sabit kodunuzla değiştirebilirsiniz.

Çok memnun kalacağım!

Bu Web Kullanıcı Denetimi, istediğiniz ASP NumericUpDown'tır!

16

Aynı şeyi yapmaya çalışıyorum ve asp metin kutusunun bunun için bir seçeneği olduğu ortaya çıkıyor.

<asp:TextBox TextMode="Number" runat="server" min="0" max="20" step="1"/> 

bu bana, mouseovered veya odak verildiğinde, yukarı-aşağı kontrolleri gösterir ve sadece max dk numaraları sağlayan bir metin kutusu verdi: Ne benim için çalıştı bu oldu. Eğer .NET 4 kalıyorsanız o

<input type="number" min="0" max="20" step="1"> 
+6

[Number 'gibi TextMode değerleri] (https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textboxmode (v = vs.110) .aspx) sürüm 4.5'te kullanılabilir. .NET Framework'ün (değil 4). – ChrisW

0

gibi çalışır.

<asp:TextBox runat="server" ID="txtMyTextBox" type="number" min="0" max="10" step="1"/> 

Eğer: 0 ve size ekstra "tip" etiketi ile ASP TextBox denetiminin bir kombinasyonunu kullanabilirsiniz yerel HTML5 giriş türü (daha doğrusu Ajax Control Toolkit dan NumericUpDown yerine) "sayı" kullanmak istiyorum herhangi bir metin girişi engellemek istiyor, hatta Ajax Control Toolkit bir FilteredTextBoxExtender ekleyebilirsiniz:

<ajaxToolkit:FilteredTextBoxExtender runat="server" TargetControlID="txtMyTextBox" FilterType="Numbers" /> 
İlgili konular