How to bind value to hyperlink property? - c#

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'.

Related

Create Dynamic ItemTemplate based on GUID column names

I have a process that pivots data to create columns based on the UniqueIdentifier key value. The resulting data table has column names such as:
> PartNum
> [DB1A6498-7CC6-4EA0-846A-9B6EAB771777]
The portion of ASP is:
<asp:TemplateField HeaderText="Part No.">
<ItemTemplate>
<asp:Label ID="lblPartNum" runat="server" Text='<%#Eval("PartNum") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Out">
<ItemTemplate>
<asp:Label ID="lblRunOut" runat="server" Text='<%#Eval("DB1A6498-7CC6-4EA0-846A-9B6EAB771777") %>' />
</ItemTemplate>
</asp:TemplateField>
The Part Number displays just fine, however field based on the uniqueID results in a message:
DB1A6498-7CC6-4EA0-846A-9B6EAB771777/ is neither a DataColumn nor a DataRelation for table .
I read through the data table columns to create the grid columns, which display fine, however when attemtping to dynamically create the ItemTemplate, the error occurs. Putting the brackets "[]" around the uniqueID in the label did not work either.
Any suggestions are greatly appreciated. Thanks
Kevin...your response led me down the path of the brackets. Based on that, I found information about the DataBinder with the final result being that the Text property of the asp:label reads:
Text='<%# DataBinder.GetPropertyValue(Container.DataItem,"[DB1A6498-7CC6-4EA0-846A-9B6EAB771777]") %>'
This is displaying data from the uniqueID fields with no errors.

Accessing Header Text property of GridView TemplateField in the Code Behind when it is set from Header Template

I have a GridView in an asp .net application, where the Header Text of a template field is set in the Header Template of the field, as a label (where it will come from the resource file). Below is the code
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1">
<HeaderTemplate>
<asp:Label ID="lblNameHeader" Text="Name" runat="server" meta:resourcekey="lblNameHeaderResource1"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>'/>
</ItemTemplate>
</Columns>
</asp:GridView>
In code behind I am trying to access the HeaderText set on the column like this
var headerText = gridView.Columns[0].HeaderText;
But the value is coming Empty and I am not able to retrieve it from the gridView.Columns' HeaderTemplate property as well.
Please help me.
You have several problems with your code.
You are missing a closing </asp:TemplateField>. Your asp:Label is missing a runat="server" attribute.
If you want to use the .HeaderText property, this should be your markup:
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1" HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If you want to use a HeaderTemplate with custom markup in it, then you need to cast the column to a TemplateField, then access the controls within it.

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

TemplateField Not Updating In GridView using LINQDataSource

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").

Troubles retrieving data from within OnRowCommand from a GridViews

If I have this <asp:ButtonField runat="server" DataTextField="Name" CommandName="GetName"></asp:ButonField> Within a GridView. Is there any way to retrieve the DataTextField (Name) from the OnRowCommand method?
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource_Names"
DataKeyNames="ID,ModuleId" OnRowCommand="ChangeName">
Or alternatively, is there any way to make CommandName into an attribute where the command is dynamically entered based on given data, similar to the difference between DataTextField vs TextField.
I added <asp:BoundField DataField="Name" /> Then you can retrieve it using the same method here: GridView.onRowCommand Method
All I need to do now is find out a way to hide the field. If anyone knows how to, please let me know. Visible="false" gets rid of the field all together...
Not really sure if this is the best way, but it works:
Make your column a templatefield and bind the name value to a asp:hiddenfield control:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfName" runat="server" Value='<%# Eval("Name") %>'>
</asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>

Categories

Resources