简体   繁体   English

如何从ASP.NET中的DetailsView控件获取值?

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

I have DetailsView on my page. 我的页面上有DetailsView。 I set DefaultMode="Edit" . 我设置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. 我知道我可以使用<ItemTemplate>而不是<BoundField> ,但无论如何它应该是从这个字段获取此值的某种方式。 Help please! 请帮助! Thank you! 谢谢!

Unfortunately, trying to access the Text of ..Cells[1] is not what you are looking for. 不幸的是,尝试访问..Cells[1]Text并不是你想要的。 You need the value of the TextBox control within that cell: 您需要该单元格中TextBox控件的值:

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, Bryuk,

Try OnItemUpdating event instead. 请尝试OnItemUpdating事件。 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. 将OnItemUpdating =“dvApplicantDetails_ItemUpdating”属性添加到aspx中的DetailsView控件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM