非常感谢您的帮助.
<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; } } }}总结
以上是内存溢出为你收集整理的c# – Gridview格式字段为电话号码,但有些结果是4位数的扩展名,如何处理?全部内容,希望文章能够帮你解决c# – Gridview格式字段为电话号码,但有些结果是4位数的扩展名,如何处理?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)