I am trying to add edit, delete button on my gridview using,
<asp:CommandField HeaderText="Edit/Delete" ShowEditButton="true" ShowDeleteButton="True" />
however it looks like this now,
I want Delete button on a new row, what can I do ?
like this
Edit
Delete
Try
first convert commandbuttons to template then
adding <br /> between edit button and delete button
UPDATE In Gridview Task then Select the commands that you want to convert to TemplateField in "Selected Fields:" then check your source code. You will see in the ItemTemplate
<asp:ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
<br />
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
Related
I have a Gridview set up and populated by binding my data. I have created a column that holds an "Edit" button. Clicking that button changes all of my fields to a text box populated with the data that is pulled from the database and replaces the "Edit" button with an "Update" and "Cancel" button. Up to this point all is working as intended. I change the value in the textboxes that I want to update and click the "Update" button. At this point everything is cleared out of all of the text boxes in each cell. I can enter the data again at this point and click the "Update" button a second time and any values that I have entered (the second time) will be passed back to my updating event procedure, but that is not quite the functionality I'm looking for.
I have run across a couple reports of the "Edit" button requiring 2 clicks to function, but the fixes didn't really apply to my situation.
This is my page_load
{
if (!this.IsPostBack)
{
Build_DDL();
Build_Turn_Checkbox_List();
Show_Data();
}
CheckBox_Selected_Values();
}
This is my gridview declaration:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" HeaderStyle-CssClass="headerClass" HeaderStyle-VerticalAlign="Bottom"
RowStyle-Wrap="true" HeaderStyle-Wrap="true" OnDataBound ="OnDataBound" AllowSorting="True" HeaderStyle-Height="50px"
OnSorting="GridView1_SelectedIndexChanged" CssClass="reportData" OnRowDataBound="GridView_OnRowDataBound"
OnRowCancelingEdit="GridView1_RowCancelEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<HeaderStyle VerticalAlign="Bottom" Wrap="True" Height="50px" />
<RowStyle Wrap="True" CssClass="oddRow" />
<AlternatingRowStyle CssClass="evenRow" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID ="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State" Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_state" runat="server" Text='<%#Eval("STATE") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_state" runat="server" Text='<%#Eval("STATE") %>'></asp:TextBox>
</EditItemTemplate>
There are more fields, but they are all built exactly the same.
Here is my update code:
{
//declare variables
string stateVal;
//set up textboxes
GridViewRow row = GridView1.Rows[e.RowIndex];
TextBox txt_State = (TextBox)row.FindControl("txt_state");
stateVal = txt_State.Text;
}
I didn't notice that when I clicked the "Edit" button on row 2, it was row 5 that was transitioning into text boxes. This was because I have the following code creating groupings in the gridview and each group creates a new row that is not accounted for in the row index scheme:
helper.RegisterGroup("State", true, true);
helper.RegisterGroup("Project Type", true, true);
helper.GroupHeader += new GroupEvent(Helper_GroupHeader);
helper.ApplyGroupSort();
GridView1.DataBind();
I will have to somehow account for the group headers and adjust the neweditindex accordingly.
I've added an Edit button to the Action column of my Gridview using a TemplateField. I want every column to have the edit button in the first column except for the last row, which should have an Add button there instead. How would I implement this separate last row with the different button? Here is the code I have.
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
</ItemTemplate>
</asp:TemplateField>
DataTable dt = new DataTable();
dt.Columns.Add("Action");
DataRow row1 = dt.NewRow();
...
dt.Rows.Add(row1);
gridRoles.DataSource = dt;
gridRoles.DataBind();
First you have to add 2 button 1st Edit(visible) and 2nd Add (make it display=none initially)
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
<asp:Button ID="Edit" runat="server" text="Edit" OnClientClick="AddClick()" />
</ItemTemplate>
</asp:TemplateField>
Then Find Gridview Last row and find the Button Controls
GridViewRow row = GridView1.Rows[GridView1.Rows.Count-1];
Button Edit = ((Button)row.FindControl("btnedit")).Text;
Button Add = ((Button)row.FindControl("btnadd")).Text;
Edit.visible =false;
Add.Visible =true;
Here reverse the button display option : False for Edit and true for Add
That item template column is going to add whatever it's contents are to every row. One way around this is to add and empty column, and add your button during the OnRowDataBound event.
You can check to see if you are on the last row, and if so, add the other button, rather than the edit button.
One way to do it is to use the footer as the last line of the GridView. It could look like this:
<asp:GridView ID="GridView1" runat="server" ShowFooter="true" ... >
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" text="Edit" OnClientClick="btnEditClick()" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" text="Add" OnClientClick="btnAddClick()" />
</FooterTemplate>
</asp:TemplateField>
....
</asp:GridView>
You can look at this article for a complete example, with input controls and validation: Inserting a New Record from the GridView's Footer.
In my gridView I've inserted the edit command name for every single row.
Now I need when the database value of myDate is not null stopping the edit in this row.
I tried this solution, but in GV see the icon stop.gif when the database value of myDate is not null, but if click in stop.gif the edit row is open, why?
Can you help me?
Thanks in advance.
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<center>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server"
ImageUrl='<%#(String.IsNullOrEmpty(Eval("myDate").ToString()) ? "/Images/edit.gif" : "/Images/stop.gif")%>'
ToolTip="Edit" /></center>
</ItemTemplate>
<EditItemTemplate>
<center>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="/Images/update.gif"
ToolTip="Update" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="/Images/cancel.gif"
ToolTip="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
If you just make the image url blank, it does not mean that you are not rendering the control. The button will still render. What you could do instead is like below
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server"
ImageUrl='/Images/stop.gif'
Visbile = '<%#Eval("myDate").HasValue %>'
ToolTip="Edit" />
In a gridview how can I do single radiobutton select without using JavaScript
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="GroupName" />
</ItemTemplate>
</asp:TemplateField
Give Different name to each radio button control and remove groupname attribute
e.g.
<asp:RadioButton ID="RadioButton2" runat="server" />
<asp:GridView ID="UserTable" runat="server" AllowPaging="False" SelectedIndex="0" DataKeyNames="UserID" ShowHeaderWhenEmpty="True"
OnRowDeleting="UserTable_RowDeleting" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Edit Controls" ItemStyle-Width="15%">
<ItemTemplate>
<%-- <asp:LinkButton ID="lnk_Edit" ToolTip="Edit User" CommandArgument='<%# Eval("UserID") %>' CommandName="Edit1" runat="server">--%>
<asp:ImageButton ID="img_Edit" src="Styles/Images/Edit.jpg" runat="server" style="border-style: none" CommandArgument='<%# Eval("UserID") %>'
CommandName="Edit1" />
<%--</asp:LinkButton>--%>
<asp:PopupControlExtender ID="PopUCtrlExt_Edit" runat="server" DynamicServicePath=""
Enabled="True" ExtenderControlID="" TargetControlID="img_Edit" PopupControlID="pnl_updateUser">
</asp:PopupControlExtender>
I wanted to get a pop up when I click the edit image inside my grid view. When I tried with a edit image inside a link the pop up was doing post back so I had to remove the link. Now How can i get the values of the selected row and use that value in the pop up panel which has
<asp:ListBox ID="listboxE_RoleName" runat="server" DataSourceID="SurelyKnown" DataTextField="RoleName" DataValueField="RoleName"></asp:ListBox>
<asp:TextBox ID="txtE_Email" runat="server" CssClass="style23"></asp:TextBox>
I want to edit the selected value. How to populate the values of selected row in the pop up panel in client side. If possible also a fix for the pop up postback when I use the image button inside the link. Thanks
Check these links
http://mattberseth.com/blog/2007/07/modalpopupextender_example_for.html
http://mattberseth.com/blog/2008/06/masterdetail_with_the_gridview_1.html
http://mattberseth.com/blog/2008/04/masterdetail_with_the_gridview.html
http://www.aspdotnet-suresh.com/2011/03/how-to-show-modalpopup-to-edit-gridview.html