While creating a site I created a DetailsView to insert and update some item. While I created a view that allowed inserting and updating, changed a field item to a template view and added a dropdown to pull a list of options from another table within the database. This was all well and good but I ran into a situation where there were some dropdowns that I would not utilize while adding or inserting items. The current state would choose the first value of the dropdown which was not selected. This would insert the value into the database causing stored values that were not desired.
The solution was quite simple. Set a default selection to a mull value. To do this you need to set things up normally and insert two items inside the dropdown tag.
1: Insert AppendDataBoundItems="true" to the <dropdown...>tag. This allows you to append listitems to a datasourced list. </dropdown...>
2: Insert
<listitem text="[your default list item]" value="[value]">
For my problem it was simple I changed the list item to "--Select Item--" and set [value] to NULL. This would allow me to only insert values for items that I wished and insert a null value to unutilized selections.
You can use the code below to make the same changes to your site. As this will work for other situations I have only used this on a dropdown with a datasource generated list.
<asp:dropdownlist id="MyDropDownList" AppendDataBoundItems="true" runat="server"></dropdownlist>
<Listitem text="--Please select one--" value=""></listitem>
ASP.net, SQL, and vbscript coding I have been using to generate pages and maintain different sites. Also a good supply for fly fishing enthusiast for the NorthWestern States including Utah.
November 21, 2009
November 10, 2009
To display a drop-down list while editing or inserting
*** This can be used for editing or inserting. For inserting change the ItemTemplate section from EditItemTemplate to InsertItemTemplate. ***
From the Data node of the Toolbox, drag a SqlDataSource control onto the page.
Configure the datasource
Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, select Edit Columns.
In the Fields dialog box, select City from the Selected fields list box.
Click Convert this field into a TemplateField link.
Click OK to close the Fields dialog box.
Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, select Edit Templates.
Select EditItemTemplate in the Display drop-down list.
Right-click the default TextBox control in the template and select Delete to remove it.
From the Standard tab of the Toolbox, drag a DropDownList control onto the template.
Right-click the DropDownList control and select Show Smart Tag. In the DropDownList Tasks menu, select Choose Data Source.
Select SqlDataSource2.
Click OK.
In the DropDownList Tasks menu, choose Edit DataBindings. The SelectedValue property of the DropDownList control is selected in the DataBindings dialog box.
Click the Field Binding radio button and select City for Bound To.
Select the Two-way databinding check box.
Click OK.
Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, click End Template Editing.
Security Note
User input in an ASP.NET Web page can include potentially malicious client script. By default, ASP.NET Web pages validate user input to make sure input does not include script or HTML elements. As long as this validation is enabled, you do not need to explicitly check for script or HTML elements in user input. For more information, see Script Exploits Overview.* Information derived from the MSDN website: Walkthrough
Subscribe to:
Posts (Atom)