What im trying to accomplish is to try to edit a gridview by using the updating event but not sure how to do this. The way im populating the datagrid is putting it into a dataSet and using that as the dataSource and doing a databind. Here is my datagrid below. Any help would be very helpful.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="AIRCRAFT" HeaderText="ProductID" />
<asp:BoundField DataField="ENGINE" HeaderText="Product" />
<asp:BoundField DataField="LEMAC" HeaderText="Price" />
<asp:CommandField ShowEditButton="true" />
</Columns>
<SelectedRowStyle BackColor="#99CCFF" />
</asp:GridView>
When the user clicks on the "Update" button, the page does a Postback and then changes the controls of the row in question, allowing the Update.
Besides, it enables 2 buttons: Update and Cancel.
Cancel, will leave the GridView as it was before the first click.
Update, will send the user modified' data to saving.
In order to allow update, you have to capture the RowUpdating event. Something like this:
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
' Do your Update here
End Sub
Related
I've set up an .aspx gridview that shows data from an SQL database based on the user selecting the parameter in a drop-down list.
What I'd like to achieve is that the gridview is empty before the user has made a selection. Is this possible?
There will always be data in the database, so the question is NOT about what to display when there are no rows to show.
Thanks in advance for any assistance!
//Eva-Lotta
ASP.Net 4.0 added the boolean ShowHeaderWhenEmpty property.
<asp:GridView runat="server" ID="GridView1" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</Columns>
</asp:GridView>
The headers will not appear unless DataBind() is called with something other than null.
GridView1.DataSource = New List(Of String)
GridView1.DataBind()
I've solved it in a really simple way! I set gridview visible to false on page load, and true when the user clicked a button. Sometimes it's much easier than expected.
The dropdownlist and the gridview are on the same page.
Code for dropdownlist :-
<asp:DropDownList OnSelectedIndexChanged="drplist_SelectedIndexChanged"
AutoPostBack="true" ViewStateMode="Disabled" AppendDataBoundItems="true"
ID="drplist" runat="server" DataSourceID="datasource1"
DataTextField="User_ID" DataValueField="User_ID">
<asp:ListItem Value="" Text="Make a selection"/>
</asp:DropDownList>
Code for gridview :-
<asp:GridView ID="gview" runat="server" Width="100%" AutoGenerateColumns="false" DataKeyNames="User_ID" DataSourceID="datasource">
<Columns>
<asp:BoundField DataField="User_ID" HeaderText="User ID" />
<asp:BoundField DataField="User_Name" HeaderText="User Name" />
<asp:BoundField DataField="User_Email" HeaderText="User e-mail" />
<asp:BoundField DataField="User_Mob" HeaderText="User mobile number" />
<asp:BoundField DataField="User_Created" HeaderText="Created Date" />
<asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
</Columns>
</asp:GridView>
The dropdown has User IDs from the database and when a selection is made from the dropdown it fires it selectedIndexChanged event , code for the same:-
protected void drplist_SelectedIndexChanged(object sender, EventArgs e)
{
var index = drplist.SelectedValue;
drplist.SelectedIndex = 0;
Response.Redirect("~/Demo/TableDemo.aspx?User_ID="+index);
}
Now when I click browser's back button , the dropdown still retains its selected value, which I do not want, I want it to reset to index 0. As I return to the dropdownlist from the TableDemo.aspx page and click on the edit button or delete button in gridview , the postback for the dropdown is triggered again taking me to the TableDemo.aspx page. Only a manual page refresh sets things straight. I tried to search and implement JS code for forcing page refresh on pressing browser back button but it didn't solve my issue and created more. I have tried using the update panel to my frustration and it happens to do nothing to solve my issue here. Sorry for the long post I couldn't do better in making the scenario shorter in writing here.
You probably have your page cached, thats the reason it doesn't behave the way you want when you click the back button. Try to add this code to your page_load on the page you don't want to be cahched.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Hope this helps!
I am trying to obtain the row index on a button click in an asp gridview.
<asp:Panel runat="server" Visible="true" ID="pnlGV">
<asp:GridView ID="gvTasks" runat="server" CssClass="table table-hover table-striped" DataKeyNames="TaskID" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" PageSize="10" SkinID="standard_gv" EmptyDataText="No tasks match your search terms">
<Columns>
<asp:BoundField SortExpression="TaskID" DataField="TaskID" ReadOnly="true" HeaderText="Task ID" />
<asp:BoundField SortExpression="ProjectCode" DataField="ProjectCode" ReadOnly="true" HeaderText="Project Code" />
<asp:BoundField SortExpression="TaskType" DataField="TaskType" ReadOnly="true" HeaderText="Task Type" />
<asp:BoundField SortExpression="TaskDescription" DataField="TaskDescription" ReadOnly="true" HeaderText="Description" />
<asp:BoundField SortExpression="BizArea" DataField="BizArea" ReadOnly="true" HeaderText="Business Area" />
<asp:BoundField SortExpression="TargetDate" DataField="TargetDate" ReadOnly="true" HeaderText="Target" />
<asp:BoundField SortExpression="TaskStatus" DataField="TaskStatus" ReadOnly="true" HeaderText="Status" />
<asp:BoundField SortExpression="TaskPriority" DataField="TaskPriority" ReadOnly="true" HeaderText="Priority" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnNotes" runat="server" CommandName="notes" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" Text="Notes" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
and
Protected Sub gvTasks_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
Try
If (e.CommandName = "notes") Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gvTasks.Rows(index)
notesAcc.Populate(CInt(gvTasks.Rows(index).Cells(0).Text))
notesAcc.Visible = True
End If
Catch ex As Exception
Throw
End Try
End Sub
This just throws this error message at me:
Invalid postback or callback argument. Event validation is enabled
using in configuration or <%#
Page EnableEventValidation="true" %> in a page. For security
purposes, this feature verifies that arguments to postback or callback
events originate from the server control that originally rendered
them. If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
and I have no idea how to fix it.
Would anyone be able to give me some insight?
Thank you in advance.
Tom
Edit:
Thank you for the responses, it is much appreciated.
What is notesAcc.Populate(CInt(gvTasks.Rows(index).Cells(0).Text)) dong? If you comment it out does the problem go away?
That is calling a Sub from an .ascx file, and I wouldn't imagine so as I've breakpointed the event handler and it doesn't even reach it.
Do you get that error when page loads or when you click the button?
When I click the button.
It is probably worth mentioning that I am no longer getting the error. I made a tiny insignificant change - so small I can't even remember what it was - and it's gone away, however, it's still not working as intended. The event handler doesn't trigger and I have no idea why. Again, would anyone be able to give me a nudge in the right direction?
You can do one of 3 things:
Set AutoEventWireUp to true.
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Manager.Default" %>
or
Add the event to the GridView
<asp:GridView ID="gvTasks" runat="server" CssClass="table table-hover table-striped" DataKeyNames="TaskID" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" PageSize="10" SkinID="standard_gv" EmptyDataText="No tasks match your search terms" OnRowCommand="gvTasks_RowCommand">
or
Add the handles keyword to the method
Protected Sub gvTasks_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvTasks.RowCommand
I hope it helps
i am using asp.net for my web application when i click on checkbox inside gridview and after that i check its value on button click it does not show me the exact value
here is asp code
<asp:GridView ID="dgvMenu" runat="server" Width="100%" CssClass="grid" GridLines="None"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
View
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="View" runat="server" Checked="<%#Bind('View') %>" />
</ItemTemplate>
<ItemStyle Width="50px" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Menu" HeaderText="Menu Name">
<HeaderStyle HorizontalAlign="Left" CssClass="firstcol" />
<ItemStyle CssClass="firstcol" />
</asp:BoundField>
</Columns>
</asp:GridView>
and here is its vb version to get its value on button click version
For Each item As GridViewRow In dgvMenu.Rows
Dim MenuName As String = item.Cells.Item(1).Text
Dim chkView As CheckBox = DirectCast(item.FindControl("View"), CheckBox)
Next
i want to check its value whether its checked or unchecked so that i can process its value
I assume that your binding your GridView even on postbacks. You should only bind it if not PageIsPostback.
Put this into Page_Load:
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' following function loads data and binds it to the GridView '
BindGrid()
End If
End Sub
If you DataBind the Grid on every postback, ViewState cannot be reloaded, hence all changed values are lost and events will not be triggered correctly.
I have a gridview with a button, and when the button is clicked, it fires a rowcommand procedure and adds a new row to the database. Everything works fine until I add a databound drop down list to the gridview.
With a databound dropdown list, the page loads fine, but when I click the button the error shows as "Internet Explorer cannot display the webpage". here is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"
DataSourceID="SqlDataSource1">
<Columns>
<asp:ButtonField CommandName="insertNew"
Text="Button" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CommandName="insertNew"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
Text="Add" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField></asp:TemplateField>
</Columns>
</asp:GridView>
And here is my code behind that runs when the button is pressed;
Protected Sub GridView1_RowCommand(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "insertNew") Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
MsgBox(index)
End If
End Sub
I just had to put this into the system.web web.config file
<httpRuntime maxRequestLength="32768" />
The problem is in the MsgBox line. MsgBox(index) is not supported in web applications.
Please remove MsgBox(index), and the issue will be fixed, because that feature is only supported in Windows Applications.