如何在ASP.net中的GridView控件内单击一次验证DropDownList和Update Database?

时间:2016-10-07 10:42:53

标签: javascript asp.net vb.net gridview

我在更新面板中有一个GridViewGridView的最后一列有一个LinkButtonID="lnkUpdate")。此LinkButton用于使用JavaScript函数(Validate(lnkUpdate))验证DropDowns的其他列中的一些GridView

我想使用此LinkButton更新数据库中的数据。如果我使用OnClick="btnSave_Data_Click",我可以成功更新数据库中的数据,但是,我必须删除OnClientClick="return Validate(this)"

论坛中的专家能否帮助我找到一种方法来保存这两个事件。我的意思是我希望能够更新数据库以及验证DropDownList

我尝试过:

Validate(lnkUpdate)的代码是:

<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>

并且btnSave_Data_Click的代码是:

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

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用

从服务器端按钮单击调用javascript
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);