TemplateField Not Updating In GridView using LINQDataSource - c#

Im using a LINQDataSource to populate a GridView of Universities.
Each University has an associated State, which is in another table and associated by a foreign key (StateID).
I have a TemplateField in the GridView so that when you view it normally it displays the StateName which comes from the State table and when you edit it shows a DDL populated with everything from the State table.
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="State" SortExpression="State">
<EditItemTemplate>
<asp:DropDownList ID="DropDownListStateEdit" runat="server"
DataSourceID="LinqDataSourceStates" DataTextField="StateName" DataValueField="StateID"
SelectedValue='<%#Eval("StateID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("State.StateName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
If I debug, inside of RowUpdating, GridViewUpdateEventArgs.NewValues doesnt even have a key for State.
Question: How do I let my gridview know I want it to update this column? All the BoundFields just seem to work...

In EditItemTemplate you should use #Bind("StateID") instead of #Eval("StateID").

Related

EditItemTemplate and InsertItemTemplate wont allow dropdownlist designer entry

I'm working on an aspx page where I'm trying to reference a dropdownlist from my code behind, now I add the dropdownlist to the page (ddlProgram) and its fine but soon as I move it into the InsertItemTemplate or EditItemTemplate or my GridView where I need it to go the designer entry disappears and I can no longer reference it from the code behind, is there a way around this? I have tried using the FindControl("ControlID") method but it always returns null. I need to set the selected index to 0 when the cancel button is click on my page. Thanks!
<asp:TemplateField HeaderText="Program" SortExpression="IDProgram">
<EditItemTemplate>
<asp:DropDownList ID="ddlProgram" AppendDataBoundItems="true" runat="server" DataSourceID="odsProgram" DataTextField="NameWithCode" DataValueField="IDProgram" CssClass="form-control ddl-normalize">
<asp:ListItem Text="No Program" Value="-1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="programName" runat="server" Text='<%# getProgramName((Eval("IDProgram")?? string.Empty).ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

CommandName = Insert in EditTemplate of ASP.NET ListView throws "Insert can only be called on an insert item"

I am supporting a web application. In that, there are two tables - TaxCode and TaxRate. TaxCode has 1 to many relationship with TaxRate.
The UI has a ListView with LayoutTemplate, ItemTemplate and EditTemplate to show TaxCode. When the users selects a tax code in EditTemplate it shows a CutomGridView that allows the user to create or edit tax rates for that particular tax code. This CustomGridView has 3 rows each has 4 template fields as shown below.
<asp:TemplateField HeaderStyle-CssClass="highlightTitlebar" HeaderStyle-Width="5%" HeaderStyle-Height="30px">
<HeaderTemplate>
<custom:CustomImageButton ID="imgAdd" runat="server" ImageUrl="image/add_round.gif" OnClick="imgAddTaxRateDetail_Click" CausesValidation="False"/>
</HeaderTemplate>
<ItemTemplate>
<custom:CustomImageButton ID="imgEdit" runat="server" ImageUrl="image/edit.gif" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="image/update.gif" CommandName="Update" />
<asp:HiddenField runat="server" ID="hfId" Value='<%# Bind("Id") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgInsert" runat="server" ImageUrl="image/insert.gif" CommandName="Insert" OnClick="imgInsert_Click" />
</FooterTemplate>
Each row in the CustomGridView is a template field. In, below image EffectiveOn section is CustomGridView,
When I try to save TaxRate with proper EffectiveOn and Rate, it throws "Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert." error as the ListView doesn't have InsertTemplate. But the record gets inserted into the DB.
Please let me know if there is any way to resolve this issue?
The error is self explanatory.
Take a look at this: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.insertitemtemplate(v=vs.110).aspx
So you can do either of these things.
Create an InsertItemplate and insert using the ItemInserted event of the listview
Change the CommandName to CommandName="InsertData" and catch that event on the ItemCommand

How to bind value to hyperlink property?

The next code is a part of a Grid View,i am trying to bind data to hyperlink control in the grid view.
The properties of the hyperlink is changing,it depends on the choose,so i need to make them dynamic and bind data to the when the user choose the option,this is the code:
<Columns>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:HyperLink ID="Download" runat="server" HeaderText='<%# Eval("choose") %>'
DataTextField='<%# Eval("choose") %>' DataNavigateUrlFields='<%# Eval("choose") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
"choose" is a string on server side that gets the name of my column name in the data base that i want to display.
I got this error:
'System.Data.DataRowView' does not contain a property with the name
'choose'.

Eval() in a DropDownList within a GridView

How can I do Eval() in a DropDownList that are inside a GridView?
I tried the follow code, but still wrong:
<asp:TemplateField HeaderText="Prioridade">
<ItemTemplate>
<asp:DropDownList ID="ddlPrioridade" runat="server"
SelectedValue='<%# Eval("Prioridade") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
Try this way
<asp:DropDownList ID="ddlPrioridade" runat="server" AppendDataBoundItems="true" DataSourceID="datasource" DataTextField="key" DataValueField="data" AutoPostBack="True" SelectedValue='<%# Eval("Prioridade") %>'>
also have a look at this DropDownList inside a GridView (or DataGrid). It might help you
You are receiving this error because the drop down list is not loaded with items and asp.net is trying to set the selected value of an empty drop down list and that's why it gives you error. So fill the drop down list first before setting it's selected value.

ASP.NET: How do I create radio buttons and databind them in a DetailsView?

I have a TemplateField in a DetailsView and its input should be one of a few choices in a lookup table. Currently it's a text field, but I want it to be a group of radio buttons, and it should work in both insert and edit mode (the correct current value should be selected in edit mode).
How do I create mutually exclusive radio buttons and databind them in a DetailsView TemplateField?
I'm on ASP.NET 3.5 using an Oracle database as a datasource.
<EditItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
DataSourceID="LookupSqlDataSource" DataTextField="LOOKUPITEM_DESCRIPTION"
DataValueField="LOOKUPITEM_ID" SelectedValue='<%# Bind("ITEM_ID")%>'>
</asp:RadioButtonList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
DataSourceID="LookupSqlDataSource" DataTextField="LOOKUPITEM_DESCRIPTION"
DataValueField="LOOKUPITEM_ID" SelectedValue='<%# Bind("ITEM_ID")%>'>
</asp:RadioButtonList>
</InsertItemTemplate>

Categories

Resources