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" />
Related
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;
}
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
I have the following GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SysInvoiceID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="InvoiceID" HeaderText="SysInvoiceID" ReadOnly="True" SortExpression="SysInvoiceID" />
<asp:BoundField DataField="BillMonth" HeaderText="BillMonth" SortExpression="BillMonth" />
<asp:BoundField DataField="InvoiceDate" HeaderText="InvoiceDate" ReadOnly="True" SortExpression="InvoiceDate" />
<asp:BoundField DataField="InvoiceNumber" HeaderText="InvoiceNumber" SortExpression="InvoiceNumber" />
<asp:BoundField DataField="Net" HeaderText="Net" SortExpression="Net" />
<asp:BoundField DataField="VAT" HeaderText="VAT" SortExpression="VAT" />
<asp:BoundField DataField="Gross" HeaderText="Gross" SortExpression="Gross" />
<asp:ButtonField CommandName="ViewInvoice" HeaderText=" " ShowHeader="True" Text="View" />
</Columns>
</asp:GridView>
The very last column (ButtonField) is one I created myself just to include the text 'View' on each row, which when clicked, will bring up a PDF invoice.
I'm not sure if this is even possible, but I was wondering if it was possible to add some sort of validation for that column or something, so that if the 'InvoiceID' column is blank, the 'View' link on the corresponding row won't show up.
I felt close to doing this by going on split view in Visual Studio and then the 'Edit Columns' button on GridView tasks, but like I said I'm not sure if it's possible to do it this way and may have to resort to simply coding it.
Thanks for any help!
Use a <TemplateField> instead of a <ButtonField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="View"
Visible='<%# Eval("IsEmpty(InvoiceID)") %>' CommandName="ViewInvoice" />
</ItemTemplate>
</asp:TemplateField>
And add a method to your page that is IsEmpty(string id) or whatever type your id is, and just check to see if it's empty first.
You can also add a CommandArgument attribute to the Button that will let you specify what the argument to it will be.
Use a placeholder...
<asp:Placeholder runat="server" ID="plView" Visible="<%# Convert.ToBoolean(InvoiceID == null) ? false : true %>">
<asp:ButtonField CommandName="ViewInvoice" HeaderText=" " ShowHeader="True" Text="View" />
</asp:Placeholder>
I'm having trouble getting a gridview's button column to do anything. I'm using a DirectoryInfo object to get the details of a file. I put the filename and the date created into the gridview columns. The third column is a button column. I have set the datakeys(Name, CreationTime), named the button column's commandName to "sendcommand". I want to send the filename to another page. I have this code for the RowCommand event:
protected void gvFiles_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "sendcommand")
{
int index = Convert.ToInt32(e.CommandArgument);
string fileID = ((GridView)sender).DataKeys[index]["Name"].ToString();
Response.Redirect("irMain.aspx?#filename=" + fileID);
}
}
Nothing happens, except for a postback I think. How do I do this?
<asp:GridView ID="gvFiles" runat="server" Font-Name="Verdana" Font-Names="Verdana"
Width="401px" AutoGenerateColumns="False" BackColor="White"
BorderColor="Black" BorderStyle="Ridge" BorderWidth="2px"
DataKeyNames="Name,CreationTime"
>
<Columns>
<asp:HyperLinkField AccessibleHeaderText="File Name"
DataNavigateUrlFields="Name" DataNavigateUrlFormatString="~\Assets\reports\{0}"
DataTextField="Name" HeaderText="File Name" >
<HeaderStyle BackColor="#0033CC" ForeColor="White" />
</asp:HyperLinkField>
<asp:BoundField AccessibleHeaderText="Date" DataField="CreationTime"
DataFormatString="{0:d}" HeaderText="Date">
<HeaderStyle BackColor="Blue" ForeColor="White" />
</asp:BoundField>
<asp:ButtonField ButtonType="Button" Text="DO Stuff" CommandName="sendcommand"
HeaderText="WHAT?!" />
</Columns>
<AlternatingRowStyle BackColor="#6699FF" />
</asp:GridView>
You have to add the OnRowCommand attribute to your GridView in the ASPX, otherwise the GridView doesn't know what method to call when you execute a command on it.
AFAIK this is an entirely optional attribute and isn't generated via the designer so you have to add it manually when you want to use it.
I have a problem with gridview, I am using a gridview and listview on a page, when I select a row in the gridview I want to insert the data into the listview based on selection in the gridview. I am using a primary key in gridview.
PLease help me out
This is also called "Master-Detail".
You can find a great tutorial here.
In short, you need to make the row selectable.
<asp:GridView ID="ProductsGrid" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="AllProductsDataSource" EnableViewState="False">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ProductName"
HeaderText="Product" SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice"
DataFormatString="{0:c}" HeaderText="Unit Price"
HtmlEncode="False" SortExpression="UnitPrice" />
</Columns>
</asp:GridView>
This is done with <asp:CommandField ShowSelectButton="True" /> in the code above.
The next step is to add you DetailsView to be binded to the selected element of the master gridview. This can be done because the selection return the primary key of the element, from there you can load the detail.
This do a PostBack, if you want you can use Ajax and remove the PostBack flickering.
I believe the select on the gridview should fire off the event SelectedIndexChanged(). You would use that event to create the datasource for your listview and then bind the datasource to your listview.