4

I have DetailsView on my page. I set DefaultMode="Edit". Now i want to get value that user will edit in this cell.

<asp:DetailsView ID="dvApplicantDetails" runat="server" 
            AutoGenerateRows="false" DataKeyNames="ApplicantID" DefaultMode="Edit" 
            onitemcommand="dvApplicantDetails_ItemCommand" >
        <Fields>
            <asp:BoundField DataField="ApplicantID" HeaderText="ApplicantID" ReadOnly="true"/>
            <asp:BoundField DataField="FirstName" HeaderText="First Name" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" />

            <asp:CommandField ButtonType="Button" ShowEditButton="true" EditText="Update" ShowCancelButton="false" />
        </Fields>
        </asp:DetailsView>

I've tried many ways to get this value, such as:

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

But it doesn't work... How to get this value using this mode. I know that i can use <ItemTemplate> instead of <BoundField>, but anyway it should be some way to get this value from this field. Help please! Thank you!

3 Answers 3

7

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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! That's exactly what I need! Work's like a charm!
Thanks so much.. You saved me! Not sure, why MS had to make this so difficult when they are giving Edit and Insert mode ! It should/could have been a lot simpler
1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.