I want to use a DetailsView to insert a record into an empty list.
I have a DetailsView in my page source (DefaultMode set to insert), and the datasource in set in the codebehind.
myObject = new CustomObject();
if (!IsPostBack)
{
myDetailsView.DataSource = new List
myDetailsView.DataBind();
}
...Then I run the website and type some text into one of the bound fields,
then click a button on the form...
protected void Button_Click(object sender, EventArgs e)
{
string s = myObject.myProperty;
}
But the value of the object is NULL.
Even myDetailsView.DataSource returns an empty list
what am I doing wrong??? I just want to retrieve the values types into the DetailsView
Thanks
James
JamesPosted Oct 27, 2009, 5:45 PM
not quite - myObject is a globally defined variable, and in the page_init I set it to a new customObject() - but only if not isPostback.
Then in the page_Load I bind the DetailsView to the datasource I created, by making a CustomObject list with myObject in as the first/only item.
I only bind it on not isPostback otherwise the viewstate binds the original list on postback and and newly inserted data is lost.
I guess I need to catch the data insert somehow, insert it into the list, and then rebind on every postback.
But I cant find how to get the data without making ItemTemplates so that each TextBox has an ID that I can access.
DetailsView.DataSource is null on postback and every property in DetailsView appears to be.
Kirtan PatelPosted Oct 27, 2009, 12:08 AM
jingePosted Oct 26, 2009, 9:03 PM
So when you want to get its value in the button click event handler, you get null value since myObject has gone out of its scope.
The right way shoulb be like the following in the button click event handler.
Let me know if it works.
JamesPosted Oct 26, 2009, 4:58 PM
Then I have created a new instance of this with no data filled in
User myNewUser = new User();
then I add this to a list as a detailsview needs a collection
List
The set that as the datasource and bind it.
What I want to do is fill in the fields on the dataview (like FirstName, etc)
and when the user clicks a button on the form, I want to get the values they have entered as part of a new object
so I can say myNewObject.Name and get the value they typed into the detailsview field.
However, in the ButtonClick event, I interrogate the object/detailsview datasource and they are null. So where is the data?
Kirtan PatelPosted Oct 26, 2009, 4:52 PM
i really does not understood what you excalty want to do
but as per i understood try to give you answer
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("TestItem1");
list.Add("TestItem2");
list.Add("TestItem3");
DetailsView1.DataSource = list;
DataBind();
}
it it helps you check "Do you like answer"
it if does not help you then explain a bit more in clear language :)
or just mail me your project (zip ) with more information in mail at [email protected]
i will help you out :)