I have a gridview with template button in it each row
each button fires an Onclick event.
i need to track the row of the pressed button.
if i want , lets say colorize the background , how should i do it?
<asp:GridView ID="OrdersGridView" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="DeleteOrder" runat="server" BackColor="Red" Font-Bold="True" ForeColor="White" Text="מחק" OnClick="DeleteOrder_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="OrderHost" HeaderText="OrderName" />
<asp:BoundField DataField="OrderId" Visible="False" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
The event is empty.
protected void DeleteOrder_Click(object sender, EventArgs e)
{
}
Based from your comment, you just want to get the index of your GridView.
protected void DeleteOrder_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
//Get the Row Index by calling row.RowIndex;
}
Hope it helps!
Related
this is my .cs code
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Gridview1.DeleteRow(e.RowIndex);
Gridview1.DataBind();
}
and this is my source code for
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="Black" GridLines="Horizontal" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" EnableSortingAndPagingCallbacks="True">
<Columns>
<asp:CommandField CancelText="İptal" DeleteText="Sil" EditText="Düzenle" InsertText="Yerleştir" SelectText="Seç" ShowEditButton="True" UpdateText="Güncelle" />
<asp:CommandField CancelText="İptal" DeleteText="Sil" EditText="Düzenle" InsertText="Yerleştir" SelectText="Seç" ShowDeleteButton="True" UpdateText="Güncelle" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
i have to delete row when i select rowdeleting but i dont know how to
Edit : i made .cs like this
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
SqlCommand cmd = new SqlCommand("Delete From adminler (admin_no,admin_kullanici_adi,admin_sifre,admin_email,admin_adi,admin_soyadi)");
GridView1.DataBind();
}
when i press delete button it deletes all of the datas and when i reload the page the datas coming back so its not saving them
bunu dene
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" /// Burası önemli tablodaki ıdenty id yi yaz
CellPadding="4" ForeColor="Black" GridLines="Horizontal" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" EnableSortingAndPagingCallbacks="True">
<Columns>
<asp:CommandField CancelText="İptal" DeleteText="Sil" EditText="Düzenle" InsertText="Yerleştir" SelectText="Seç" ShowEditButton="True" UpdateText="Güncelle" />
<asp:CommandField CancelText="İptal" DeleteText="Sil" EditText="Düzenle" InsertText="Yerleştir" SelectText="Seç" ShowDeleteButton="True" UpdateText="Güncelle" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
SqlCommand cmd = new SqlCommand("Delete From adminler where ID=#ID ");
cmd.AddWithValue("#ID",int.parse(GridView1.DataKeys[ID].Value))// yukarıda datakey girdiğin tablonun identy si yani
GridView1.DataBind();
}
I have a gridview that should split into pages but the issue is that when I change the page the whole gridview is disappearing I tried everything I found in the internet but with no solution here is my code
<asp:GridView ID="ExistContents" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
AllowPaging="true" PageSize="5" OnPageIndexChanging="ExistContents_PageIndexChanging" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="ContentID" HeaderText="id" />
<asp:ImageField DataImageUrlField="TmpFilename" HeaderText="Image">
<ControlStyle Height="64px" Width="96px" />
</asp:ImageField>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Type" HeaderText="Type" />
<asp:BoundField DataField="ContentID" HeaderText="id" Visible="false" ShowHeader="false" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:CheckBox runat="server" ID="ChBox1" OnCheckedChanged="ExistContents_CheckedChanged" AutoPostBack="True"/>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="header"/>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
and here is the event handler
protected void ExistContents_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ExistContents.PageIndex = e.NewPageIndex;
List<CONTENT> panier;
panier = (List<CONTENT>)Session["PANIER"];
ExistContents.DataSource = panier;
ExistContents.DataBind();
}
You should handle the PageIndexChanged event instead.
The pageIndexChanging is made to give you an option to cancel the paging before it's executed.
here is an answer for this probleme, acctually my datasource was not the session but a linq query so i changed it and it works perfectly
and here is my new code
protected void ExistContents_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow gr = (GridViewRow)chk.Parent.Parent.Parent.Parent;
int id= Convert.ToInt32(gr.Cells[0].Text);
if (chk.Checked)
AddToCaddy(id, "DELETE");
else
DeleteFromCaddy(id);
UpdatePanel.DataBind();
UpdatePanel.Update();
}
just for information if you are making a databind to your gridview in the Page_Load inside the if (!IsPostBack) then you will need to redefind the dataSource but if you'r not then you only need to use these functions
UpdatePanel.DataBind();
UpdatePanel.Update();
i hope this helps
i have a gridview as:
<asp:GridView ID="gdvOpinions" runat="server" Width="100%" CellPadding="4"
ForeColor="#333333" GridLines="None" Height="100px" Visible="False"
AutoGenerateColumns="False" onrowcommand="gdvOpinions_RowCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Confirm" Text="تائید" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="حذف" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="مقاله" HeaderText="مقاله" >
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="نظر کاربر" HeaderText="نظر کاربر" >
<ControlStyle Width="250px" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
As you see, i have two buttonfield for every row, but when i click one of these buttons and want to get their row in
gdvOpinions_RowCommand
the
gdvOpinions.SelectedRow
returns null;
how should i find which row is selected?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
OnRowCommand = "OnRowCommand">
<Columns>
<asp:ButtonField CommandName = "ButtonField" DataTextField = "CustomerID"
ButtonType = "Button"/>
</Columns>
</asp:GridView>
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
}
From what I understand out of your question, if you are trying to get the row for which the button was clicked, then in that case this might be the right thing to do :
GridViewRow gRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
Now here, this is for a control such as a Button or ImageButton. You might have to improvise on this for ButtonField.
You can also change the ButtonField to a simple TemplateField with a Button.
Hope this helps.
I am designing a search page where if the View/Edit link button is clicked, another page loads with more info on the subject, however; when the button is clicked, it does not throw the Gridview's RowCommand event. The Gridview is built dynamically based on search terms and comparing to a database through an SQLDataSource. Here is the html for the Gridview.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RID" DataSourceID="Report_System" CellPadding="4" ForeColor="#333333" GridLines="None" OnDataBound="GridView1_DataBound" Width="739px" OnRowCommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="RID" HeaderText="Report ID" InsertVisible="False" ReadOnly="True" SortExpression="RID" />
<asp:BoundField DataField="Rdate" HeaderText="Report Date" SortExpression="Rdate" />
<asp:BoundField DataField="Rtype" HeaderText="Type" SortExpression="Rtype" />
<asp:BoundField DataField="OID" HeaderText="Officer ID" SortExpression="OID" />
<asp:BoundField DataField="Ofirstname" HeaderText="Officer" SortExpression="Ofirstname" />
<asp:BoundField DataField="Pname" HeaderText="Student Reportee" SortExpression="Pname" />
<asp:ButtonField CommandName="View/Edit" Text="View/Edit" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
Currently, the RowCommand event is SUPPOSED to simply post back to a label for testing purposes.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//get row index
int row = Convert.ToInt32(e.CommandArgument);
errorLabel.Text = row.ToString();
}
I have made sure that ViewState is Enabled, the OnRowCommand is in the right place, and my PageLoad event is empty. I can't find any forum posts to help me beyond this. Any ideas?
Try adding a check on CommandName inside GridView1_RowCommand with an if statement, like this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "View/Edit")
{
//get row index
int row = Convert.ToInt32(e.CommandArgument);
errorLabel.Text = row.ToString();
}
}
I am trying to dynamically set width of gridview columns but when I apply the following code, I see no change in the browser.
.aspx code is:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" CellSpacing="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="File_Name" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_OnRowDataBound">
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="File">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnFileName" runat="server" CommandArgument='<%# Eval("File_Name") %>' CommandName="Download" Text='<%# Eval("File_Name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
.cs code is:
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//SET WIDTH OF THE COLUMNS
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Width = 100; //DATE
e.Row.Cells[1].Width = 100; //CUSTOMER
e.Row.Cells[2].Width = 100; //DESTINATION
}
}
Can anyone notice where I might be going wrong?
Please check .. I think you may need to do the following way
e.Row.Cells[0].Width = Unit.Pixel(500);
e.Row.Cells[1].Width = Unit.Pixel(500);