ListView Inserting Event - ListViewInsertEventArgs Values collection is empty - asp.net

Given the following InsertItemTemplate (simplified) I'm not getting anything back in the event object's Values collection.
<InsertItemTemplate>
Item:
<asp:DropDownList ID="ItemInsertType"
DataTextField="SearchItemName"
DataValueField="SearchItemID" runat="server" />
Hide Location:
<asp:TextBox ID="txtItemHideLocation" Text='<%# Bind("HideLocation") %>' runat="server"></asp:TextBox>
<asp:Button ID="btnSearchSearchItemInsert" CommandName="Insert" runat="server" Text="Add" />
<asp:Button ID="btnSearchSearchItemInsertCancel" CommandName="Cancel" runat="server" Text="Cancel" />
</InsertItemTemplate>
I'm binding the listview to a collection attached to my nHibernate model object (SearchObject.SearchItems). This collection doesn't have insert or update handling like an object datasource would, so I want to handle the insert/update events manually. Is there a way to get these values to come through automatically, or do I have to manually grab each value from its control when I handle this event?

I was using reflector on that assembly, and it looks to me (could be wrong) that it only supports IBindableControl controls for automatic extraction into the Values collection. So Values would not contain an entry for any non-IBindableControl-supporting controls. TextBox does not implement this, nor does DropDownList.
I typically use the InsertItem property and extract the control references directly using FindControl.
HTH.

Related

Where do I find the commandname handler for asp:linkbutton

Trying to debug a aspx script. Don't know any asp.net.
I've come across this piece of code:
<asp:LinkButton
ID="EditButton"
runat="server"
CausesValidation="False"
CommandName="Edit"
Text="Edit"
CssClass="LightBlue">
</asp:LinkButton>
From this site:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.command%28v=vs.110%29.aspx
I see that the method to look for is LinkButton_Command. I have done a search in all the files and not found this method.
This link is doing something, so it's not ignored. Could this method have a different name and if so how do I find what it is?
I have changed the Text attribute and that change is there when I run it, so I have the right piece of code.
I've also changed the CommandName attribute and the link stops working, so something somewhere is handling it.
There are two more linkbuttons in the code immediately after it:
<asp:LinkButton
ID="DeleteButton"
runat="server"
CausesValidation="False"
CommandName="Delete"
Text="Delete"
CssClass="LightBlue"
OnClick="DeleteButton_Click">
</asp:LinkButton>
<asp:LinkButton
ID="NewButton"
runat="server"
CausesValidation="False"
CommandName="New"
Text="New"
CssClass="LightBlue">
</asp:LinkButton>
I notice that the deletebutton has an onclick attribute, and I can find that method within the same script, but nothing obvious near that handler for the other two.
EDIT:
Another way of getting the answer that I want might be to ask the question:
What are all the different ways to add a click handler to a asp:linkbutton?
EDIT:
Don't know if it helps, but the link button is inside the following structure:
<ajax:UpdatePanel...>
<ContentTemplate>
<asp:FormView
id="FormView1"
runat="server"
OnItemDeleted="FormView1_ItemDeleted"
DataKeyNames="Id"
OnDataBound="FormView1_DataBound"
OnItemUpdated="FormView1_ItemUpdated"
OnItemUpdating="FormView1_ItemUpdating"
OnItemInserted="FormView1_ItemInserted"
OnItemInserting="FormView1_ItemInserting"
DefaultMode="Insert"
DataSourceID="odsLogEntryForm"
>
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="EditButton"...
The difference with the example from the site you posted is that they used the OnCommand="LinkButton_Command" to subscribe the handler to the event. In your example this is not the case. This can mean 2 things:
- The event is not handled
- The event is subscribed to in code, probably in the code behind file of the aspx/ascx file (.aspx.cs). Look for the following code:
EditButton.Command += <name of the function to handle the command event>;
EditButton is the name of your LinkButton.
Command is the name of the event to handle.
<name of the function to handle the command event> is a function which the programmer created. He/She could have named it anything.
After seeing your edit:
The event is handled by the encapsulating FormView. A FormView will handle command events of a inner buttons. Since the CommandName of the LinkButtons is set to "Delete" and "New" is will be handled by the corresponding events in the FormView.
see: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview_events(v=vs.110).aspx for the available events of a FormVIew in this case the events with the name "Item" apply.

Updating Database with value from datepicker control ASP.NET VB

