Gridview linkbutton not firing RowCommand - c#

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();
}
}

Related

Get GridView DataKeyName and use it in external button

I'm using ASP.NET Web Application (.NET Framework).
I have a GridView with auto-generated "Select" button, and an "Approve" button which is outside of the GridView. I want to click on the "Select" link to get the DataKeyName (ViewingRequestID) and use it in the "Approve" button's onClick method.
When I run my code, I click on "Select", it gives the correct ViewingRequestID. But when I click on "Approve" after that, the ViewingRequestID becomes 0.
How do I get the value to remain the same?
HTML
<asp:GridView ID="gvViewingRequests" runat="server" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ViewingRequestID" PageSize="5" OnRowCommand="gvViewingRequests_RowCommand" AutoGenerateSelectButton="True">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundField DataField="ViewingRequestID" HeaderText="Viewing Request ID" />
<asp:BoundField DataField="ParentID" HeaderText="Parent ID" />
<asp:BoundField DataField="StudentName" HeaderText="Student Name" />
<asp:BoundField DataField="StudentID" HeaderText="Student ID" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="DateCreated" HeaderText="Date Created" />
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Center" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
Code-behind
ViewingRequest viewingRequest = new ViewingRequest();
protected void gvViewingRequests_RowCommand(object sender, GridViewCommandEventArgs e)
{
int i = Convert.ToInt32(e.CommandArgument);
viewingRequest.viewingRequestID = Convert.ToInt32(gvViewingRequests.Rows[i].Cells[1].Text);
}
protected void btnApprove_Click(object sender, EventArgs e)
{
int errorCode = viewingRequest.changeStatus(viewingRequest.viewingRequestID, "A");
if (errorCode == 0)
{
displayViewingRequests();
lblMsg.Text = $"Viewing Request {viewingRequest.viewingRequestID} approved.";
}
}

How to track a specific Button from gridview

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!

gridview disappears after changing the page

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

Getting selectedrow in RowCommand

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.

Adding record to sql with gridview rowcommand buttonfield

I'm trying to add a Register button to every row in my course gridview so that when the user click on the register button on that particular row, it will register him to that particular course on that row by adding a course registration record into sql.
How should I write my code to insert that row's data into sql?
C#:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Register")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
}
ASP.NET:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowCommand="GridView1_RowCommand" Width="394px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="SpeakerName" HeaderText="Speaker" ReadOnly="True" />
<asp:BoundField DataField="startdate" HeaderText="Date" ReadOnly="True" />
<asp:BoundField DataField="Capacity" HeaderText="Capacity" />
<asp:TemplateField HeaderText="Button" ShowHeader="False">
<ItemTemplate>
<asp:Button ID="RegButton" runat="server" CausesValidation="false" CommandName="Register"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Register" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Select" SelectText="Details" ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
first you should add course Id (primary key) as datakeynames property to GridView1,and then can get the Id on GridView1_RowCommand like below :
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Register")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
int courseId = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
//your other codes
}
so,you have your course Id and by selecting the record you can add current record to sql.hope it helps.
Edit :
also you don't need this part CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" in your aspx page any more

Categories

Resources