I'm a newbie when it comes to asp.net, so grateful for any help.
I have a simple data bound drop down list, with a details view control. I can select a value from the list, hit update and the correct value gets written to the database. Problem is, the control the automatically "resets" to display the 1st value in the list. This would confuse the user and make them think they'd selected the 1st value prior to the update.
The only code-behind code in relation to this drop down list can be found in the ItemUpdating method of the details view control, as follows:
DropDownList ddlLoc = (DropDownList)dvYourProfile.FindControl("ddlLocation");
e.NewValues["PreferredLocation"] = ddlLoc.SelectedValue;
And here's the code form the aspx page
<asp:TemplateField HeaderText="Preferred Location"
SortExpression="PreferredLocation">
<ItemTemplate>
<asp:DropDownList Text='<%# Bind("PreferredLocation") %>' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:SqlDataSource ID="dsStaticDate" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT * FROM StaticData" />
You need to bind form the code behind. The out-of-the-box behavior of binding on the aspx page with the SqlDataSource does not allow you to stop the rebinding of the DropDownList after an event occurs.
Related
$("#ddl_subject").select2();
how to use javascript call ID in asp gridview ItemTemplate.
<asp:TemplateField>
<ItemStyle Width="24%" BackColor="#f8f8f8" />
<ItemTemplate>
<asp:DropDownList Style="width: 100%"
ID="ddl_subject" runat="server" EnableViewState="true" AutoPostBack="True" >
<asp:ListItem Value="">-- Please Select Value --</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
It's hard to say from the amount of code posted but a control within a gridview will not have the simple id of "ddl_subject", it will be prefixed with other things generated by asp .net.
Try doing the following selector instead:
$("[id$='ddl_subject']").select2();
This says to select any elements ending in "ddl_subject" which should find the drop down control on every row.
I'm doing this website for a school project, and when I run this website I have a GridView where I select one registry, then it opens the FormView with all the details about the selected registry
Image of the website working
Now, when I hit that Edit on the FormView it gives me the following error:
'ddl_SelectMae' has a invalid SelectedValue because it does not exist in the list of items. Parameter name: value
I know that, this error appears because the value on field "Mãe" is null, but I saw the database and that value can be null. (Same error applies to the "Pai" and "Comprador" field)
Here the part of the code where error happens, I can put all the code if needed
<asp:FormView ID="fv_Alteracoes" runat="server" DataKeyNames="idCao" DataSourceID="SqlDS_InsEdiDel">
<EditItemTemplate>
<table>
<tr>
<td>Mãe:</td>
<td>
<asp:DropDownList ID="dll_SelectMae" runat="server" DataSourceID="SqlDS_ListarMae" DataTextField="idCao" DataValueField="idCao"
SelectedValue='<%# Bind("Mae") %>'>
</asp:DropDownList>
<asp:SqlDataSource runat="server" ID="SqlDS_ListarMae" ConnectionString='<%$ ConnectionStrings:ConnectionString %>'
SelectCommand="SELECT [idCao], [Nome] FROM [tbl_Cao] WHERE ([Sexo] = 'F')">
</asp:SqlDataSource>
</td>
</table>
<asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateButton" CausesValidation="True" />
<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="UpdateCancelButton" CausesValidation="False" />
</EditItemTemplate>
If I do the same but with a registry with all the details filled the website works 100% as I want
Image of website working perfectly
Can someone help me how to stop that error and keep the DropDownList to edit the fields?
PS: Sorry about my English but I am Portuguese, thank you for help
Change Your Data Source so that null can be allowed as :
SELECT [idCao], [Nome] FROM [tbl_Cao] WHERE ([Sexo] = 'F')
union
Select null,'None'
I have a collection of records, each record has an ID and a description.
Now in my formview I have 8 textboxes and I want each text box to hold description
of each record.
So if I do this
Text='<%# Eval("Record[0].Description") %>' />
This gives an error, any other way to do it?
Also can I do it in the markup, or do I need to do it in code behind, under databound method for the formview?
Thanks..
FormView is not meant for showing List of Data.
If you have a List of Data, then you should use GridView or ListView.
Bind your FormView with a datasource having single record and then directly Eval the fields of the datasource.
i.e. do this:
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSourceId">
<ItemTemplate>
<asp:TextBox id="txtDescription"
Text='<%# Eval("Description") %>' />
<asp:TextBox id="txtName"
Text='<%# Eval("Name") %>' />
..
</ItemTemplate>
</asp:FormView>
so basically, your FormView should contain different DataField and it should be bound to a DataSource having just one Item.
You could use a repeater inside:
<asp:repeater ID="rep" runat="server" DataSource='<%# Eval("Record") &>'>
<ItemTemplate>
<asp:textbox id="txt" runat="server" Text='<%# Eval("Description") &>' />
</ItemTemplate>
</asp:repeater>
In the repeater you will bind to your outer datasource, inside the repeater your datacontext is the record
I have a basic ASP.NET Web Forms application.
I give the user the possibility to create the records by using a Web Form and to update them by using a DetailsView.
The web form stores the empty field on the TextBoxes as *empty strin*g in the DB correctly.
The DetailsView instead stores them as null fields in the DB.
Moreover in the DetailsView_ItemUpdated event if I check the e.newvalues and e.oldvalues arguments the corresponding fields are null as well (throwing an exception) that means the data are already sent as null to the server upon submit.
In the DetailsView I use templates and I set all Update parameters in the SQLDataSource as ConvertEmptyStringToNull="false".
SQLDataSource parameter:
<asp:Parameter Name="SHELF" Type="String" ConvertEmptyStringToNull="false"/>
DetailsView
<asp:DetailsView runat="server"
ID="GvProductDetail" AutoGenerateRows="False" DataKeyNames="rowid"
DataSourceID="ProductDetailData"
OnItemUpdated="gvProductDetail_ItemUpdated">
<asp:TemplateField>
<HeaderTemplate>Stock Shelf </HeaderTemplate>
<ItemTemplate>
<%# Eval("Shelf") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShelf" Text='<%# Bind("Shelf") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Is that possible that Bind converts the empty values to null? I thought it was enough to set the UpdateParameters rule not to convert empty string to null but apprently there is some other surprise. anybody might help?
I solved it by placing ConvertEmptyStringToNull="false" to every <asp:TemplateField> containing <asp:TextBox>:
<asp:TemplateField ConvertEmptyStringToNull="false">
<HeaderTemplate>Stock Shelf </HeaderTemplate>
<ItemTemplate> <%# Eval("Shelf") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShelf" Text='<%# Bind("Shelf") %>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
I am very new to ASP.NET.
I have a an ASP.NET page with an AJAX Combobox and a TextBox. The combobox populates from a database and has value of ID and displays Name. The textbox should display Address. All i want to do is change the value in the Address textbox when the index on the Name combo box changes.
And then be able to change the address and save it back to the database.
How do I do this (simple?) task?
Code so far...
<form id="form1" runat="server">
<div>
<asp:ComboBox ID="ComboBox1" runat="server" DataSourceID="AccessDataSource1" DataTextField="CompositeName"
DataValueField="Id" MaxLength="0" Style="display: inline;"
AutoCompleteMode="SuggestAppend"
onselectedindexchanged="ComboBox1_SelectedIndexChanged">
</asp:ComboBox>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/StudentDB.accdb"
SelectCommand="SELECT [Id], [Name], [Address] FROM [tblStudents]">
</asp:AccessDataSource>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
Try with the below markup. I've used DropDownList, it could be replaced with AJAX ComboBox.
The DetailsView could be further enhanced with CSS and ItemTemplate.
You could put more fields into the ItemTemplate like City, Country and so on.
<asp:AccessDataSource DataFile="App_Data/Students.accdb" ID="AccessDataSource1"
DataSourceMode='DataSet' SelectCommand='Select ID, [First Name], [Last Name] from Students'
runat='server' >
</asp:AccessDataSource>
<asp:AccessDataSource DataFile="App_Data/Students.accdb" ID="AccessDataSource2"
SelectCommandType="Text" SelectCommand='Select [ID], [Address] from [Students] where ID=#id'
runat='server'>
<SelectParameters>
<asp:ControlParameter ControlID='DropDownList1' Name='id'/>
</SelectParameters>
</asp:AccessDataSource>
<asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' DataSourceID='AccessDataSource1'
DataTextField="First Name" DataValueField='ID'>
</asp:DropDownList>
<asp:DetailsView ID='DetailsView' runat="server" DataSourceID='AccessDataSource2' DataKeyNames='ID'>
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID='AddressTextBox' runat='server' Text='<%# Bind("Address") %>'></asp:TextBox>
<asp:Button ID='AddressSaveButton' runat='server' Text='Save' UseSubmitBehavior='true' />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
In your ComboBox1_SelectedIndexChanged event. Set TextBox1.Text = CurrentAddress variable.
In reality, I am not a big fan of bindings commands directly in the asp.
I would create a Sub that is called LoadMyComboBox() which would fire in the Page_Load() event.
I would assume your going to have a button or some type of event that would be fired to perform your update? TextChanged would be overkill here. Adding an update button and then building your update command in the code behind would handle your update. Once that update is complete you can simply call LoadMyComboBox() again to refresh your combobox to refresh any necessary changes and do whatever you want with your textbox at this time as well.
u can do this by using a dropdownlist itself. In the DropDownList's SelectedIndexChanged event, bind the address to the TextBox control according the dropdownlist selection and pass this as a parameter and save it in database.