Click here to Skip to main content
15,888,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one GridView inside an update panel. Last column of the GridView has a LinkButton (<pre lang="HTML">ID="lnkUpdate"
). This LinkButton is used to Validate a few DropDowns in other Columns of GridView using JavaScript function (Validate(lnkUpdate)). I want to update the Data in a DataBase using this LinkButton. I can successfully update the data in the database if I use OnClick="btnSave_Data_Click" however, I have to remove OnClientClick="return Validate(this)".

Can the experts in the forum help me find a way to keep both of these events. I mean I want to be able to Update the Database as well as Validate the DropDownList.

Thanks in advance


What I have tried:

The code for Validate(lnkUpdate) is:

JavaScript
<script type="text/javascript">
    function Validate(lnkUpdate) {
        var txtIncident, ddlStatus, ddlReason;
        var row = lnkUpdate.parentNode.parentNode;
        var controls = row.getElementsByTagName("*");
        for (var i = 0; i < controls.length; i++) {
            if (controls[i].id.indexOf("txtIncident") != -1) {
                txtIncident = controls[i];
            }
            if (controls[i].id.indexOf("ddlStatus") != -1) {
                ddlStatus = controls[i];
            }
            if (controls[i].id.indexOf("ddlReason") != -1) {
                ddlReason = controls[i];
            }

        }
        var message = "";
        if (ddlStatus.value == "") {
            message += "Please select Status";
        } else if ((ddlStatus.value == "Make") && (ddlReason.value == "")) {
            message += "Please select Reason";
        } else if ((ddlStatus.value == "Miss") && (ddlReason.value == "")) {
            message += "Please select Reason";
        } else if (
            ((ddlStatus.value == "Miss") && (ddlReason.value == "Data Availablility: Customer") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Data Availablility: Dell Other") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Data Availablility: Dell Reporting Team") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Data Availablility: Vendor") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Data Integrity: Customer") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Data Integrity: Dell Other") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Data Integrity: Dell Reporting Team") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Data Integrity: Vendor") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Duplicate Instance") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "External Factors") && (txtIncident.value == ""))
            || ((ddlStatus.value == "Miss") && (ddlReason.value == "Report Modifications: Customer") && (txtIncident.value == ""))
            ) {
            message += "Please enter Incident#";
        } else {


        }
        //Display error message.
        if (message != "") {
            alert(message);
            return false;
        }
        return true;
    }

</script>



and the code for btnSave_Data_Click is:

Protected Sub btnSave_Data_Click(sender As Object, e As EventArgs) Handles btnSave_Data.Click
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "sp_Insrt_Updt_Rcrd"
cmd.Parameters.AddWithValue("@Report_Date", Today())
cmd.Parameters.AddWithValue("@Name", "")
cmd.Parameters.AddWithValue("@Report_Time", "")
cmd.Parameters.AddWithValue("@Type", "")
cmd.Parameters.AddWithValue("@Effort", "")
cmd.Parameters.AddWithValue("@Account", "")
cmd.Parameters.AddWithValue("@Method", "")
cmd.Parameters.AddWithValue("@Format", "")
cmd.Parameters.AddWithValue("@Status", "")
cmd.Parameters.AddWithValue("@Reason", "")
cmd.Parameters.AddWithValue("@Incident", "")
cmd.Parameters.AddWithValue("@Action", "")
cmd.Parameters.AddWithValue("@Designation", "")
cmd.Parameters.AddWithValue("@Location", "")
cmd.Parameters.AddWithValue("@Support_Region", "")
cmd.Parameters.AddWithValue("@Badge", "")
cmd.Parameters.AddWithValue("@DateTime_IST_India", "")
cmd.Parameters.AddWithValue("@DateTime_CST_Round_Rock", "")
cmd.Parameters.AddWithValue("@DateTime_MST_Cyberjaya", "")
cmd.Parameters.AddWithValue("@DateTime_GMT_Casablanca", "")
cmd.Parameters.AddWithValue("@Week_Working_Day_Number", "")
cmd.Parameters.AddWithValue("@Month_Working_Day_Number", "")
cmd.Parameters.AddWithValue("@NT_Login", "")
cmd.Parameters.AddWithValue("@Report_UID", Today() & "Finally_03")

Dim dt As DataTable = New DataTable
Dim con As SqlConnection = New SqlConnection(strConnString)
Dim sda As SqlDataAdapter = New SqlDataAdapter
cmd.Connection = con
con.Open()
sda.SelectCommand = cmd
sda.Fill(dt)
End Sub
Posted
Updated 7-Oct-16 7:01am
Comments
Are you sure that the client side event is returning true?
ZohaibRazaTheDProgrammer 7-Oct-16 12:12pm    
@Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ) when I click the lnkUpdate get to see the validation errors when wrong values are selected in dropdownlist.
I thought btnSave_Data_Click is called on LinkButton click. Anyway, why can't you do this? So, after you finish with the validation and it returns true, the server click would be fired automatically.
Vincent Maverick Durano 7-Oct-16 7:07am    
Your server-side "btnSave_Data_Click" event should be fired once your JavaScript validation returns true.
ZohaibRazaTheDProgrammer 7-Oct-16 12:12pm    
@Vincent Maverick Durano lnkUpdate is inside a gridview it is not an independent server control.

1 solution

If I understood you correctly, you wanted to execute an update and at the same time do the validation in LinkButton event. If so, then attach an Onclick event for your LinkButton (lnkUpdate) like:

OnClick="lnkUpdate_Click"

Then do something like this in your code behind:
VB
Protected Sub lnkUpdate_Click(sender As Object, e As EventArgs)
	Dim lb As LinkButton = DirectCast(sender, LinkButton)
	Dim row As GridViewRow = DirectCast(lb.NamingContainer, GridViewRow)
	If row IsNot Nothing Then
                'gets the row index selected
		Dim index As Integer = row.RowIndex
		
		'access the controls here
        Dim ddlStatus As DropDownList = DirectCast(row.FindControl("DropDownListID"), DropDownList)
		'your update code here
		
	End If
End Sub

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================
 
Share this answer
 
v3
Comments
ZohaibRazaTheDProgrammer 11-Oct-16 9:41am    
a little more help will be appreciated. Please explain the code.
Vincent Maverick Durano 11-Oct-16 11:00am    
The code is very straight-foward already. Which part of it that you didn't understand?
ZohaibRazaTheDProgrammer 21-Oct-16 12:25pm    
I have used:
<system.web.services.webmethod()> _
Public Shared Function updtRcrds(ByVal name As String) As String
... code to insert records
Return "Hello " & name & Environment.NewLine & "The Current Time is: " & _
DateTime.Now.ToString()
End Function

and called updtRcrds() from JavaScript using:

PageMethods.GetDetails("MY_NAME", OnSuccess);

function OnSuccess(response, userContext, methodName) {
alert(response);
}

This works fine but I want to access the GridView Row Controls when clicking the lnkUpdate.

Any help will be greatly appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900