ASP.NET generate ID using Eval functinon - c#

For each row in GridView i have a HyperLink, and i need to generate different ID´s for each of them. Using Eval doesn´t work properly:
<asp:GridView
ID="ResultTableGvw"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="ResultTableGvw_RowDataBound">
<Columns>
<asp:BoundField ReadOnly="true" HeaderText="ID" DataField="id" />
<asp:BoundField ReadOnly="true" HeaderText="accountId" DataField="externalId" />
<asp:BoundField ReadOnly="true" HeaderText="tydeId" DataField="typeId" />
<asp:BoundField ReadOnly="true" HeaderText="Estado" DataField="statusId" />
<asp:BoundField ReadOnly="true" HeaderText="Timestamp do Pedido" DataField="timestampRequest" />
<asp:TemplateField HeaderText="Operação" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:HyperLink
ID='<%# DataBinder.Eval( Container.DataItem, "Id", "OperationManagementHl{0}") %>'
runat="server"
Text="">
</asp:HyperLink>
</ItemTemplate>
<ItemStyle Wrap="False"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
Any suggestion to solve this problem?
The error is : The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />

Try with this pattern and it works for me,
<asp:Label ID="lblNames" runat="server" Text='<%# Eval("Names") %>' Height ="60px" Width ="90%" CssClass="LabelDesign" />

Related

Telerik Grid disturbed with action column

Note: when I added an action column with edit delete image into a Telerik grid, the whole grid is disturbed as shown in screenshot.
This only occurs when I want to add action column.
Any help will be highly apprecieted.
Thank you
<telerik:radgrid id="RadGrid1" runat="server" pagesize="20" width="100%"
autogeneratecolumns="False">
<MasterTableView Name="MasterView1" DataKeyNames="Regid" AllowMultiColumnSorting="false">
<ItemStyle CssClass="GridRow1" />
<AlternatingItemStyle CssClass="GridRow2" />
<Columns>
<telerik:GridTemplateColumn HeaderText="Action" DataField="Regid" UniqueName="CkboxDelKey" HeaderStyle-CssClass="LeftGridHeader">
<ItemTemplate>
<nobr>
<asp:ImageButton ID="btnNoteEdit" ToolTip="Edit Goal" ImageUrl="images/EmailAlertEdit.png"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Regid") %>' CommandName="UpdateKeyResult"
runat="server" />
<ItemStyle Width="90px" />
<asp:ImageButton ID="btnDel" ToolTip="Delete Goal" ImageUrl="images/EmailAlertDelete.png"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Regid") %>' CommandName="DelNote" OnClientClick="javascript:return confirm('Are you sure you want to Delete?');"
runat="server" />
<ItemStyle Width="90px" />
</nobr>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" HeaderStyle-CssClass="GridHeader"
SortAscImageUrl="images/EmailAlertsGridAsc.png" SortDescImageUrl="images/EmailAlertGridDesc.png"
SortExpression="LastName" UniqueName="LastName">
<ItemStyle Width="90px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Firstname" HeaderText="Firstname" HeaderStyle-CssClass="GridHeader"
SortAscImageUrl="images/EmailAlertsGridAsc.png" SortDescImageUrl="images/EmailAlertGridDesc.png"
SortExpression="Firstname" UniqueName="Firstname">
<ItemStyle Width="90px" />
</telerik:GridBoundColumn>
<%-- <telerik:GridBoundColumn DataField="KeyResult" HeaderText="Key Result Score" HeaderStyle-CssClass="GridHeader"
SortAscImageUrl="images/EmailAlertsGridAsc.png" SortDescImageUrl="images/EmailAlertGridDesc.png"
SortExpression="KeyResult" UniqueName="KeyResult">
<ItemStyle Width="80px" />
</telerik:GridBoundColumn>--%>
</Columns>
</MasterTableView>
</telerik:radgrid>
What do you expect it to do? At it's heart, the RadGrid is just a table element with rows and columns. If you add one more cell to one of the rows, it will not look right. I suggest making a GridTemplateColumn for every row and deciding whether to show the buttons or not within this cell.

Cannot set specific column widths in bootstrap gridview

I have a gridview:
<asp:GridView ID="ParentSelect" runat="server" AutoGenerateColumns="false" OnRowCommand="ParentSelect_RowCommand" OnRowCreated="ParentSelect_RowCreated" emptydatatext="Please Submit A Clip. C'mon dude." ShowHeaderWhenEmpty="true" HorizontalAlign="Center" Width="600" CssClass= "table table-striped table-bordered table-condense" OnDataBound="ParentSelect_DataBound" DataKeyNames="SubmissionID" RowStyle-Wrap="true">
<HeaderStyle BorderColor="Black" />
<Columns>
<asp:BoundField DataField ="SubmissionID" />
<asp:TemplateField HeaderText ="Vote" ItemStyle-HorizontalAlign="center" HeaderStyle-CssClass="DisplayCenterHeaderGrid">
<ItemTemplate>
<asp:Imagebutton ID="VoteUp" runat="server" ImageUrl="Content/glyphicons-202-upload.png" Width="20" ToolTip="Vote Up" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
" CommandName="VoteUp" OnCommand="VoteUp_Command" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField ="Title" HeaderText="Title" Visible="True" HeaderStyle-Width="30" HeaderStyle-CssClass="DisplayCenterHeaderGrid"/>
<asp:BoundField DataField ="VideoUrl" HeaderText="Title" Visible="false" />
<asp:BoundField DataField ="Description" HeaderText="Title" Visible="false" />
<asp:BoundField DataField ="GenreSelection" HeaderText="Genre" Visible="True" HeaderStyle-Width="30" ItemStyle-HorizontalAlign="center" HeaderStyle-HorizontalAlign="Center" HeaderStyle-CssClass="DisplayCenterHeaderGrid" />
</Columns>
</asp:GridView>
I have tried virtually every way imaginable to set specific column widths and have been unsuccessful in doing so. Please help
Try to set ItemStyle-Width this property for your BoundField
<asp:BoundField DataField ="Title" HeaderText="Title" Visible="True"
HeaderStyle-Width="30" HeaderStyle-CssClass="DisplayCenterHeaderGrid"
ItemStyle-Width="300px"/>
In design view, click the GridView handle. Convert the BoundField to a TemplateField. Edit the template. Set the width property of the control in the ItemTemplate.
resulting sample code:
<asp:TemplateField HeaderText="Comments" SortExpression="comments">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("comments") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("comments") %>' Width="400px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

