Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pt
Hi All,

Currently I'm facing a problem validating the user input from gridview emptydatatemplate. I'll elaborate my scenario below please take a look.
The requirement is like the users should enter data and should be able to see the entered data before final save. So, I've taken a gridview and binded it to a temptable which will don't have any data initially, so on the first page load the datasource will be empty and the emptydatatemplate will be shown to the user. On this EmptyDataTemplate I've prepared a dataentry screen where the user can give the information and can save the data. After saving the data the data will go to the temptable and the gridview will get populated with the data the user has entered. For next go, the user can enter records through the gridview footer row. In this way I'm doing my things.
Now I haven't done the validation yet, I wanted to do a validation of the user input on gridview emptydatatemplate and gridview footer as well. In order to do that I've created one Button object on RowDataBound and assigned the gridview emptydatatemplate save button against it and also binded one javascript function with this button like below.

<br />
protected void gvProductData_OnRowDataBound(object sender, GridViewRowEventArgs e)<br />
    {<br />
        Button btnAddModel;<br />
        btnAddModel = (Button)gvProductData.Controls[0].Controls[0].FindControl("btnOk"); // This is the save button of empty data template<br />
        DropDownList ddlModelNoEdt = gvProductData.Controls[0].Controls[0].FindControl("ddlModelNoEDT") as DropDownList;<br />
        DropDownList ddlElectroluxPncSku = gvProductData.Controls[0].Controls[0].FindControl("ddlPncEdt") as DropDownList;<br />
        DropDownList ddlBrandName = gvProductData.Controls[0].Controls[0].FindControl("ddlBrandNameEdt") as DropDownList;<br />
        DropDownList ddlSalesMarketEdt = gvProductData.Controls[0].Controls[0].FindControl("ddlSalesMarketEdt") as DropDownList;<br />
        DropDownList ddlProjectClosedEdt = gvProductData.Controls[0].Controls[0].FindControl("ddlProjectClosedEdt") as DropDownList;<br />
        TextBox txtModelNoEdt = gvProductData.Controls[0].Controls[0].FindControl("txtModelNoEDT") as TextBox;<br />
        TextBox txtPilotRunDate1Edt = gvProductData.Controls[0].Controls[0].FindControl("txtPilotRunDate1Edt") as TextBox;<br />
        TextBox txtPilotRunDate2Edt = gvProductData.Controls[0].Controls[0].FindControl("txtPilotRunDate2Edt") as TextBox;<br />
        TextBox txtMassProdRunDateEdt = gvProductData.Controls[0].Controls[0].FindControl("txtMassProdRunDateInsertEdt") as TextBox;<br />
        TextBox txtShiplogDateEdt = gvProductData.Controls[0].Controls[0].FindControl("txtShiplogDateEdt") as TextBox;<br />
        btnAddModel.Attributes.Add("onClick", "javascript:return validatemodel('" + ddlModelNoEdt.ClientID + "','" + ddlPncSku.ClientID + "','" + ddlBrandName.ClientID + "','" + txtModelNoEdt.ClientID + "','" + ddlSalesMarketEdt.ClientID + "','"<br />
            + txtPilotRunDate1Edt.ClientID + "','" + txtPilotRunDate2Edt.ClientID + "','" + txtMassProdRunDateEdt.ClientID + "','" + txtShiplogDateEdt.ClientID + "','" + ddlProjectClosedEdt.ClientID + "');");//Called the javascript function<br />


The javascript is working fine and returning boolean values based on the validation done.
But the problem is after I did this validations the RowCommand event of my gridview is not firing. I'm inserting this records on this event only based on the commandname of the button. But if I remove this javascript validation everything works fine for me.
I'm using .NET3.5, Visual Studio 2008. My gridview resides inside an ajax tabcontainer. Is there any way I can validate the user input on Gridview EmptyDataTemplate and Footer Template using javascript. ANy help on this would be highly appreciated. Thanks in advance.
Posted
Comments
Sunasara Imdadhusen 22-Sep-10 3:18am    
you have written everything is right, just i want to check JavaScript function. Please send me the code of java script function validatemodel,
Bodan_ 22-Sep-10 3:49am    
Hi,
Thanks for your quick reply. Please find the javascript code below.

<code>
<pre lang="cs">function validatemodel(ddlModel,ddlPncSku,ddlBrandName,txtModelNoEdt, ddlSalesMarket,txtPilotRunDate1Edt,txtPilotRunDate2Edt,
 txtMassProdRunDateEdt,txtShiplogDateEdt,ddlProjectClosed)
{
      var msg = &quot;&quot;;
      var Model_ddlst = document.getElementById(ddlModel);
      var Model_val;
      if (Model_ddlst == null)
      {
        var Model_txt = document.getElementById(txtModelNoEdt);
            Model_val=ElectroluxModel_txt.value;
      }
      else
      {
         Model_val = Model_ddlst.options[Model_ddlst.selectedIndex].value;
      }


      var PncSku_ddlst = document.getElementById(ddlPncSku);
      var PncSku_val = PncSku_ddlst.options[PncSku_ddlst.selectedIndex].value;
      var BrandName_ddlst = document.getElementById(ddlBrandName);
      var BrandName_val = BrandName_ddlst.options[BrandName_ddlst.selectedIndex].value;
       //alert(BrandName_val);
      var SalesMarket_ddlst = document.getElementById(ddlSalesMarket);
      var SalesMarket_val = SalesMarket_ddlst.options[SalesMarket_ddlst.selectedIndex].value;
      //alert(SalesMarket_val);
      var PilotRunDate1_txt = document.getElementById(txtPilotRunDate1Edt);
      var PilotRunDate1_val= PilotRunDate1_txt.value;
      //alert(PilotRunDate1_val);
      var PilotRunDate2_txt = document.getElementById(txtPilotRunDate2Edt);
      var PilotRunDate2_val= PilotRunDate2_txt.value;
      //alert(PilotRunDate2_val);
      var MassProdRunDate_txt = document.getElementById(txtMassProdRunDateEdt);
      var MassProdRunDate_val= MassProdRunDate_txt.value;
      //alert(MassProdRunDate_val);
      var ShiplogDate_txt = document.getElementById(txtShiplogDateEdt);
      var ShiplogDate_val= ShiplogDate_txt.value;
      //alert(ShiplogDate_val);

      var ProjectClosed_ddlst = document.getElementById(ddlProjectClosed);
      var ProjectClosed_val = ProjectClosed_ddlst.options[ProjectClosed_ddlst.selectedIndex].value;
        //alert(ProjectClosed_val);
        if(ElectroluxModel_val == &quot;--Select--&quot; || ElectroluxModel_val==&quot;&quot;)
        {
            msg+=&quot;- Please give one valid Electrolux Model.
&quot;;
        }

        if(ElectroluxPncSku_val == &quot;--Select--&quot; || ElectroluxPncSku_val == null)
        {
            msg+=&quot;- Please select one valid Electrolux PNC SKU.
&quot;;
        }

        if(BrandName_val == &quot;--Select--&quot; || BrandName_val == null)
        {
            msg+=&quot;- Please select one valid Brand Name.
&quot;;
        }

        if(SalesMarket_val == &quot;--Select--&quot; || SalesMarket_val == null)
        {
            msg+=&quot;- Please select one valid Sales Market.
&quot;;
        }

        if(ProjectClosed_val == &quot;--Select--&quot; || ProjectClosed_val == null)
        {
            msg+=&quot;- Please select Y if the Project Closed or select N otherwise.
&quot;;
        }

      if(msg==&quot;&quot;)
      {

        return true;
      }
      else
      {
        alert(&quot;The following fields are mandatory.

&quot; + msg);
        return false;
      }
}



I've tested this script multiple time but couldn't able to find any issue after successfull validation it is returning true. I make sure by giving an alert into the if block from where it is returning true. It would be very helpful for me if you can guide me on this. Thank again.
Sunasara Imdadhusen 22-Sep-10 4:09am    
Thanks, as you said it 'after successfull validation it is returning true'. please can you provide HTML block of code for complete <asp:gridview>
Sunasara Imdadhusen 22-Sep-10 4:09am    
asp:GridView tag

1 solution

Hi Bodan,

Here is the solution, Please do the following steps:

1. Enable EnableEventValidation="false"
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ZTest.aspx.cs" Inherits="ZTest" EnableEventValidation="false" %>

2.Create event for btnOk
C#
protected void btnOk_Click(object sender, EventArgs e)
    {
        //Your code for saving data into DataBase is here...
    }

3. Attach click event to the btnOk at client side
XML
<asp:Button ID="btnOk" runat="server" CausesValidation="true" CssClass="button-go"Text="Add Model" Width="80" OnClick="btnOk_Click" />



Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.

Thanks,
Imdadhusen
 
Share this answer
 
Comments
Bodan_ 22-Sep-10 4:56am    
Hi Imadadhusen,

Thank you for your prompt solution.Currently I'm inserting data from OnRowCommand event of the gridview based on the command name given to the button. I'm doing this for some other buttons also. Do you want me to remove the code from OnRowCommand and put into the buttonclick event on the backend? I'll be doing this and will let you know. I'm afraid if this causes any issue with the existing buttons on the gridview. Thanks again.

Regards,
Bodan
Bodan_ 22-Sep-10 5:25am    
Hi Imadadhusen,

I've followed the steps you have provided above but unfortunately it also didn't work for me. After validation is done by javascript the onclick event is not firing for the Save button. Is there any thing else that I can do. Please assist.

Regards,
Bodan
Sunasara Imdadhusen 22-Sep-10 5:33am    
how can it possible? because the same think i have written in my application and it is working fine.
Please confirm third step with each attributes. if you have more attributes please remove it.

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