Getting an object back from my GridView rows

前端 未结 4 1790
Happy的楠姐
Happy的楠姐 2021-02-08 10:34

Basically, i want my object back...

I have an Email object.

public class Email{
    public string emailAddress;
    public bool primary;
    public int c         


        
4条回答
  •  梦毁少年i
    2021-02-08 11:21

    Well I ended up looping through my List EmailCollection, which was saved into the ViewState.

    So in the page, a Save button is clicked, when the event is caught, I loop through my List Collection and grab the row from the GridView by index.

    On the GridViewRow I have to use a GridView1.Rows[i].Cells[j].FindControl("myControl1") then get the appropriate value from it, be it a check box, text box, or drop down list.

    I do see that a GridViewRow object has a DataItem property, which contains my Email object, but it's only available during the RowBound phase.

    Unfortunately If/When i need to expand upon this Email Collection later, by adding or removing columns, it'll take a few steps.

    protected void SaveButton_OnClick(object sender, EventArgs e){
        for (int i = 0; i < this.EmailCollection.Count; i++)
        {
            Email email = this.EmailCollection[i];
            GridViewRow row = this.GridView1.Rows[i];
    
            string gv_emailAddress = ((TextBox)row.Cells[0].FindControl("EmailAddress")).Text;
            if (email.EmailAddress != gv_emailAddress)
            {
                email.EmailAddress = gv_emailAddress;
                email.Updated = true;
            }
            ...
        }
    }
    

    I'd still be open to more efficient solutions.

提交回复
热议问题