Forms Data Controls :: Dynamic GridView RowUpdating Event Handler Will Not Run?
Feb 1, 2010
I'm creating a GridView in code. I can successfully attach handlers for Editing, and CancelEditing. RowUpdating never runs, however. Instead, if the GridView is in a Panel, the Edit handler is called when Update is clicked (and this is the command name I get back on the Update click, too). If the GridView is not in a panel, then the Cancel handler is called when Update is clicked (again, this is the command name I get back from clicking Update). I've searched high and low for a reason why the RowUpdating event is not getting called, and I can't find one. Does anyone out there haveHere is my code (My actual code is much more involved than this, but for testing purposes, I extracted the following code and stuck it in a new project to isolate my problem):
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
I have a Modified Date and Modified By column in my gridview and I want them to be updated with the current date and current user any time a user updates the row.
I converted my columns to template fields and the textboxes in the edititemtemplate are bound to my sql data columns "ModifiedDate" and "ModifiedBy"
Then I added the attached code in my RowUpdating event.
When I test the code and click the Update link on my gridview I don't get an error but the values don't update either. I put a breakpoint on the code and when I hover over "ModifiedDateLabel.Text" I see the old value of the field so I know I'm accessing the control correctly.
I have page (inheriting from master page) in which I have a dynamic Grid view (columns are not defined at design time), but gird is binded from xml at run time.
From code behind, I am adding a LinkButton into a single column in Gridview as
[code]....
But when I click on the link button, it does a post back, but the Click Handler is not called? Also in the same page i have an asp:button, from javascript I am doing document.getElementByID('someid').click(), this also causes a postback but the button click handler on server is not invoked?
I'm using a GridView to display some columns and rows that reside in a database. This works excellent. Because i wanted to add columns dynamically out of a List of names. Let's say we have a list with 5 names in it, then it dynamically creates a column for every name in the GridView.
Here's some code to display what i do:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //Create columns for each student List<Student> allStudents = new Eetlijst.Business.Students().GetAll(); allStudents.Reverse();.......
How do i create the dynamic columns for each row? I create them with a DataBound event. So when the GridView databinds, the columns get filled with data. There is also a check inside to see if the row is in edit mode or not. So when i click on the edit button next to the row, the row goes perfectly in edit mode. The code i made adds a TextBox control to the cell.
When i fill in a value and press the Update button, it can't find any controls anymore and therefore i can't get the value i entered. The control is still there in the DataBound when i put a break point after it. But as soon as i click the Update button, all the controls of the cells are gone.
I searched on the internet and all i found is that it has something to do with the application firing a postback before it reaches the RowUpdating method.
So, how can i let the controls exist that i added to the cell when it is in edit mode?
I am using a gridview and want to update the row.. but i am unable to do it.... pls help me out where i am going wrong.... here is the code for the gridview nd rowupdating event.
It's breaking at spot below in RowUpdating Event with the error msg "Index was out of range. Must be non-negative and less than the size of the collection"
txtAddress = CType(GridView1.Rows(e.RowIndex).FindControl("txtAddress"), TextBox).Text Here's my code: ASP <form id="form1" runat="server"> <asp:GridView id="GridView1" runat="server" CssClass="Grid" DataKeyNames="ID" AllowPaging="false" OnRowEditing = "GridView1_RowEditing" OnRowDeleting = "GridView1_RowDelete" OnRowUpdating = "GridView1_RowUpdating" AutoGenerateColumns="False" EnableViewState="false" ShowFooter="true"> <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle> <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle> <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <%#Eval("ID")%> </ItemTemplate> <FooterTemplate> <asp:Button id="btnAdd" Runat="server" Text="Add New" CommandName="Insert"></asp:Button> </FooterTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="true" EditText="Edit" HeaderText="Edit" CausesValidation="false"/> <asp:CommandField ShowDeleteButton="true" EditText="Delete" HeaderText="Delete /> <asp:TemplateField HeaderText="mlsnum"> <EditItemTemplate> <asp:TextBox ID="txtMls" runat="server" Text='<%# Eval("mlsnum") %>'/> </EditItemTemplate> <ItemTemplate> <%#Eval("mlsnum")%> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtMLS" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="reqMLS" runat="server" ControlToValidate="txtMLS" Display="Dynamic" ErrorMessage="You must enter an MLS #."> * </asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <EditItemTemplate> <asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval("Address") %>'/> </EditItemTemplate> <ItemTemplate> <%#Eval("Address")%> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="reqAddress" runat="server" ControlToValidate="txtAddress" Display="Dynamic" ErrorMessage="Please enter an address."> * </asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Subdivision"> <EditItemTemplate> <asp:TextBox ID="txtSubdivision" runat="server" Text='<%# Eval("subdivision") %>'/> </EditItemTemplate> <ItemTemplate> <%#Eval("Subdivision")%> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtSubdivision" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="reqSubdivision" runat="server" ControlToValidate="txtSubdivision" Display="Dynamic" ErrorMessage="Please enter a Subdivision."> * </asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price"> <EditItemTemplate> <asp:TextBox ID="txtPrice" runat="server" Text='<%# Bind("Price") %>'/> </EditItemTemplate> <ItemTemplate> <%#Eval("Price")%> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="reqPrice" runat="server" ControlToValidate="txtPrice" Display="Dynamic" ErrorMessage="Please enter an Price."> * </asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Agent"> <ItemTemplate> <%#Eval("Agent")%> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtAgent" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="reqAgent" runat="server" ControlToValidate="txtAgent" Display="Dynamic" ErrorMessage="Please enter an Agent."> * </asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Comment"> <EditItemTemplate> <asp:TextBox ID="txtComment" runat="server" Text='<%# Eval("Comment") %>'/> </EditItemTemplate> <ItemTemplate> <%#Eval("Comment")%> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtComment" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="reqComment" runat="server" ControlToValidate="txtComment" Display="Dynamic" ErrorMessage="Please enter a Comment."> * </asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="PhotoAd"> <EditItemTemplate> <asp:checkbox ID="chkPhotoAd" runat="server" Checked=true/> </EditItemTemplate> <ItemTemplate> <asp:checkbox id="chkPhotoAd" runat="server" Enabled="true" /> </ItemTemplate> <FooterTemplate> <asp:CheckBox ID="chkAddPhotoAd" runat="server" Enabled="true" /> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="LineAd"> <EditItemTemplate> <asp:checkbox ID="chkLineAd" runat="server" Checked=true/> </EditItemTemplate> <ItemTemplate> <asp:checkbox id="chkLineAd" runat="server" Enabled="true" /> </ItemTemplate> <FooterTemplate> <asp:CheckBox ID="chkAddLineAd" runat="server" Enabled="true" /> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="# of Balloons"> <EditItemTemplate> <asp:TextBox ID="txtNumBalloons" runat="server" Text='<%# Eval("NumBalloons") %>'/> </EditItemTemplate> <ItemTemplate> <%# Eval("NumBalloons")%> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="intNumBalloons" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> VB Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Try BindData() Catch ex As Exception 'Error Handler lblError.Visible = True lblError.Text = Err.Description() 'lblError.Text = Err.Number End Try End If End Sub Sub BindData() Dim ds As New DataSet Dim objDS = New DataSet Dim cmd As SqlCommand Dim dataAdapter As New SqlDataAdapter strSqlConnection = ConfigurationManager.AppSettings("ConnectionString") sqlConn = New SqlConnection(strSqlConnection) strSqlStatement = "select * from tblAdSignup" cmd = New SqlCommand(strSqlStatement, sqlConn) dataAdapter.SelectCommand = cmd dataAdapter.Fill(ds) objDS.DataSetName = "ds" GridView1.DataSource = ds GridView1.DataBind() End Sub Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) GridView1.ShowFooter = False GridView1.EditIndex = e.NewEditIndex BindData() End Sub Protected Sub GridView1_RowCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) GridView1.ShowFooter = True GridView1.EditIndex = -1 BindData() End Sub Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) 'Try 'BindData() 'Dim eRow As Int16 = e.RowIndex 'Dim id2 As Integer ID = GridView1.DataKeys(e.RowIndex).Value 'ID2 = GridView1.DataKeys(1).Value Dim txtAddress, txtAddress2 As String 'Dim test As Integer 'test = GridView1.Rows(0).Cells.Count 'Dim strAddress As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtAddress"), TextBox).Text 'Dim txtAddress As TextBox 'txtAddress = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtAddress"), TextBox) txtAddress = CType(GridView1.Rows(e.RowIndex).FindControl("txtAddress"), TextBox).Text 'txtAddress2 = GridView1.Rows(0).Cells(0).Text 'txtAddress2 = CType(GridView1.SelectedRow.FindControl("txtAddress"), TextBox).Text ' 'Catch ex As Exception ' 'Error Handler ' lblError.Visible = True ' lblError.Text = Err.Description() ' 'lblError.Text = Err.Number 'End Try End Sub
This event handler is not responding at all to the click event. On click, the pagination numbers disappear and I'm not able to trace it in the debugger because the event handle doesn't even fire so I have no idea what's going on.
I want to add an event handeler that tell me wich row had been selected and highlighted it too. I tried many times, but usually face this error "No overload for 'Grd_ServersInfo_CheckSelectedRow' matches delegate 'System.EventHandler' ". Could any one paste the right code lines to handel this event
I get an error when attempting to navigate to the page containing this gridview. The error is 'SelectPackage_Click' is not a member of 'ASP.offer_aspx'.
here my page directive:
[Code]....
Here is my markup. Eventhough SelectPackage_Click is present in the code behind, it is not wired up with the button control.
I am trying to create a custom GridView with a header toolbar at the top that will contain icons to export the grid view to different formats like excel, word, etc.. The problem is, I cannot get the method to fire that is assigned to handle the click event for the corresponding Image Button. The page posts back, but the code I have in the method to handle the click event does not fire. I have followed examples in other posts and from what I can tell I have followed what others have done. Can somebody take a look at see if they can find something that I am doing wrong.
How do I get the old values in a gridview row in the row_updating event? I'm using a sqldatasource control to populate the grid and want to write the update code in the RowUpdating event. There are 10 fields displayed in the grid. I can get the value of the first field using string old_Category = e.OldValues[0].ToString();.
But it doesn't work for index items greater than 0. This doesn't work string old_Category = e.OldValues[1].ToString(); it returns an error message Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index. even though there are 10 columns in the grid.
I've put a simple gridview on my Asp.net webpage. I've configured the fields via C# code:
gvMainDataGrid.AutoGenerateColumns = false; gvMainDataGrid.AutoGenerateDeleteButton = false; BoundField bfldSeqNmbr = new BoundField(); bfldSeqNmbr.DataField = "PKSequenceNmbr"; bfldSeqNmbr.Visible = false; gvMainDataGrid.Columns.Add(bfldSeqNmbr); BoundField bfldEmployeeCode = new BoundField(); bfldEmployeeCode.DataField = "Employee Code"; bfldEmployeeCode.HeaderText = "Employee Code"; gvMainDataGrid.Columns.Add(bfldEmployeeCode); BoundField bfldProjectCode = new BoundField(); bfldProjectCode.DataField = "Project Code"; bfldProjectCode.HeaderText = "Project Code"; gvMainDataGrid.Columns.Add(bfldProjectCode); BoundField bfldProcessCode = new BoundField(); bfldProcessCode.DataField = "Process Code"; bfldProcessCode.HeaderText = "Process Code"; gvMainDataGrid.Columns.Add(bfldProcessCode); BoundField bfldActivityDate = new BoundField(); bfldActivityDate.DataField = "Date"; bfldActivityDate.HeaderText = "Date"; gvMainDataGrid.Columns.Add(bfldActivityDate); BoundField bfldActivityHours = new BoundField(); bfldActivityHours.DataField = "Activity Hours"; bfldActivityHours.HeaderText = "Activity Hours"; gvMainDataGrid.Columns.Add(bfldActivityHours); CommandField cfldDeleteButton = new CommandField(); cfldDeleteButton.ShowDeleteButton = true; gvMainDataGrid.Columns.Add(cfldDeleteButton); It's data source is a Dataset retrieved from a webservice. Here is the C#code to bind the data to the gridview Service1 MyService = new Service1(); dsMainData = MyService.GetData(strIdNumber); gvMainDataGrid.DataSource = dsMainData; gvMainDataGrid.DataBind();
It retrieves great. No problems at all. Then, when I actually try to use the delete button, I get an error that the RowDeleting event is not being handled. I thought that was pretty explicit, so I created the event: