Move the GridView Add button to right of delete button? - css

The attached screenshot shows two images.
Is it possible to make to Add button and delete button align side by side as depicted by the bottom image?
We are currently using GridView to create dynamic rows and it gets bit too long helped in part by the position of the Add button
<asp:gridview ID="grvCustomers" GridLines="None" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDeleting="grvCustomers_RowDeleting">
<Columns>
<asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:TextBox ID="custname" runat="server" style="width:250px;" class="form-control"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Adress">
<ItemTemplate>
<asp:TextBox ID="custaddress" runat="server" style="width:250px;" class="form-control"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="custamount" runat="server" style="width:250px;" class="form-control"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add More"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:gridview>

I second what #ahwm said.
But also, if the goal is to add the record to the data source, couldn't you simply add the commandfield showinsertbutton="true".

Related

focus a div on link button click

In my page, I have a gridview with link buttons in a column.
By clicking the link button should show a div below the gridview.
I gave href in onClientClick event of the link button as follows.
function showDiv() {
location.href = '#div1';
}
When I click the link button the div is showing, but after the page load the page goes up.
aspx code
<asp:GridView ID="gridDate" runat="server" CssClass="gridview_Order"
HeaderStyle-BackColor="#09182F" HeaderStyle-ForeColor="#ffffff"
AutoGenerateColumns="false" Visible="False">
<Columns>
<asp:TemplateField ItemStyle-Width="20%" HeaderText="Sl No." ItemStyle-CssClass="paddng">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="DATE" ItemStyle-CssClass="paddng">
<ItemTemplate>
<asp:LinkButton ID="lbtnDate" runat="server" CommandArgument='<%#Eval("tblName") %>' Text='<%# Eval("dtvar") %>' OnClientClick="showDiv()" OnCommand="lbtnDate_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="25%" HeaderText="" ItemStyle-CssClass="paddng">
<ItemTemplate>
<asp:LinkButton ID="lbtnDownload" runat="server" Text="Download Excel" CommandArgument='<%# Eval("tblName") %>' OnCommand="lbtnDownload_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><hr /><br />
<div id="div1">
<asp:GridView ID="gridOrderByUser" runat="server" CssClass="gridview"
HeaderStyle-BackColor="#09182F" HeaderStyle-ForeColor="#ffffff"
AutoGenerateColumns="false" Visible="False" >
<Columns>
<asp:TemplateField ItemStyle-Width="2%" HeaderText="Sl No.">
<ItemTemplate>
<asp:Label ID="lblSlNo1" runat="server" Text='<%#Container.DataItemIndex+1 %>' style="color:#000;"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="cod" HeaderText="CODE" InsertVisible="False" ReadOnly="True" SortExpression="cod" ItemStyle-Width="16%" />
<asp:TemplateField HeaderText="IMAGE" ItemStyle-Width="18%">
<ItemTemplate>
<asp:Image ID="img1" runat="server" Height="100px" Width="54px" ImageUrl='<%#"~/images/"+Eval("Image") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
So what should I do for staying the page in the div position.
Thanks
If you dont need the server side event of the link button you could do this:
OnClientClick="location.href = '#div1'; return false;"

FormView Layout

