Display a button in gridview - asp.net

I have a grid view which display records by reading the data from a local database. I want to display a button called view with each record displayed. Simply meaning in each row there should be an asp.net button called view Details.
Here is my grid view code:
<asp:GridView ID="ItemView" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CssClass="list" DataKeyNames="ItemID"
DataSourceID="ADDataSource" GridLines="Both" Width="215px">
<Columns>
<asp:BoundField DataField="itemID" HeaderText="Item ID" />
<asp:BoundField DataField="Item Name" HeaderText="Item Name" />
<asp:BoundField DataField="Item Brand" HeaderText="Item Brand" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
I have added a button to delete records and edit records through common field property, but how do i add my own button called view to display in each row as the other 2 buttons.
Thank you.

I managed do it myself.
<asp:BoundField DataField="topic" HeaderText="Topic Name" />
<asp:BoundField DataField="Brand" HeaderText="Vehicle Brand" />
<asp:BoundField DataField="Model" HeaderText="Vehicle Model" />
<asp:BoundField DataField="Year" HeaderText="Vehicle Year" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="viewDetails" runat="server" Text="View" />
</ItemTemplate>
</asp:TemplateField>

Related

hiding a column in asp.net

I want to hide a column in a gridview. I use the following code;
dgvTekleme.Columns[1].Visible = false;
but this does not work. (may be it does not work because of using that column in the code)
Is there any solution to hide a column in code-behind
you can do this manually.....
goto asp:gridview tag and in gridview tag set autogeneratecolumn="false"
if you don't want to display some column just don't write that column....
for example if you don't want to display prodId column just erase that line....
and write manually for displaying data like!!!!!!!!!!
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="ProdID" DataField="prodid" ReadOnly="true" />
<asp:BoundField HeaderText="ProdName" DataField="ProdName" />
<asp:BoundField HeaderText="Quantity" DataField="quantity" />
<asp:BoundField HeaderText="SupplierID" DataField="SupplierId" />
<asp:BoundField HeaderText="StockLvl" DataField="stocklevel" />
<asp:BoundField HeaderText="MinStockLvl" DataField="minstocklevel" />
<asp:BoundField HeaderText="CostPrice" DataField="costprice" />
<asp:BoundField HeaderText="SalesPrice" DataField="saleprice" />
<asp:BoundField HeaderText="Loc" DataField="location" />
<asp:BoundField HeaderText="ProdCode" DataField="prodtypecode" />
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
or you can check this link:
How to hide columns in an ASP.NET GridView with auto-generated columns?

selectcountmethod in objectdatasource isn't called

I am facing a situation where the selectcountmethod isn't called.
The getdatamethod is called every time, but the count method is not. I also tried to remove the selectcountmethod and set enablepaging="false" and no data is displayed either, even if the dataset is not empty.
<asp:GridView ID="gvGradeDocent" runat="server" AutoGenerateColumns="False" AllowPaging="True"
DataSourceID="odsGradeDocent" DataKeyNames="Id" PagerStyle-HorizontalAlign="Right"
PagerSettings-Position="Top" CssClass="table">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" Visible="false" />
<asp:BoundField DataField="DecisionNo" HeaderText="DecisionNo" SortExpression="DecisionNo" />
<asp:BoundField DataField="DecisionDate" HeaderText="DecisionDate" SortExpression="DecisionDate"
DataFormatString="{0:dd/MM/yyyy}" />
<asp:CheckBoxField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:CommandField ShowSelectButton="True" ButtonType="Image" SelectImageUrl="~/Images/edit.png" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Image" DeleteImageUrl="~/Images/delete.png" />
</Columns>
</asp:GridView>
</div>
<asp:ObjectDataSource ID="odsGradeDocent" runat="server" SelectMethod="GetDocentGrades"
TypeName="mash.BusinessLogic.DocentGrade" EnablePaging="True" DeleteMethod="Delete"
SelectCountMethod="GetCountDocentGrades"></asp:ObjectDataSource>
Try working with the code sample here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.enablepaging.aspx
Check your query first to ensure that data is coming back. Also set the MaximumRowsParameterName and StartRowIndexParameterName parameters to the corresponding params in your select query.

hyperlink should come in multiline code not working

i use this code .. but when the hyperlinks are longer then field size the gridview expands verticaly. i want the link below as like a paragraph . give me a solution
<asp:gridview id="titlesGrid" runat="server"
datasourceid="titles"
width=90% cellpadding=5 font-size="8pt"
autogeneratecolumns=false
headerstyle-backcolor="maroon"
headerstyle-forecolor="khaki"
headerstyle-font-bold
rowstyle-verticalalign="top">
<columns>
<asp:hyperlinkfield headertext="Title"
datatextfield="title"
datanavigateurlformatstring="title_details.aspx?titleid={0}"
datanavigateurlfields="title_id" />
<asp:boundfield headertext="Title ID"
datafield="title_id" />
<asp:boundfield headertext="Category"
datafield="type" />
<asp:boundfield headertext="Pub ID"
datafield="pub_id" />
<asp:boundfield headertext="Price"
datafield="price"
htmlencode=false
dataformatstring="{0:n2}"
itemstyle-horizontalalign="right" />
</columns>
</asp:gridview>
Have you tried using <ItemStyle Wrap="True" /> for <asp:hyperlinkfield /> ?

keeping GridView checkbox state when posting back

I have a GridView which with a CheckBox in a TemplateField.
When I check something and hit the submit button, the items that I have checked are not check on the postback.
I tried using EnableViewState and that did not help.
How can this be fixed?
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="SELECT">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ICAO" HeaderText="ICAO" />
<asp:BoundField DataField="IATA" HeaderText="IATA" />
<asp:BoundField DataField="AIRPORT_NAME" HeaderText="AIRPORT NAME" />
<asp:BoundField DataField="CITY" HeaderText="CITY" />
<asp:BoundField DataField="COUNTRY" HeaderText="COUNTRY" />
<asp:BoundField DataField="REVISED_DATE" HeaderText="REVISED DATE" />
<asp:BoundField DataField="EMAIL_DATE" HeaderText="EMAIL DATE" />
</Columns>
</asp:GridView>
You need to override LoadViewState and SaveViewState methods to maintain the selection.
https://web.archive.org/web/20211020153240/https://www.4guysfromrolla.com/articles/110205-1.aspx#postadlink

"An entry with the same key already exists" appears when compiled under .NET 4

I am trying to upgrade an ASP.NET application to .NET 4, but one page on my site contains an exception:
Argument Exception: an entry with the same key already exists".
What is different about ASP.NET 4 that might cause this problem?
One Solution
Not sure why but setting clientIDMode="Predictable" rather than Static seems to have avoided this exception message.
I had the same issue and fixed it.
I went through my entire ASPX page and found ASP.NET control that had the same ID as another.
I also tested this fix, and found that any control that conflicts with another control on the page will cause this error.
<asp:Label ID="FailureText" runat="server" EnableViewState="False" ClientIDMode="Static" />
<asp:Label ID="FailureText" runat="server" EnableViewState="False" ClientIDMode="Static" />
It happens when you Copy/Paste elements on the same page.
This in your web.config may also cause the error by allowing duplicate IDs
<pages clientIDMode="Static">
I have this error too and not resolve with this
<pages clientIDMode="Static">
my datagrid work fine this :
I have gridview :
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" CancelText="لغو" DeleteText="حذف" EditText="ويرايش" UpdateText="بروزرساني">
<ControlStyle CssClass="btn btn-xs btn-default" />
<ItemStyle Width="143px" />
</asp:CommandField>
<asp:BoundField DataField="ID" HeaderText="#" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="عنوان" SortExpression="Title" />
<asp:BoundField DataField="ParentID" HeaderText="پدر" SortExpression="ParentID" />
<asp:BoundField DataField="Url" HeaderText="آدرس" SortExpression="Url">
<ItemStyle CssClass="ltr"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Icon" HeaderText="آيکون" SortExpression="Icon" />
<asp:BoundField DataField="Order" HeaderText="اولويت" SortExpression="Order" />
</Columns>
</asp:GridView>
but after add image field i see this error
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Button" CancelText="لغو" DeleteText="حذف" EditText="ويرايش" UpdateText="بروزرساني">
<ControlStyle CssClass="btn btn-xs btn-default" />
<ItemStyle Width="143px" />
</asp:CommandField>
<asp:ImageField ReadOnly="true" SortExpression="Icon" DataImageUrlField="icon">
</asp:ImageField>
<asp:BoundField DataField="ID" HeaderText="#" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="عنوان" SortExpression="Title" />
<asp:BoundField DataField="ParentID" HeaderText="پدر" SortExpression="ParentID" />
<asp:BoundField DataField="Url" HeaderText="آدرس" SortExpression="Url">
<ItemStyle CssClass="ltr"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Icon" HeaderText="آيکون" SortExpression="Icon" />
<asp:BoundField DataField="Order" HeaderText="اولويت" SortExpression="Order" />
</Columns>
</asp:GridView>

Resources