从DetailsView获取单元格文本

时间:2011-04-16 13:42:58

标签: c# asp.net detailsview

我有一个未附加到数据源的DetailsView控件,但是由storedprocedure填充。另外,我没有在标记中定义任何字段。我遇到的问题是在ItemUpdating事件期间抓取单元格文本。例如,我想从五行中的每一行中获取单元格1中的文本并将它们存储到变量中,但每次变量的文本都返回null。任何帮助,将不胜感激。下面是我的标记,然后是代码隐藏:

 <asp:DetailsView ID="goalsDetailsView" runat="server" AllowPaging="True" 
                            CssClass="mGrid" DataKeyNames="GoalID,TaskSetID" Height="50px" 
                            onitemcreated="goalsDetailsView_ItemCreated" 
                            onmodechanging="goalsDetailsView_ModeChanging" 
                            OnPageIndexChanging="goalsDetailsView_PageIndexChanging" Width="100%" 
                            onitemdeleting="goalsDetailsView_ItemDeleting" 
                            onitemupdating="goalsDetailsView_ItemUpdating">
                            <Fields>
                                <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />                                             
                            </Fields>

                        </asp:DetailsView>



protected void goalsDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        //Accessing Edited values from the Details View            

        //grab the goalid and create a string guid
        string strgoalid = goalsDetailsView.Rows[0].Cells[1].Text; //GoalID
        Guid goalid = new Guid(strgoalid);

        //grab the tasksetid and create a string guid
        string strtasksetid = goalsDetailsView.Rows[1].Cells[1].Text; //TaskSetID
        Guid tasksetid = new Guid(strtasksetid);

        //grab goal text
        //string goal = ((TextBox)GoalsGridView.Rows[3].Cells[1].Controls[0]).Text; //Company
        string goal = goalsDetailsView.Rows[2].Cells[1].Text; 

        //grab task text            
        string Task1 = goalsDetailsView.Rows[3].Cells[1].Text; //Task 1
        string Task2 = goalsDetailsView.Rows[4].Cells[1].Text; //Task 2
        string Task3 = goalsDetailsView.Rows[5].Cells[1].Text; //Task 3
        string Task4 = goalsDetailsView.Rows[6].Cells[1].Text; //Task 4
        string Task5 = goalsDetailsView.Rows[7].Cells[1].Text; //Task 5



        UpdateOrAddNewRecord(goalid, tasksetid, Task1, Task2, Task3, Task4, Task5, goal, true); // call update method            
        BindDetailsView(); // Rebind GridView to reflect changes made
    }

1 个答案:

答案 0 :(得分:1)

我在我的另一个项目中遇到过这个问题;你想从DetailsViewUpdateEventArgs参数中获取它 - 它包含表单提交的所有新值

string task1 = e.NewValue[3]; // etc

您可能希望将值转换为字符串System.Convert.ToString(e.NewValue[3]);尽量不要使用CStr()!

希望这有帮助。