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.
Related
I have a Gridview together with some LinkButton inside of an Update Panel. The click events are fired correctly, the way I expect them to work. BUT: After adding an additional Header Row to the grid in code behind, I need to rebind the Grid after each postback. Since then the click causes a postback, but the method is not executed.
<asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="GridViewFollowUpMove" runat="server" AutoGenerateColumns ="false" OnRowDataBound="FollowUp_RowDataBound" OnDataBound="FollowUpMove_OnDataBound">
<Columns>
<asp:TemplateField HeaderText="Due date" ItemStyle-HorizontalAlign="left">
<ItemTemplate>
<asp:LinkButton ID="LB_DueDate" runat="server" OnClick="ActivateDueDate" CssClass="NoDeco" CommandArgument='<%# Bind("ISSUEID") %>'>
<asp:Label ID="LabelDueDate" runat="server" Text='<%# Bind("DueDate","{0:dd.MM.yyyy}") %>' Width="60px" Class="line"/></asp:LinkButton>
<asp:TextBox ID="TXTBX_DueDate" runat="server" Text='<%# Bind("DueDate") %>' Font-Size="11px" visible="false" OnTextChanged="TextChanged_DueDate" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillFollowUp();
}
FillFollowUp();
}
I solved this problem by moving the commands that create and customize the additional Header row to the OnRowCreated Event. As I read somewhere: When a postback occurs, the Gridview must be recreated on the server, and in this process ItemCreated is fired for each item, but ItemDataBound is not (since the control properties are pulled from ViewState).
So building these headers in DataBound is the wrong choice!
I am using a simple gridview which is as following:
<asp:GridView ID="grdViewData" runat="server" Width="98%" DataKeyNames="RecordID" OnSelectedIndexChanged="grdViewData_SelectedIndexChanged"
onrowcommand="grdViewData_RowCommand">
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server"
CausesValidation="False" CommandName="Select" OnClientClick="window.scroll(0,0)"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server"
CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm('Do you want to delete this record?');" Text="Delete" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns> </asp:GridView>
For updating the records in gridview i use gridview_SelectedIndexChanged method where as for deleting records i use gridview_RowCommand (if e.CommandName == "Delete") method as follows:
protected void grdViewData_SelectedIndexChanged(object sender, EventArgs e)
{
// Code Working properly Controls on page also getting updated
}
protected void grdViewData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
// Code to Delete Record (Working Properly, Record being deleted.)
// Here Onwards code not working
Message.Text = "Data Deleted Successfully..";
ErrorMessage.Text = "";
}
}
In case of updating records, After updating I have some code to update controls on page which is working properly, but In case of deleting records, Records are being deleted but controls on page do not get updated. Attaching debugger shows that the code is executed perfectly but yet yet controls are not updated.
I have been googling for a day and really need to know the reason instead of going for alternative.
I have a gridview which contains images (populated dynamically from database) and dropdownlist which contains two values. First column contains checkbox. I want to insert selected checkbox's images and dropdown values to a new table on button click. What may be the suitable way?
Here is the grid view:
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" AllowPaging="true"
EmptyDataText="No images found" OnPageIndexChanging="gvDetails_PageIndexChanging" PageSize="5">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckUncheckAll"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID ="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="imgPreview" ImageUrl='<%#
"ImageHandler.ashx?imgID="+ Eval("ID") %>' runat="server"
Height="80px" Width="80px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dropdown" ItemStyle-Width="50px">
<ItemTemplate>
<asp:DropDownList ID="dpdListEstatus" runat="server" OnSelectedIndexChanged="dpdListEstatus_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
Loop with gridview rows
Find for the checkbox control
Check if its Checked property is true
If yes, call insert statement
Set the values you want to get from image and dropdownlist. Of course you need to use findcontrol on it too.
Dim cbSelect As CheckBox, imgToInsert As Image, ddlStatus As DropDownList
For Each r As GridViewRow In gvDetails.Rows
cbSelect = r.Cells(0).FindControl("CheckBox2")
If cbSelect.Checked Then
imgToInsert = r.Cells(1).FindControl("imgPreview")
ddlStatus = r.Cells(2).FindControl("dpdListEstatus")
'Insert statement goes here...
End If
Next r
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 a grid and I want to set a dropdown list with Yes & No for some columns. I am not able to put that dropdown list like I mentioned below. Its because I use checkbox in the grid to edit the rows. So even if I put these I am not able to see the drop down when I click the checkbox to edit.
<asp:TemplateField HeaderText="Lead" ItemStyle-Width="100">
<ItemTemplate>
<asp:Label ID="lblLead" runat="server" Text='<%# Bind("Lead") %>'></asp:Label>
<asp:TextBox ID="txtLead" runat="server" Text='<%# Bind("Lead") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="100px" />
<EditItemTemplate>
<asp:DropDownList id="ddlLead" runat="server">
<asp:ListItem Value="Yes"> Yes </asp:ListItem>
<asp:ListItem Value="No"> No </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
Try this
protected void grdVw_RowEditing(object sender, GridViewEditEventArgs e)
{
grdVw.EditIndex = e.NewEditIndex;
/* Insert specific editing logic here */
grdBind();//method to bind gridview
}
First you gotta make sure you are setting the mode to Edit. For this I use the default buttons, and you are using checkboxes, they should both work:
<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:CommandField ShowEditButton="True" />
<%--Other columns here--%>
</Columns>
</asp:GridView>
Then, you need to handle the RowEditing event, by setting the EditIndex to the index of the row you want to edit, and re-bind the GridView:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
setDataSource(); //this sets the data source of the grid, and re-binds it.
}