Change AutoGeneratedDelete LinkButton of Gridview to an Image Button - c#

i have a gridview with AutoGenerateDeleteButton Property set true.
Of course this property auto generates a linkbutton at the leftmost of the gridview, my question is, how can i change it to an Image Button?? i wanted my gridview to look more presentable by making the control buttons an image.
Thanks! :)

You can do by using template in grid...
for more info
http://p2p.wrox.com/asp-net-2-0-professional/46216-image-button-template-field-gridview-control.html

You can create a TemplateField and use autogeneratecolumns="false".
Here's an example of a GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Link" runat="server" Text="click" OnClick="link_Click"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="field1" HeaderText="My Column 1" />
<asp:BoundField DataField="field2" HeaderText="My Column 2" />
</Columns>
</asp:GridView>
Where field1 and field2 are headers from your DataTable
And to access the row within the event handler:
protected void link_Click(object sender, EventArgs e)
{
int rowindex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
//do something with rowindex etc
}

Add new Template Column and in add image button and set CommandName='Delete' it'll raise the delete event automatically.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgDelete" ImageUrl="~/imgs/Delete.png"
CommandName="Delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>

Try this
<Columns>
<asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/DeleteImage.png"
ShowDeleteButton="true"/>
</Columns>
And set AutoGenerateDeleteButton="false"
Or Refer this too

Related

Change position of CommandField "Update" and "Cancel" buttons?

The CommandField of my gridview looks like this:
<asp:CommandField ShowEditButton="true" ItemStyle-Width="70px" />
Clicking on "Edit" will show the "Update" and "Cancel" buttons like so:
Is there an easy way to change the position of those two buttons so that they look like this?
If you use TemplateField, and give the buttons a reserved CommandName (Edit, Update, Cancel, Delete) then you can give them any style and position in the GridView you want.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete">Update</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Set button attribute inside Gridview

I have a button inside a Gridview. What I want to do is when the button is clicked simply change the button color.
However, after publishing the paged and clicking the button nothing happens.
Am I missing a step.
Here is my .aspx code
<asp:GridView id="gvStatus" runat="server" AutoGenerateColumns="false" BorderWidth="1px" BackColor="White" CellPadding="3" CellSpacing="2" BorderStyle="Solid" BorderColor="Black" GridLines="Both" Pager="10" OnRowDataBound="setcolor" OnRowCommand="setsingle" >
<Columns>
<asp:TemplateField HeaderText="1" Visible="true" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Button id="btnDOne" Width="35px" Height ="25px" runat="server" Text='<%#(Eval("dateone", "{0:ddd}"))%>' CommandName="GetData" CommandArgument="<%#((GridViewRow) Container).RowIndex %>" ></asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is the c#
protected void setsingle(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GetData")
{
//Get row index
int rowindex = Convert.ToInt32(e.CommandArgument);
////Get Row
GridViewRow gvr = gvStatus.Rows[rowindex];
//Find the button
Button DayButton = gvr.FindControl("btnDOne") as Button;
//Set the color of the button
DayButton.BackColor = Color.Cyan;
}
To test if the code was working added a linkbutton to the Gridview and gave it the same CommandName:
<asp:TemplateField HeaderText="1" Visible="true" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Button id="btnDOne" Width="35px" Height ="25px" runat="server" Text='<%#(Eval("dateone", "{0:ddd}"))%>' CommandName="GetData" CommandArgument='<%# Container.DataItemIndex %>' ></asp:Button>
<asp:LinkButton id="lbd1" runat="server" Text="clickme " CommandName="GetData" CommandArgument='<%# Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>
So, the linkbutton executes the behind code no problem. But the commandbutton does not execute anything. In the last code snippet you will notice I compared the two and basically made the commandbutton match the linkbutton as far as the CommandArgument goes.
I have verified the behind code works, as well as the CommandName assignment is correct.
Is there a different way to do this for a commandbutton?
I ended up abandoning the command buttons and going with the link buttons then setting the back-color of the Grid cell as needed.

Selection of checkboxes lost during pagination of gridview

I have a problem in gridview, as per requirement i have set No of Records per page = 4 in gridview. I have to select Checkbox against every complaint but problem is then when i got to next pge in gridview and e.g fro 1 to 2 then when i come back to page 1 then it doesn't show TICK in check boxes . It doesn't remember my selection when i browse to and back to page.
<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="4" runat="server" AutoGenerateColumns="False" CssClass="mGrid" BorderColor="#333333" Width="550px" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />
<%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" /> --%>
<asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" />
<asp:BoundField HeaderText="Message" DataField="Message" >
<ItemStyle Wrap="True" />
</asp:BoundField>
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:CheckBox ID="ckboxIsComplaint" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
please check the above link.here your problem was clearly explained here.i think so it may be helpfull
As per comments...
If you do not update the underlying database by processing the OnCheckChanged event of the check box, then it will simply be reading the same data all the time.
From How to add event for Checkbox click in Asp.net Gridview Column, I have extracted the required information and tried to modify to fit your initial question.
<asp:TemplateField HeaderText="IsComplaint">
<ItemTemplate>
<asp:CheckBox ID="ckboxIsComplaint" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true/>
</ItemTemplate>
</asp:TemplateField>
add checkbox change event in aspx.cs page
protected void chk_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
<your data source>.Rows[row.DataItemIndex]["B"] = ((CheckBox)GridViewSmsComplaints.Rows[row.RowIndex].FindControl("ckboxIsComplaint")).Checked;
}

