在ASP.NET中使用逗号后修剪字符串值,C# [英] Trim string value after comma in ASP.NET, C#
问题描述
public string CorrectedPicture(object container)
{
object devNumber = DataBinder.Eval(container, "ImageFileName");
string ret = devNumber.ToString();
//// Add picture link if needed.
DataRow row = ((DataRowView)container).Row;
object ImageFileName = row["ImageFileName"];
string ImageFileName1 = ImageFileName.ToString();
string[] values = ImageFileName1.Split(',');
for (int i = 0; i <= 0; i++)
{
if (ImageFileName != DBNull.Value)
{
string url = @"D:\NewOutlookFiles" + txtProjectId.Text + "/" + values[i].ToString();
string titleText = "Deviation" + values[i].ToString() + "Picture";
string link = "javascript: void openPicture('_" + values[i].ToString() + "', '" + titleText + "', '" + url + "')";
ret = "<a href=\x22" + link + "\x22>" + ret + "</a>";
}
}
return ret;
}
< asp:标签ID =lblPicturerunat =serverText ='<%#this.CorrectedPicture(Container.DataItem)%>'
ItemStyle-wrap =trueItemStyle-Width =10px >
我的尝试:
我的ImageFileName列有多个以逗号分隔的值,并将值绑定到label。它工作正常,但问题是我想只将第一个值绑定到标签而不是所有值来自列
<asp:Label ID="lblPicture" runat="server" Text='<%# this.CorrectedPicture(Container.DataItem) %>'
ItemStyle-wrap="true" ItemStyle-Width="10px">
What I have tried:
My "ImageFileName" column has multiple values separated by comma , and binding values to label. it's working fine but problem is i want to bind only first value to the label not all the values from column
推荐答案
而是运行循环只需分配第一个值完成拆分后的数组。
要从ImageFileName获取第一个值,您可以执行以下操作 -
Instead running a loop just assign the first value in the array after you did the split.
To get the first value from the "ImageFileName", you can do something like following-
string imgfilename= ImageFileName1.Split(',')[0];
如果您怀疑可能的空条目或逗号开头的字符串,您可以使用 StringSplitOption [ ^ ]以防止此类结果值。
If you are suspecting of possible empty entries or a comma in the start of the string, you can make use of StringSplitOption[^] to prevent such values as result.
string imgfilename= ImageFileName1.Split(',',StringSplitOptions.RemoveEmptyEntries)[0];
希望,它会有所帮助。
请告诉我万一你需要进一步的帮助。
谢谢:)
Hope, it helps.
Please let me know in case you need further help on this.
Thanks :)
试试这个
在验证null和dbnull值后进行拆分
try this
do the split after validating null and dbnull values
object ImageFileName = row["ImageFileName"];
if (ImageFileName != DBNull.Value && ImageFileName != null )
{
string value = ImageFileName.ToString().Split(',')[0];
string url = @"D:\NewOutlookFiles" + txtProjectId.Text + "/" + value;
string titleText = "Deviation" + value + "Picture";
string link = "javascript: void openPicture('_" + value + "', '" + titleText + "', '" + url + "')";
ret = "<a href=\x22" + link + "\x22>" + ret + "</a>";
}
我的ImageFileName列具有由逗号分隔的多个值,并将值绑定到label。它工作正常,但问题是我想只将第一个值绑定到标签而不是将所有值绑定到
My "ImageFileName" column has multiple values separated by comma , and binding values to label. it's working fine but problem is i want to bind only first value to the label not all the values from column
拆分并选择第一个。
这篇关于在ASP.NET中使用逗号后修剪字符串值,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!