如何在动态生成的属性中存储下拉列表文本的一部分

时间:2014-03-19 17:45:00

标签: c# asp.net

这是我的代码:

EntityLayer.Entity[] en = new EntityLayer.Entity[counter];
int count = 0;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
    if (CheckBoxList1.Items[i].Selected == true)
    {
        en[count] = new EntityLayer.Entity();
        en[count].all_Emp_Mobile = DropDownList1.SelectedValue.ToString();
        en[count].all_Emp_Name = DropDownList1.Text;
        en[count].all_Stu_Mobile = CheckBoxList1.Items[i].Value.ToString();
        count++;
    }
}

现在的问题是,下拉列表文本的名称和手机号码连接在一起,并由“/”字符分隔。我想要做的只是在属性en[count].all_Emp_Name中保存人的名字。在c#中是否存在类似Remove的方法或类似的东西可以删除后一部分即移动字符串中的数字。

Remove方法要求您从中删除文本的索引以及我可能使用的索引 Indexof但我在这里有点困惑如何去做,因为属性是在for循环中动态生成的

2 个答案:

答案 0 :(得分:2)

做这样的事情:

string DropDownText = "EhsanSajjad/03123009099";

string[] temp = DropDownText.Split('/');

string personName  = temp[0];

string CellNumber = temp[1];

答案 1 :(得分:1)

最终得到它......

 int index = DropDownList1.SelectedItem.Text.LastIndexOf('/');
 string empName = DropDownList1.SelectedItem.Text.Remove(index);
 en[count].all_Emp_Name = empName;

这似乎工作得很完美。