Get cell text from DetailsView
I have a DetailsView control that is not attached to a datasource, but is populated by a storedprocedure. Also, I am not defining any fields in the markup. What I'm having a problem with is grabbing cell text during the ItemUpdating event. For example, I want to grab the text from cell 1 from each of five rows and store them to a variable, but the text of the v开发者_运维知识库ariable comes back null every time. Any help would be appreciated. Below is my markup and then the codebehind:
<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
}
I came across this in another project of mine; you want to get it from the DetailsViewUpdateEventArgs argument - this contains all the new values being submitted by the form
string task1 = e.NewValue[3]; // etc
you may want to cast the value as a string System.Convert.ToString(e.NewValue[3]);
Try not to use CStr()!
Hope this helps.
精彩评论