I have a asp DetailsView control that I auto generate the fields.
There is one column that has an ID that is mapped to another table (foreign key). It shows up in a textbox. I want that column to be displayed as a dropdownlist as indicated in my code example below. This works fine, but the other column still shows the textbox with the ID in it.
My question is:
is it possible to use the auto generate and still hide columns you don't need or want to modify?
I hate to have to write code for every single column just because one column needs to use a TemplateField.
DetailsView
<asp:DetailsView ID="DetailsView1" runat="server"
DefaultMode="Edit" DataSourceID="EntityDataSource1"
AutoGenerateEditButton="True" AutoGenerateInsertButton="True">
<Fields>
<asp:TemplateField HeaderText="Authorization">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" Runat="server" DataSourceID="EntityDataSource2" CssClass="DropDown"
DataTextField="Name" DataValueField="AuthenticationId" SelectedValue='<%# bind("AuthenticationId") %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp
</Fields>
</asp:DetailsView>
DetailsView DataSource:
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ContextTypeName="EntityNamespace.MyEntity" EnableFlattening="False"
EntitySetName="Routes" Include="Authentication" Where="it.RouteId = #RouteId">
<WhereParameters>
<asp:RouteParameter Type="Int32" RouteKey="RouteId" Name="RouteId" />
</WhereParameters>
</asp:EntityDataSource>
Dropdownlist DataSource
<asp:EntityDataSource ID="EntityDataSource2" runat="server"
ContextTypeName="EntityNamespace.MyEntity" EnableFlattening="False"
EntitySetName="Authentications">
</asp:EntityDataSource>
I think you can. Take a look at AutoGenerateRows. It says like this on msdn:
Explicitly declared row fields can be used in combination with
automatically generated row fields. When both are used, explicitly
declared row fields are rendered first, followed by the automatically
generated row fields.
But you also have to consider that the rows are not in the field collection
Automatically generated bound row fields are not added to the Fields
collection.
Reference here
Related
I have a gridview with a dropdownlist for the products description in the footer template.
There is no way to create a SelectedIndexChanged in the IDE and writing it out manually produces an error? How to create code to handle the Selection change? I need to populate the product ids when the product description is selected.
"EDITED"
I tried using the gridview rowediting event assuming that if a row item was changed (ie, a new selection in the dropdown, it would fire, but it doesn't) It appears a gridview event has to be fired when that dropdown list changes, that's where i need the code to go. Any ides on what event?
Here is what the template field markup is:
<asp:TemplateField HeaderText="description" SortExpression="description">
<FooterTemplate>
<asp:DropDownList ID="ddlProductDesc" runat="server" DataSourceID="edsProductDesc" DataTextField="description"
OnSelectedIndexChanged="ddlProductDesc_SelectedIndexChanged">
</asp:DropDownList>
<%--<asp:TextBox ID="tbInsertdescriptiton" Width="350" runat="server"></asp:TextBox>--%>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblProdDesc" runat="server" Text='<%# Bind("description")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Take a look at this:
http://www.c-sharpcorner.com/blogs/7228/asp-net-gridview-dropdown-with-related-records-in-textbox.aspx
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.
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.
I have a query expression which I am binding to a GridView in Page_Load. The data I want to capture in the SelectedIndexChaned event is in a BoundField defined thus:
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" Visible="False" />
If I set Visible="True", I have no trouble getting this data. Is there a way to hide the ID field and still get the data?
Depends on how you are trying to get the data. If this is an ID field that is unique for each row in the datasource, use DataKeyNames = "ID" in the GridView declaration. Then, in the code behind, whenever you need the ID, you can use the following line:
string ID = GridView1.Rows[GridRowIndex].DataKeys[0].Value.ToString();
You could also convert one of your BoundFields to a TemplateField, and place a HiddenField in it to store the ID. Like so:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="someOtherDataLabel" runat="server" />
<asp:HiddenField ID="IDHiddenField" runat=server />
</ItemTemplate>
</asp:TemplateField>
Then you could use FindControl() in the RowDataBound event of the GridView to store the ID value.