2010-08-02 51 views
3

您知道iDevices如何根據類型屬性的HTML 5值顯示不同的鍵盤佈局嗎?因此,<input type="email"...將顯示iPad的電子郵件keboard佈局。我正在使用.net文本框,但希望讓iDevices顯示該字段的相應鍵盤佈局。但是,當TextBox控件呈現時,type屬性被覆蓋爲「text」。有任何想法嗎?帶有asp.net的iPhone電子郵件鍵盤佈局文本框

回答

6

不容易解決,但你可以創造一些新的控制,如:

public EnhancedTextBox : TextBox { 
    public Html5Type Html5Type { get; set; } 

    override AddAttributesToRender(HtmlTextWriter writer) { 
      // add attribute according to the selected type in the property 
      // something like 
      writer.AddAttribute("type", "email"); 
      base.AddAttributesToRender(writer); 
    } 
} 

,並用它來代替正常的文本框

+0

這是輝煌的,但我有一個後續問題:一旦我創建了這個類,我如何在我的標記中使用它?是否有某處需要放置此代碼,以便我的.aspx文件能識別這個新類? – Andrew 2012-03-22 19:38:39

+0

試試這個,通過'<%@ Register Assembly =「ServerControl」TagPrefix =「mytag」命名空間=「ServerControl」%>'然後通過'' – 2012-03-22 20:01:15

+0

「ServerControl」是我應該放在那裏的實際文本,還是代表別的東西?系統無法找到該文件,我收到錯誤消息。 – Andrew 2012-03-23 17:47:43

0

也可以實現與「<輸入RUNAT =更換'服務器'/ >「

然而,當你定義<輸入RUNAT = '服務器' 出現問題珍聞型= '郵件' ID = 'txtEmail'/使用Visual Studio 2010的aspx文件中的。

在使用type ='email'時,Visual Studio將刪除實例變量定義 - 「protected global :: System.Web.UI.HtmlControls .HtmlInputText txtEmail;「在[...] aspx.designer.cs文件中。

因此,您必須在aspx中只定義「< input runat ='server'id ='txtEmail'/ >」。 txtEmail.Attributes.Add(「type」,「email」);而不是在[。] .aspx中添加type ='email'屬性,您可以在文件後面的代碼中提供一個屬性,如下所示:

5

.NET Framework 4的更新允許您指定類型屬性。

http://support.microsoft.com/kb/2468871

查看功能3一路下跌的頁面

Feature 3 

New syntax lets you define a TextBox control that is HTML5 compatible. For example, the following code defines a TextBox control that is HTML5 compatible: 

<asp:TextBox runat="server" type="some-HTML5-type" /> 
2

我只是解決了這個問題,控制適配器< ASP:文本框>。將這個類添加到項目的某個位置,並從App_Browsers中的.browser文件中引用它,它將讓您設置想要輸入控件的類型。

public class TextBoxControlAdapter : ControlAdapter 
{ 
    protected override void Render(HtmlTextWriter writer) { 
     var textBox = Control as WebControl; 
     if (textBox != null) { 
      var type = textBox.Attributes["type"]; 
      if (!String.IsNullOrEmpty(type)) { 
       writer.AddAttribute("type", type); 
      } 
     } 
     base.Render(writer); 
    } 
} 

在App_Browsers文件\ AdapterMappings.browser(例如),補充一點:

<adapter controlType="System.Web.UI.WebControls.TextBox" adapterType="MyProject.ControlAdapters.TextBoxControlAdapter" /> 
+0

這會將'type =「email」'添加到渲染輸出中,但是仍然是另一個'type'屬性,其值爲「text」。 – 2012-02-08 15:05:03

1

還有一個解決方案。不幸的是,以上所有內容都需要大量編碼,新類,KB安裝,它不針對iPhone OS 3+進行更新。

使用這個,如果你想你的手機顯示一個數字鍵盤在ASP.NET TextBox控件: 模式=「[0-9] *」

<asp:textbox runat="server" id="txtNumber" pattern="[0-9]*" /> 
+0

優秀 - 不需要編碼,可以在asp.net framework v2.0上運行。 – GlennG 2013-10-15 08:54:14

+0

唉,只有iPhone解析模式並呈現數字鍵盤:Android不。還應該與title屬性一起使用;但是,這一點在觸摸設備上不受支持。 – GlennG 2013-10-15 09:13:58