C# - Retrieving Keys And NewValues Within A Formview With Manual Data Binding
Jul 25, 2010
I have a Formview binded in the code file to a generic list. Now, upon editing of a record, I wish to access the Keys and NewValues out of the FormViewUpdateEventArgs parameter of the ItemUpdating event handler method. From what I've tried and searched over the internet as of now, I've come to know that updated values are only available if the Formview is set a data source control on the markup page else they'd be null. Is this true?
Secondly, at this moment I am casting the sender object to formview and individually filling each object property by using FindControl method to find and retrieve values present in the controls. Is this the best way to do this task? As an example, this is what I am doing atm:
I have a FormView that I user for updating a record. There is a link button that when fires should perforom the updating via BLL and DAL. I am not using built-in ODS and I will not condsider using it. I have all my grids and formviews populated manuualy by calling methods that fetch the data from the database. For instance my details view is populated like this:
protected void DlMembers_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName.ToString() == "Select") { DlMembers.Visible = false; lblError.Text = string.Empty; lblError.Visible = false; fvMemberDetail.Visible = true; fvMemberDetail.ChangeMode(FormViewMode.Edit); MemberBLL getMemberInfo = new MemberBLL(); int Ident = Convert.ToInt32(e.CommandArgument.ToString()); fvMemberDetail.DataSource = getMemberInfo.GetMemberByIdent(Ident); fvMemberDetail.DataBind(); } if (e.CommandName.ToString() == "DeleteSelected") { DlMembers.Visible = true; lblError.Text = string.Empty; lblError.Visible = false; fvMemberDetail.Visible = false; fvMemberDetail.ChangeMode(FormViewMode.ReadOnly); } What I want to do if to capature my linkbutton on click event and do this (except that the runtime never reaches this method): protected void MemberInfoUpdating(object sender, EventArgs e) { TextBox id = (TextBox)fvMemberDetail.FindControl("txtIdent"); if (id.Text != string.Empty || id.Text != "") { TextBox txtFN = (TextBox)fvMemberDetail.FindControl("txtFN"); TextBox txtLN = (TextBox)fvMemberDetail.FindControl("txtLN"); DropDownList ddlAddress = (DropDownList)fvMemberDetail.FindControl("ddlAddress"); TextBox txtEmail = (TextBox)fvMemberDetail.FindControl("txtEmail"); TextBox txtHPhone = (TextBox)fvMemberDetail.FindControl("txtHPhone"); TextBox txtWPhone = (TextBox)fvMemberDetail.FindControl("txtWPhone"); TextBox txtMPhone = (TextBox)fvMemberDetail.FindControl("txtMPhone"); DropDownList ddlPos = (DropDownList)fvMemberDetail.FindControl("ddlPos"); DropDownList ddlIsAdmin = (DropDownList)fvMemberDetail.FindControl("ddlIsAdmin"); bool blIsAdmin = false; if (ddlIsAdmin.SelectedValue == "True") blIsAdmin = true; TextBox txtComments = (TextBox)fvMemberDetail.FindControl("txtComments"); MemberBLL updateMemberInfo = new MemberBLL(); bool UpdateOK = updateMemberInfo.UpdateMemberByIdent( txtFN.Text, txtLN.Text, ddlAddress.SelectedValue, txtEmail.Text, txtHPhone.Text, txtWPhone.Text, txtMPhone.Text, blIsAdmin, txtComments.Text, Convert.ToInt32(ddlPos.SelectedValue), Convert.ToInt32(id.Text)); } else { //Display error - no user id cannot update record } }
I am using FormView component which has a field that is the sex of the person that I am using RadioButtonList with a simple way to show Male and Female, but want to retrieve the information and OldValue NewValues not anything behind me.
In the FormView Edit Template: <! - SEX -> <tr> <td> <strong> Gender: </ strong> </ td>
I placed one FormView and one GridView conrtrols on the same page with one linqdatasource that is connected to both controls via their DataSourceID properties. While the GridView works without any problem (on updating) the FormView doesn't update the record.I traced the FormView events and found that in FormView1_ItemUpdating the argument e.NewVaues.Count = 0 as well as e.OldVaues. Count (also zero). Getting items from e.NewValues[index] or [key] causes an obvious exception. However, all the bound contolsn this wa
Sub PFTFormView_ItemUpdating(ByVal sender As Object, ByVal e As FormViewUpdateEventArgs) Dim PFTObj As New PFT Dim PFTDST As New Data.DataSet Dim newPFTLastName As String = DirectCast(PFTFormView.FindControl("txtPFTLastName"), TextBox).Text Dim newPFTFirstName As String = DirectCast(PFTFormView.FindControl("txtPFTFirstName"), TextBox).Text PFTObj.PFTLastName = newPFTLastName PFTObj.PFTFirstName = newPFTFirstName PFTObj.PFTHome = DirectCast(PFTFormView.FindControl("txtPFTHome"), TextBox).Text If PFTObj.UpdatePFT(Session("PFTId")) Then Me.lblErrorMessage.Text = "PFT Update Successfully" Else ' Me.lblErrorMessage.Text = "PFT Update UnSuccessfully" End If PFTDST = Nothing PFTObj = Nothing end sub
I have a GridView or DetailsView and its bound to a CslaDataSource.I noticed that the "Object_Select()" event is fired automatically with every time the page is loaded or refreshed.How I can perfrom mnaual biding between the DetailsView Control and CslaDataSource ?I want to control binding via code using the DetailsView.DataBind() method in certain cases only.
I have a very simple formview which opens fine with FirstName and LastName as the values in it. But when I click on the update button and inside the onitemupdating method the Keys count is always coming Zero. I have failed to understand why?
I 'm trying to use a gv with a modal formview to edit the row details
my first problem is: i can't get the Gv data keys there are actually two but i im trying to go one at a time. the data keys will be 1=rideid & 2=rideleg ... the combination of these two fields make all rows unique.
Secondly: after the row has been edited i need to change the data source so it can be saved in a second table.
these are the errors i'm getting with the code below
Error 65 The best overloaded method match for 'System.Web.UI.WebControls.ParameterCollection.Add(string, string)' has some invalid arguments
Error 66 Argument '2': cannot convert from 'object' to 'string'
I don't much care for the template rigmarole of the ASP.NET FormView web forms control. It seems the only way to access template defined controls is to 'capture' module level references to the wanted controls in the ItemCreated data binding event, for use in other tasks and event handlers. Ideally I would just like to be able to call set this.DataSource and call this.DataBind on a page, but only the latter is possible through inheritence, and doesn't achieve any of my normal data binding needs. What else is there besides the hairy, scary FormView control?
I have a FormView which will bind values from the database using a ID parameter from queryString. In my database, I have a field named CategoryList which can store multiple CategoryID (From Category table) in CSV format (eg: "1,3,4,5"). I'm using SQLDataSource as my datasource.
So, I want to show the selected category in CheckBoxList. Meaning that, the checkbox will be checked (on page load) if its ID is in the CategoryList. Also, I want to allow the user to check/uncheck the CheckBoxList and updates to the database in CSV format. How can i do that?
I have a sql-bound formview with three DDL's bound to individual SQL datasources. In edit mode, I want to set the selected value to the formview's value, and this works:
But it only works when there's a match between what's in the formview's DS and the DDL's DS.
Unfortunately, there may be instances where the the formview's DS may return a null value or a value different from the DDL's datasource...and when that happens I receive a compilation error like:
'ddl2' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
What do I need to do to trap that condition before attempting to set the selected value? Do I need to set the selected value of the DDL in formview's dataBinding or dataBound event, by looping through the DDL and checking for a match first?
im learning webforms and asp.net in general and what i have so far is a formview that displays data being pulled from a sqldatasource, but i also want a checkboxlist that can be used to input information and display information. this checkboxlist is insode the formview. the problem im having is that my formview is calling a datasource with an specific stored procedure with one table and my checkboxlist needs to called another stored procedure that has a different table. formviews does not allow you to have tow different datasources so how can i make this possible? ill paste the front end code and the backend code so you guys can see what im trying to do.
In an vwd2005 aspx page, I have a formview, dropdownlist and 2 sqldatasources. My markup is below
[Code]....
When the dropdownlist causes the postback, the formview does not display the data (it's there, I've double checked using query analyzer). Also, the OnIndexChanged only makes a button visible and has nothing to do with the data. What am I missing?
I select a item from gridView and trying to show/update/delete in From view. In Form View I am using the dropdownlist filling it in Code behind. Then i bound the list. But the problem is it is not showing the slected value. Here is the code
I have a label inside a formview that is not bound to anything.
Lets say I have a table of customers (with a key field called CustomerID), and then another table that has all their orders (with amount spent per order (OrderID) - based on CustomerID)
On the FormView there is a key field (CustomerID) (this is to be used as an INPUT parameter to a SP - which is to return the SUM of all the customers (Order_Totals). I could nest a view within a view I suppose to get this result, however, I just want a label to simply show this ScalarValue.
Basically, I want the label to show the aggregate (SUM) total amount each customer has spent on all orders. I have a SP called SumCost with two Fields (one a grouped by field CustomerID and the other a Summed Total of each customer's orders SumCost)
SELECT [SumCost] FROM [SumCost] WHERE ([CustomerID] = @CustomerID)
I test my SP and it works great, returning the value based on CustomerID - no problem.
My problem is getting the CustomerID passed into the SP on runtime (do I put it in FormView2_Databound) and putting the value into the label (CustTotalLbl) which is inside this form.
Using .NET 4 with a modal popup extender and an update panel, it appears that when doing an update the first time, everything is fine. NewValues contains the new value and OldValues is empty. When attempting a second update, NewValues now contains both the old value and the new value separated by a comma, while OldValues is still empty. It seems to me like OldValues should have the old value and NewValues should just have the new value. This only happens when the formview is within the update panel. When it is removed, it works fine (although OldValues is never populated).