How to get value from DetailsView Control in ASP.NET?

♀尐吖头ヾ 提交于 2019-12-04 13:06:57

Unfortunately, trying to access the Text of ..Cells[1] is not what you are looking for. You need the value of the TextBox control within that cell:

protected void dvApplicantDetails_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
    if (e.CommandName == "Update")
    {
        string firstName = ((TextBox)dvApplicantDetails.Rows[1].Cells[1].Controls[0]).Text;
        string lastName = ((TextBox)dvApplicantDetails.Rows[2].Cells[1].Controls[0]).Text
    }
}

Bryuk,

Try OnItemUpdating event instead. You can get new values as shown below:

protected void dvApplicantDetails_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
    var dv = sender as DetailsView;

    var firstName = e.NewValues[1].ToString();
    var lastName = e.NewValues[2].ToString();
}

Add OnItemUpdating="dvApplicantDetails_ItemUpdating" attribute to DetailsView control in aspx.

I needed to do this earlier today...1up for the questions and answers that helped me. Heres a function I came up with.

protected void Page_Load(object sender, EventArgs e)
{
    string sFirstName = GetDVRowValue(dvApplicantDetails, "ApplicantID")
}

public string GetDVRowValue(DetailsView dv, string sField)
{
    string sRetVal = string.Empty;
    foreach (DetailsViewRow dvr in dv.Rows)
    {
       if (dvr.Cells[0].Text == sField)
           sRetVal = dvr.Cells[1].Text;
    }
    return sRetVal;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!