This has driven me crazy a few times in the past but I've always fudged it and moved on. Today I want to try and fix it!
I quite frequently create a GridView to display data and a FormView directly beneath in Insert mode with controls in the InsertItemTemplate corresponding to the columns in the GridView, so that it looks like the FormView is just an empty row of the GridView awaiting new data entry. This works great.
The problem is that I can never get the widths of the 'columns' (really just textboxes) in the FormView to correspond to the widths of the columns in the GridView above.
In the example below, as you can see, both the GridView and FormView have a width of 100% and, sure enough, when the page is rendered they're both exactly the same width (I gave the FormView a border briefly to check). However, even though the widths of the textboxes in the FormView are identical to the widths of the columns in the GridView, they don't display that way. The textboxes are slightly wider, and by the time you get to the right-most column the cumulative effect means that the alignment is way out.
I'm guessing the problem is something to do with border widths or some other hidden element which is being rendered, but I can't figure it out. I have to say, the amount by which the alignment is out seems more than the couple of pixels a border would affect things.
<asp:GridView ID="gvTPR" runat="server" DataSourceID="SQLTPR" AutoGenerateColumns="false" DataMember="DefaultView" DataKeyNames="TPRID" Width="100%" >
<RowStyle HorizontalAlign="Center" />
<EditRowStyle BackColor="#ccffff" />
<HeaderStyle BackColor="#013b82" ForeColor="White" />
<Columns>
<asp:BoundField DataField="TPREnteredAt" HeaderText="Entered At" ReadOnly="True" SortExpression="TPREnteredAt" ItemStyle-Width="24%" ControlStyle-Width="90%" />
<asp:BoundField DataField="TPRTemp" HeaderText="Temp" ReadOnly="True" SortExpression="TPRTemp" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRPulse" HeaderText="Pulse" ReadOnly="True" SortExpression="TPRPulse" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRRespiration" HeaderText="Respiration" ReadOnly="True" SortExpression="TPRRespiration" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRPCV" HeaderText="PCV" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRTP" HeaderText="TP" ItemStyle-Width="12%" ControlStyle-Width="90%" />
<asp:CommandField ButtonType="Button" InsertVisible="False" ShowEditButton="True" ItemStyle-Width="16%" UpdateText="Save" ControlStyle-Width="60px" />
</Columns>
<EmptyDataTemplate>
No TPR records exist
</EmptyDataTemplate>
</asp:GridView>
<asp:FormView ID="fvTPR" runat="server" DataSourceID="SQLTPR" DefaultMode="Insert" Width="100%" >
<InsertItemTemplate>
<asp:textbox ID="lblEnteredAt" runat="server" Text="Will be added automatically" Width="24%" />
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Bind("TPRTemp")%>' Width="12%" />
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Bind("TPRPulse")%>' Width="12%" />
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Bind("TPRRespiration")%>' Width="12%" />
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV")%>' Width="12%" />
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP")%>' Width="12%" />
<asp:Button ID="btnAddTPR" runat="server" Text="Save" Width="5%" />
</InsertItemTemplate>
</asp:FormView>
This renders like this:
How about using GridView footer for add funcionality? This way you will have all columns in same table and you'l get around your positioning problems.
Here is an example of how your gridview should look like:
<asp:GridView ID="gvTPR" runat="server" DataSourceID="SQLTPR" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="TPRID" Width="100%" EnableModelValidation="True">
<RowStyle HorizontalAlign="Center" />
<EditRowStyle BackColor="#ccffff" />
<HeaderStyle BackColor="#013b82" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Entered At" SortExpression="TPREnteredAt">
<EditItemTemplate>
<asp:TextBox ID="lblEnteredAt" runat="server" Text='<%# Eval("TPREnteredAt") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("TPREnteredAt") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="24%" />
<FooterTemplate>
<asp:TextBox ID="lblEnteredAt" runat="server" Text="Will be added automatically" Width="24%" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Temp" SortExpression="TPRTemp">
<EditItemTemplate>
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Eval("TPRTemp") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("TPRTemp") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Bind("TPRTemp")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Pulse" SortExpression="TPRPulse">
<EditItemTemplate>
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Eval("TPRPulse") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("TPRPulse") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Bind("TPRPulse")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Respiration" SortExpression="TPRRespiration">
<EditItemTemplate>
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Eval("TPRRespiration") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("TPRRespiration") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Bind("TPRRespiration")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="PCV">
<EditItemTemplate>
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("TPRPCV") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="TP">
<EditItemTemplate>
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("TPRTP") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="lnkbtnEdit" runat="Server" Text="Edit" CommandName="Edit"
CausesValidation="false"></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="lnkbtnUpdate" runat="Server" Text="Save" CommandName="Update"
CausesValidation="true"></asp:Button><br />
<asp:LinkButton ID="lnkbtnCancel" runat="Server" Text="Cancel" CommandName="Cancel"
CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkbtnInsert" runat="Server" Text="Save" CommandName="Insert"
CausesValidation="true"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No TPR records exist
</EmptyDataTemplate>
</asp:GridView>
Hope this helps!
Regards,
Uros

How to add footer to gridview