How to populate a GridView column with a button

I have the following GridView:
<asp:GridView ID="gvDispMsg" ClientIDMode="Static" ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" EmptyDataText="There is no data to display" runat="server" AllowPaging="false" AllowSorting="true">
<Columns>
<asp:BoundField HeaderText="Delete Message" HeaderStyle-Width="12%" />
<asp:BoundField HeaderText="ID" HeaderStyle-Width="12%" DataField="ID" />
<asp:BoundField HeaderText="Date" HeaderStyle-Width="13%" DataField="Created" />
<asp:BoundField HeaderText="Message" HeaderStyle-Width="45%" DataField="Message" />
<asp:BoundField HeaderText="Active" HeaderStyle-Width="10%" DataField="Active" />
<asp:BoundField HeaderText="Created By" HeaderStyle-Width="20%" DataField="CreatedBy" />
</Columns>
</asp:GridView>
It is populated from a SQL table which is executed from code-behind. The only column which is not populated is the Delete Message column.
How can I add a button for each row in the Delete Message column which takes the argument for the ID datafield?
Add
<asp:GridView AutoGenerateDeleteButton="true" DataKeyNames="ID" OnRowDeleted="NewEvent"
and handle the NewEvent in CodeBehind
or
add a <asp:ButtonField ButtonType="Button" CommandName="delete" Text="Delete" HeaderText="Delete"/> instead of a BoundField
or
add
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
</FooterTemplate>
</asp:TemplateField>
where you have most abilities to configure this column in every state

Delete query produces two different errors

I have a couple of gridviews in my website. Here's the code:
changeSlides.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="PlaceImages/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="PicPath" HeaderText="Filename" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
</Columns>
</asp:GridView>
EditBlogPost.aspx:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateEditButton="True"
DataSourceID="AccessDataSource1"
AutoGenerateColumns="False" DataKeyNames="ID"
AlternatingRowStyle-BackColor="Gray"
AlternatingRowStyle-CssClass="editGridFormat" RowStyle-CssClass="editGridFormat"
RowStyle-VerticalAlign="Top"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="Gray" CssClass="editGridFormat"></AlternatingRowStyle>
<Columns>
<asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="BlogTitle" HeaderText="BlogTitle"
SortExpression="BlogTitle" />
<asp:ImageField DataImageUrlField="Image" HeaderText="Image"
DataImageUrlFormatString="~/PlaceImages/{0}" ControlStyle-CssClass="editPhotoGridFormat"
AlternateText="Either a wrong file type or a misspelled file name has occurred"
NullDisplayText="No picture on file" >
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="Caption"
HeaderText="Caption for Picture on Homepage" />
<asp:TemplateField headertext="Text for the homepage">
<EditItemTemplate>
<asp:TextBox id="PicTextBox" runat="server" text='<%# Bind("PicText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("PicText")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TravelDate" HeaderText="Date of Travel"
SortExpression="TravelDate" DataFormatString="{0:MMMMMMMMM, dd, yyyy }" HtmlEncode="false" />
<asp:TemplateField headertext="Top of the Blog Post">
<EditItemTemplate>
<asp:TextBox id="BeginTextBox" runat="server" text='<%# Bind("BeginText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("BeginText")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:TemplateField headertext="Text at the End of the Post">
<EditItemTemplate>
<asp:TextBox id="EndTextBox" runat="server" text='<%# Bind("EndText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("EndText")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle VerticalAlign="Top" CssClass="editGridFormat"></RowStyle>
</asp:GridView>
EditPeoplePhotos.aspx:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
DataSourceID="AccessDataSource1" Width="493px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="photos/PeoplePhotos/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
</asp:ImageField>
</Columns>
</asp:GridView>
My update statements work, but my delete statements don't work in two of the three gridviews. My corresponding delete statements are all virtually the same: "DELETE FROM (Table Name) WHERE ID=?" but I get different error codes, which are as follows:
For changeSlides.aspx, it says "Entry with same key already exists"
For EditBlogPosts.aspx, it work fine. It deletes the blog.
For EditPeoplePhotos.aspx, it says "No value given for one or more required parameters"
Each of those pages use a separate, unrelated table in my database, and each query is only referring to one table, so there are no foreign keys used.
What is going on?

how to add a hyper link in a gridview

I have a gridview control and I would like the field Title to be a hyperlink and execute a stored procedure when clicked. Can anyone assist me in this?
Does this code look right?
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="hpTitle" runat="server" Text='<%# Bind("Title") %>' NavigateUrl='<%# Bind("SelectBook") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Publisher" HeaderText="Publisher" SortExpression="Publisher" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
</Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink id="hplink" runat="server" Text='<%# Bind("columnname")%>' NavigateUrl='<%# Bind("columnname")%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
On NavigateUrl, you can redirect to any page and on page_load you can call any stored procedure by using SQlCommand Object.

Categories

Resources