gridrow delete event causing infinite loop?

I'm trying to add a delete column to my gridview, however when I press the delete button on a row, it calls the function I assigned to onRowDeleting infinitely or so it seems.
Here's my code:
<asp:GridView ID="GridView1" runat="server" CssClass="yep" AutoGenerateColumns="False" OnRowDeleting="dismissClick">
<Columns>
<asp:TemplateField HeaderText="Create Incident">
<ItemTemplate>
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="case#" HeaderText="Case #" SortExpression="case#" />
<asp:BoundField DataField="Unit #" HeaderText="Unit #" SortExpression="Unit #" />
<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
<asp:BoundField DataField="typeOfCall" HeaderText="typeOfCall" SortExpression="typeOfCall" />
<asp:BoundField DataField="Primary Impression" HeaderText="Primary Impression" SortExpression="Primary Impression" />
<asp:TemplateField HeaderText="View PCR">
<ItemTemplate>
<asp:Button ID="ViewPCR" Text="View PCR" runat="server" OnClick="viewPCRClick" CssClass="btn em" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dismiss">
<ItemTemplate>
<asp:Button ID="dismiss" Text="Dismiss" runat="server" CommandName="Delete" CssClass="btn em" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my simple deletion function:
protected void dismissClick(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
GridView1.DeleteRow(index);
}
Calling GridView1.DeleteRow(index) is causing an event loop, because calling that method also raises the RowDeleted and RowDeleting events again, as described in the MSDN documentation for GridView.DeleteRow Method:
Calling this method also raises the RowDeleted and RowDeleting events.
Instead you want to either remove it from the binding source (i.e. DataSet, DataTable, List<T> or whatever you are binding to) or persist your delete to the database or whatever is ultimately holding your data and then re-bind the grid.

GridView Checkbox - Cannot Change Properties

I have an ASP GridView with a column that contains CheckBox controls. These CheckBoxes are bound to a bit field accessed via a SqlDataSource. The GridView is editable and the CheckBoxes are disabled on load.
I am trying to get the enabled property to change to true during edit, so the CheckBox in the row that is being edited can be changed, and this change updated to the bit field in the database.
The ASP code
<asp:UpdatePanel ID="upGridView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvChecklist" runat="server"
AutoGenerateColumns="false" DataSourceID="dsChecklist"
AutoGenerateEditButton="true" onrowupdating="gvChecklist_RowUpdating"
onrowediting="gvChecklist_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Finished">
<ItemTemplate><asp:CheckBox ID="cbFinished" runat="server" Enabled="false" Checked='<%# Eval("Finished") ?? false %>' /> </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Division"
HeaderText="Division"
readonly="true" />
<asp:BoundField DataField="Application"
HeaderText="Application"
readonly="true" />
<asp:BoundField DataField="Task"
HeaderText="Task"
readonly="true" />
<asp:BoundField DataField="TestedBy" HeaderText="Tested By" readonly="true"/>
<asp:BoundField DataField="Notes" HeaderText="Notes" ReadOnly="false"/>
<asp:BoundField DataField="JiraTicket"
HeaderText="JIRA Ticket"
readonly="false" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
C# Code Behind for the RowEditing event
protected void gvChecklist_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gvChecklist = (GridView)LoginView1.FindControl("gvChecklist");
CheckBox cb = (CheckBox)gvChecklist.Rows[e.NewEditIndex].Cells[1].FindControl("cbFinished");
cb.Enabled = true;
gvChecklist.DataBind();
}
-edit: I felt the need to add a little more information here. The code to set the CheckBox cb to the GridView Row's CheckBox seems to be working correctly, as I can see the enabled property = true through the debugger. I am wondering if it is getting set back to false due to PostBack after this event runs. The Load method for this page is empty, but I am thinking that the enabled=false code is being hit in the ASP page.
UI of GridView
The solution for this was surprisingly simple. Instead of putting a CheckBox in a TemplateField, I was able to set a DataField property for the CheckBox column. This allowed the edit functionality to work with no further C# code.
<asp:CheckBoxField DataField="Finished"
HeaderText="Finished"
readonly="false" />

Categories

Resources