加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Gridview格式字段为电话号码,但有些结果是4位数的扩展名,

发布时间:2020-12-15 21:55:47 所属栏目:百科 来源:网络整理
导读:所以我在gridview中显示SQL表的结果.一些字段是电话号码.特定字段可以是正常的10位数字,但也可以是4位数字的扩展名.如果它是一个4位数字,我想至少不要在其上放置10位数字格式,最多我想在它前面加上Ext:然后是我的数据.这是我到目前为止所拥有的.我通常不是
所以我在gridview中显示SQL表的结果.一些字段是电话号码.特定字段可以是正常的10位数字,但也可以是4位数字的扩展名.如果它是一个4位数字,我想至少不要在其上放置10位数字格式,最多我想在它前面加上Ext:然后是我的数据.这是我到目前为止所拥有的.我通常不是程序员,所以这是Visual Studio向导和谷歌结果拼凑在一起.
非常感谢您的帮助.

<form id="form1" runat="server">
<div>

    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"   AutoGenerateColumns="False">
        <Columns>
           <asp:TemplateField HeaderText="Call Destination" SortExpression="CallDestination">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CallDestination") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(DataBinder.Eval (Container.DataItem,"CallDestination")))%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:OnCallConnectionString %>" SelectCommand="SELECT [TimeStamp],[CallerID],[Accepted],[CallDestination] FROM [OnCallLog]"></asp:SqlDataSource>

</div>
</form>

解决方法

您需要使用RowDataBound事件拦截每一行,因为它绑定到网格,以便您可以确定电话号码是10位数还是4位数,并根据具体情况处理每个值,如下所示:

标记:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
     AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound">

注意:删除Text =’<%#String.Format(“{0:(###)### - ####}”,Convert.ToInt64(DataBinder.Eval(Container.DataItem,“CallDestination”) )))%>”来自< asp:Label>在< ItemTemplate>中,因为您将格式化文本并在RowDataBound事件中设置Text属性而不是声明性地.

代码隐藏:

protected void GridView1_RowDataBound(Object sender,GridViewRowEventArgs e)
{
    // Only interested in each data row,not header or footer,etc.
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        // Find the Label2 control in the row
        Lable theLabel = (Label)e.row.FindControl("Label2");

        // Make sure control is not null
        if(theLabel != null)
        {
            // Cast the bound to an object we can use to extract the value from
            DataRowView rowView = (DataRowView)e.Row.DataItem;

            // Get the value for CallDestination field in data source
            string callDestinationValue = rowView["CallDestination"].ToString();

            // Find out if CallDestination is 10 digits or 4 digits
            if(callDestinationValue.Length == 10)
            {
                theLabel.Text = String.Format("{0:(###) ###-####}",Convert.ToInt64(rowView["CallDestination"]));
            }
            if(callDestinationValue.Length == 4)
            {
                theLabel.Text = "Ext: " + callDestinationValue;
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读