I have a grid with 3 columns - Edit, ID, Movie. I would like to add a footer with an Insert link button, 2 textboxes respectively, but unable to do so. Is it possible.
ASPX:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"
OnRowEditing="gridview1_RowEditing"
OnRowCancelingEdit="gridview1_RowCancelingEdit"
ShowFooter="true" >
<Columns>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:BoundField DataField="movie" HeaderText="MOVIE" />
</Columns>
</asp:GridView>
When I try the following, there is an error for commandfield which says, this element is not supported.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</columns>
The other way would be to use itemTemplate & EditTemplate for each column control. But I find this simple and would like to proceed this way. So Can I add a footer to this structure.
YES, It is possible. But this will require using the <FooterTemplate> inside <TemplateField>. Use TemplateFields for each of the columns and also set the FooterTemplate for each of the columns.
NOTE: The ID column seems here to be a Primary Key. So remove the <FooterTemplate> from the corresponding <TemplateField> defined for ID column, if ID is a Primary Key OR an autogenerated field in your database.
NOTE II: The <FooterTemplate> simply will contain a TextBox only.
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnCancel" runat="server"
CausesValidation="False"
CommandName="Cancel" Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkBtnInsert" runat="server"
CommandName="Insert">Insert</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnDelete" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<asp:TextBox ID="TextBoxID" runat="server" Text='<%# Bind("ID") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MOVIE">
<EditItemTemplate>
<asp:TextBox ID="TextBoxMovie" runat="server" Text='<%# Bind("Movie") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMovie" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Movie")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now there are 2 ways to insert Data. Either you can use GridView OnRowCommand event or you can handle the OnClick event of your Insert button.
You can't place a commandfield inside TemplateField. But can do like this:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"
OnRowEditing="gridview1_RowEditing"
OnRowCancelingEdit="gridview1_RowCancelingEdit"
ShowFooter="true" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<!--To fire the OnRowEditing event.-->
<asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
<!--To fire the OnRowDeleting event.-->
<asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete"
Text="Delete">
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:BoundField DataField="movie" HeaderText="MOVIE" />
</Columns>
</asp:GridView>

ASPX GridView header name?

How do we access the header above the edit and delete buttons? I would like to put some text there instead of it being empty.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="btnInsert" runat="server"
Text="Insert" CommandName="Insert" ForeColor="White" />
</HeaderTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
you can add text to header column to gridview column of edit button.
<asp:GridView ID="grdviewlst" runat="server" >
<Columns>
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Button ID="btnedit" CssClass="btnclassedit" runat="server" CommandName="EDIT"
Text="Edit" ToolTip="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Gridview row editing and deleting

I have a grid to update the employee's confirmation. I need to change the data from grid using edit and delete link. I have trouble to create this grid. Because gridview all textbox enable always.Textbox only enable when am click the edit button.
This is my partial code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
Width="100%" AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="None"
ShowHeaderWhenEmpty="True" EmptyDataText="No Data Found"
onpageindexchanging="GridView1_PageIndexChanging"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="EMP ID">
<ItemTemplate>
<asp:TextBox ID="lblempId" runat="server" Text='<%#Bind("fldemp_id") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLOYEE NAME">
<ItemTemplate>
<asp:TextBox ID="lblusername" runat="server" Text='<%#Bind("fldempname") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="JOINDATE">
<ItemTemplate>
<asp:TextBox ID="lbljoin" runat="server" Text='<%#Bind("fldjoindate") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CONFIRMATION DATE">
<ItemTemplate>
<asp:TextBox ID="lblconfirm" runat="server" Text='<%#Bind("fldconfirmdate") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATUS">
<ItemTemplate>
<asp:TextBox ID="lblStatus" runat="server" Text='<%#Bind("fldstatus") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CONFIRMATION STATUS">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("fldcon_status") %>' >
<asp:ListItem>Confirmed</asp:ListItem>
<asp:ListItem>Not-Confirmed</asp:ListItem>
<asp:ListItem>Extended</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/images/edit.png">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/edit.png" CommandName="edi"
CommandArgument='<%#Bind("fldemp_id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/images/Delete.png">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/Delete.png"
CommandName="del" CommandArgument='<%#Bind("fldemp_id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" HorizontalAlign="Left" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>
If you're asking how to disable editing in the grid until someone hits an Edit button, simply set Enabled property to False on the grid until your button is pressed.

Resources