I am creating a page with a standard edit item style datagrid. I have added a button to perform a javascript function which I need to dynamically pass the name of controls to. The problem I am having is if I put the dynamic onclick event in the code behind, it stops the grid from firing the postback event for the update button. I am not sure what is happening.
Upon looking at the html source generated I noticed that if I add the onclick event in the VB code behind, it causes the edit item controls to not include the datagrid prefix ("DataGrid1:_ctl4:" in this case). Could this be why the update event does not fire? How can I fix this? I can't even hard code the javascript onclick event into the aspx page because not adding the onclick causes the prefix to be added which is different for each row. Am I using the wrong type of button for this? I am using an HTMLInput button as the ASP button I was using was posting back which I didn't want it to do.
I have included the 2 html generated sources for the edit item depending on if I add the onclick event or not. Attached is also the code I use to assign the onclick event. To test it, I comment out the lines which assign the onclick event and it then allows postback upon clicking the update button. This is very strange.
'Here I add my dynamic onclick event.Protected Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated If e.Item.ItemType = ListItemType.EditItem Then Dim _Format As Control = e.Item.FindControl("uxCurrencyFormat") Dim _FormatExcel As Control = e.Item.FindControl("uxCurrencyFormatExcel") Dim _FormatActiveReports As Control = e.Item.FindControl("uxCurrencyFormatActiveReport") Dim _Symbol As Control = e.Item.FindControl("uxSymbol") Dim _Delimiter As Control = e.Item.FindControl("uxDelimiter") Dim _DecimalPt As Control = e.Item.FindControl("uxDecimalPoint") Dim _DecimalPlaces As Control = e.Item.FindControl("uxDecimalPlaces") Dim _EditBtn2 As HtmlInputButton = CType(e.Item.FindControl("btnCreateFormats"), HtmlInputButton) _EditBtn2.Attributes.Add("onClick", "javascript:return generateCurrencyFormat('" & _Format.ClientID & "','" & _FormatExcel.ClientID & "','" & _FormatActiveReports.ClientID & _ "','" & _Symbol.ClientID & "','" & _Delimiter.ClientID & "','" & _DecimalPt.ClientID & "','" & _DecimalPlaces.ClientID & "');") End If End Sub
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
Jim Murphy
Programmer at Smart IT Solutions
When asked, what has been your best career decision?
Deciding to stick with EE.
Mohamed Asif
Technical Department Head
Being involved with EE helped me to grow personally and professionally.
Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question
Connect with Certified Experts to gain insight and support on specific technology challenges including: