简体   繁体   中英

How to force a value in asp.net TextBox and Bind at the same time?

I'm currently working on a phone input control with 3 asp.net TextBoxes (international, regional and actual number) that are linked to a datasource with a 2-way databind. I was asked to force a "+" in the international textbox (when editing or creating). The idea is similar to this:

<asp:TextBox ID="txtInternational" runat="server" Text='+<%# Bind("telephone_international")%>' />

Which does not work in this case.

Do anyone have and idea, should I do it using RegEx?

Thanks PatH

<asp:TextBox ID="txtInternational" runat="server" 
Text='<%# string.IsNullOrEmpty(Eval("telephone_international")) ? "" : "+" + Eval("telephone_international")%>' />

If you used textBoxes under GridView, ListView, Repeater or DataList controls then register a ItemDataBound event. And in code behind write this (Note: following event is for DataList control): -

protected void dlSample_ItemDataBound(object sender, DataListItemEventArgs e)
        {

            TextBox tb = (TextBox)e.Item.FindControl("txtInternational");
            tb.Text = "+" + tb.Text;

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM