Sunny
Sunny

Reputation: 89

how to store a part of dropdownlist text in a dynamically generated property

here is my code:

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++;
    }
}

now the thing is that the dropdown list text has both the name and mobile number concatenated and seperated by "/" character. what i want to do is to just save the name of the person in the property en[count].all_Emp_Name., is there a method in c# like Remove or something like that which can remove the the latter part i.e the mobile number from the string.

Remove method demands the index from where you want to remove the text and for that i might use Indexof but i am a little confused here how to go about it, as the property is generated dynamically in the for loop

Upvotes: 0

Views: 50

Answers (2)

Sunny
Sunny

Reputation: 89

got it finally....

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

this seems to be working just perfect.

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

do something like this:

string DropDownText = "EhsanSajjad/03123009099";

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

string personName  = temp[0];

string CellNumber = temp[1];

Upvotes: 2

Related Questions