Apologies in advance, for what may be a newbie question. (new to asp.net, coding in VB, zero knowledge of best controls). Please respect that I am not interested in AJAX controls, or using MVC and trying to minimise javascript. What I need to do is very simple in terms of technology.
I am developing a form that allows users to Edit database data. I have chosen to use FormView so that I can format it like a legacy vba app. I am able to set the data source to my database table and the values from the database successfully show up if I allow VWD to automatically format the control. I can edit everything in the database using this form as well.
However, the boss hates having to type dates.
I haven't been able to find a datepicker control with a datasource feature that works like the text boxes that are automatically built in formview (with a bind to a datasource feature). So, I am assuming none exist. I need some assurances that what I think I need to do is the right way.
Instead of using the FormView control's datasource via the front-end automagical stuff, I should instead
place one of these datapicker controls that simply combine calendar with a text box for all date fields in the formview (no disrespect to those who built them, just can't believe they are not more feature-rich, this seems so needed giving the number of datepickers available)
declare my data source in Page_Load and load all the controls if they have existing data utilising FindControl
use Data_Bound block to retrieve the selected values from each control utilising FindControl and build a dynamic SQL string for Update
declare and update my database using one of the code behind blocks, perhaps in the DataBound
Am I on the right track? I have no experience to be confident in my assumption.
and please, if there is an easier way, I'll take it, but I have to make it "pretty".
And suggestions for controls of any sort are welcomed.
---Further into my issue
Here is some code to prove I've actually tried to resolve this...
In my FormView code block I have:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="MASTERID_Action"
DataSourceID="srcAction">
<EditItemTemplate>
MASTERID_Action:
<asp:Label ID="MASTERID_ActionLabel1" runat="server"
Text='<%# Eval("MASTERID_Action") %>' />
<br />
Action_Description:
<asp:TextBox ID="Action_DescriptionTextBox" runat="server"
Text='<%# Bind("Action_Description") %>' />
<br />
Action_Target:
<asp:TextBox ID="Action_TargetTextBox" runat="server"
Text='<%# Bind("Action_Target") %>' />
<br />
Action_Captured:
<asp:TextBox ID="Action_CapturedTextBox" runat="server"
Text='<%# Bind("Action_Captured") %>' />
<br />
Action_Declined:
<asp:TextBox ID="Action_DeclinedTextBox" runat="server"
Text='<%# Bind("Action_Declined") %>' />
<br />
Action_AgreedDate:
<ewc:CalendarPopup ID="CalendarPopup1" runat="server" Culture="en-AU"
PostedDate="" SelectedDate='<%# Bind("Action_AgreedDate") %>'
SelectedValue="01/14/2013 08:47:54" UpperBoundDate="12/31/9999 23:59:59"
VisibleDate="01/14/2013 08:47:54" />
<br />
...
</EditItemTemplate>
My database holds this Action_AgreedDate as nullable.
When I view the ItemTemplate (in the browser) the date shows up as 0.000 (because its a text field and bound to Action_AgreedDate, no error occurs) and when I click Edit to go to the EditItemTemplate I get this error:
Conversion from type 'DBNull' to type 'Date' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Date' is not valid.
Source Error:
Line 95:
Line 96: Action_AgreedDate:
Line 97: '
Line 99: SelectedValue="01/14/2013 08:47:54" UpperBoundDate="12/31/9999 23:59:59"
I can easily translate this into "the control found a null field and doesn't know what to do with it"; problem is, I don't know what to do. I have checked the properties of the field (the CalendarPOP field to see if there is a setting for handling nulls and nothing is obvious to me. I'm currently trying to find further documentation on the control online. (I've contacted eWorld and hope they will be able to respond.)
I should also add that if I request a record that already has an Action_AgreedDate I get no errors because there is a value present in the database for the control to display.
I think the best way for you to do this would be to use jQuery and jQuery UI, which has a date picker, which can be attached to a regular ASP.NET TextBox in your FormView. So if you had FormView code that looks something like this for example:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1">
<EditItemTemplate>
id:
<asp:Label ID="idLabel1" runat="server" Text='<%# Eval("solution_id") %>' />
<br />
date_modified:
<asp:TextBox ID="date_modifiedTextBox" runat="server" Text='<%# Bind("date_modified") %>' />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("solution_id") %>' />
<br />
date_modified:
<asp:Label ID="date_modifiedLabel" runat="server" Text='<%# Bind("date_modified") %>' />
</ItemTemplate>
</asp:FormView>
You could run something like this using jQuery UI to get a datepicker on the date_modified text box:
$('[id$=date_modifiedTextBox]').datepicker()
The $('[id$=date_modifiedTextBox]') part is necessary to select the ASP.NET control using jQuery, if it were just a normal element you could just use $('.className') or $('#id') or something like that. Hope this helps.
Problem solved.
I ran across an article on 4GuysFromRolla.com about the RJS POP Calendar (http://preview.tinyurl.com/cerm9xv) and it ticked all the boxes. Successfully implemented it and, because the calendar is separate from the specified text box, it doesn't have to be bound ! It works very nicely. The 4GuysFromRolla save my bacon yet again...

Multiple databound controls in a single GridView column

I have a grid view that is data bound to a dataset. In a grid I have a column DefaultValue, which has three controls in it - a dropdownlist, a checkbox and a textbox. Depending on the data that is coming in it can switch to any of these controls. Everything is simple enough when we need just to display data - in gridview_prerender event I simply make one of the controls visible. The control is setup like following:
<asp:TemplateField HeaderText="Default Value" SortExpression="DefaultValue">
<ItemTemplate>
<asp:TextBox ID="txt_defaultValue_view" runat="server" Text='<%# Bind("DefaultValue") %>' Enabled ="false" />
<asp:DropDownList ID="ddl_defaultValue_view" runat="server" Enabled ="false" />
<asp:CheckBox ID="chk_defaultValue_view" runat="server" Enabled ="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_defaultValue_edit" runat="server" Text='<%# Bind("DefaultValue") %>'/>
<asp:DropDownList ID="ddl_defaultValue_edit" runat="server" />
<asp:CheckBox ID="chk_defaultValue_edit" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
My problem starts when I am in edit mode and I need to update the grid with new data. Since only the textbox control is databound, the RowUpdating event can only access data from the textbox column and all of my other data simply gets discarded. I also can't databind with checkbox and dropdownlist control, since they can get an invalid data type exception. So, does anyone know how can I update a column that has three different controls, where each of these three controls might have a valid data?
Thanks
First, i would switch visibility of the controls in RowDataBound of the Grid and not on PreRender, because it can only change on DataBinding and not on every postback.
You can there also bind the Dropdown to its datasource, change the checked-state of the Checkbox and set the Text-property of the Textbox according to the the Dataitem of the Row.
When you update you can find your controls with FindControl in RowUpdating and get their values(f.e. Dropdownlist.SelectedValue).
GridView has pair of events:
RowUpdating/RowUpdated
RowDeleting/RowDeleted
....
In your case your need RowUpdating - is called right before update is performed, So find in row expected control (saying checkbox) and make preparation before pass to database
As another way review possibility to use ObjectDataSource event Updating - it is usual way to specify parameters for query execution.

How to programmatically distinguish element/control types from ASP.NET Request.Form key/value collection?

I have a simple ASP.NET web form as below:
<form id="form1" runat="server">
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="X" Value="X"></asp:ListItem>
<asp:ListItem Text="Y" Value="Y"></asp:ListItem>
<asp:ListItem Text="Z" Value="Z"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btn" runat="server" Text="Button" />
</form>
Request.Form contains the following key/value pairs:
[0] _VIEWSTATE
[1] _EVENTVALIDATION
[2] txt
[3] ddl
[4] btn
How do I differentiate the button (btn) from Textbox value (txt) or DropDown List value (ddl)? Or do I need to somehow come up with a naming convention? I am trying to iterate Request.Form object and save form values into a hashtable for later use.
Thanks.
The way to differentiate between the various Request.Form fields is to associate the field names with the control names--which is exactly what each control does.
Each control knows its own ID. During the Initialization phase, each control sets or restores its state based on both Request.Form and ViewState.
For dynamically created controls, the Framework will handle this for you, provided that you create the controls and add them to the control tree before the Init phase (such as in the OnPreInit event handler).
If you want to do it yourself, you can mimic the process by walking the control tree.
You can't. To the server it's a simple name:value collection.
Why not let the framework take care of this for you?
In the codebehind you can retrieve the values via their properties:
ddl.SelectedText
txt.Text

Question: Regarding GridView boundfield ReadOnly property

I have a Gridview boundfield where i set ReadOnly to true because i don't want user to change its value. However on the objectdatasource control's update method that boundfield became null when i try to use it as parameter in update method. Is there a way to set that value during updating?
No, just add the field name you need to the DataKeyNames attribute of the GridView. Then the value will be sent to the Update command.
When you mark a field as read-only on the GridView it renders on the page as a span element, not an input. Therefore the value is not available on PostBack. If you can construct the update statement so that it doesn't expect this field, that would be the best way to deal with this. If the update statement is autogenerated and you can't get around having the value to update, then you can either read the value from the database before doing the update (so that you have it) or include a HiddenField bound to this column and use a literal that obtains the value via Eval instead of binding (if necessary). This will require using a template.
<asp:TemplateField>
<InsertItemTemplate>
<asp:TextBox runat="server" ID="itemTextBox" />
</InsertItemTemplate>
<EditItemTemplate>
<asp:HiddenField runat="server" ID="itemHF" Value='<% Bind("Item") %>' />
<asp:Label runat="server" ID="itemLabel" Text='<% Eval("Item") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="itemLabel" Text='<% Bind("Item") %>' />
</ItemTemplate>
</asp:TemplateField>
Another approach is to add a new query to your tableadapter. Create an update query that just uses the fields desired to be updated. When selecting the update method on the ODS pick the update query. The BoundFields that are not part of the update query can now be turned to readonly=true and it should work.
I had a similar problem and solved it in a slightly different way. But in my case the parameter I wanted to use in my Update method was my primary key and was available in a Query string. So in my DataSource definition I defined the UpdateParameters section to use instead of . Then I was able to remove the parameter completely from my table and it would revert to the Query string parameter.

Resources