Validate User Input before calling java script function

Discussion in 'ASP .Net Datagrid Control' started by Santosh, Jun 3, 2006.

  1. Santosh

    Santosh Guest

    Hii i am writting a code for adding a user records but i want to
    activate required filed validator before displaying conform addition
    messagebox

    i am writtng following code but it is never work please tell me that
    what is solution behind them.


    public void SetUpClientValidation(object sender, DataGridItemEventArgs
    e)
    {
    if ( e.Item.ItemType == ListItemType.AlternatingItem ||
    e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.SelectedItem )
    {
    e.Item.Cells[4].Attributes.Add("OnClick","JavaScript:return
    ConfirmDeletionInformation();");
    }
    if(e.Item.ItemType == ListItemType.Footer)
    {
    e.Item.Cells[3].Attributes.Add("OnClick","JavaScript:return
    ConformAddInformation();");

    }


    //
    public void DataGridItemCommand(object sender, DataGridCommandEventArgs
    e)
    {
    try
    {
    switch(e.CommandName)
    {
    case "GotoDistrict":
    GoToDistrict(e.Item,e.Item.ItemIndex);
    break;
    case "AddNewRecords":
    AddNewRecords(e.Item,e.Item.ItemIndex);
    break;
    }
    }
    catch(Exception ex)
    {
    ex.Message.ToString();
    }
    }


    //

    private void AddNewRecords(DataGridItem item, int rowIndex)
    {
    try
    {
    RequiredFieldValidator stateNoRequired
    =(RequiredFieldValidator)item.FindControl("stateNo_Requiredfieldvalidator");
    RequiredFieldValidator stateDescRequired
    =(RequiredFieldValidator)item.FindControl("stateDesc_Requiredfieldvalidator");
    RangeValidator stateNoRange =
    (RangeValidator)item.FindControl("stateNo_RangeValidator");
    stateNoRequired.Enabled = true;
    stateDescRequired.Enabled = true;
    stateNoRange.Enabled = true;

    TextBox stateNo_TextBox =
    (TextBox)item.FindControl("addStateNo_TextBox");
    TextBox stateName_TextBox =
    (TextBox)item.FindControl("addStateDesc_TextBox");

    string recordFound =objStateMaster.FindRec(stateNo_TextBox.Text
    ,stateName_TextBox.Text);

    if (recordFound!="")
    {
    string msg = " already exists" ;
    string space = " and ";
    string stateNo="StateNo: ";
    string stateDesc= "StateName: ";


    switch(recordFound)
    {
    case "11":
    Response.Write("<script language=javascript> alert('"+ stateNo +
    "' +'" + stateNo +"' + '"+ space +"' + '"+ stateDesc +"' + '"+
    stateDesc +"'+ '" + msg + "');</script>");
    break;
    case "10":
    Response.Write("<script language=javascript> alert('"+ stateNo
    +"' +'" + stateNo +"' + '" + msg + "');</script>");
    break;
    case "01":
    Response.Write("<script language=javascript> alert('"+ stateDesc
    +"' +'"+ stateDesc + "' + '" + msg + "');</script>");
    break;
    }
    return;
    }

    if(stateNo_TextBox != null)
    {
    objStateMaster.StateNo = stateNo_TextBox.Text;

    }

    if(stateName_TextBox != null)
    {
    objStateMaster.StateDesc = stateName_TextBox.Text;
    }

    objStateMaster.AddRec();
    BindDataGrid();
    stateNo_TextBox.Text="";
    stateName_TextBox.Text="";

    stateMaster_DataGrid.Columns[5].Visible = true;
    }
    catch(Exception ex)
    {
    ex.Message.ToString();
    }
    }

    Regards,
    Santosh Shinde
    Vritti Software Solution.
     
    Santosh, Jun 3, 2006
    #1
    1. Advertising

  2. Santosh

    Scott M. Guest

    Why not just use the RequiredFieldValidator controls and let them do all the
    work?


    "Santosh" <> wrote in message
    news:...
    > Hii i am writting a code for adding a user records but i want to
    > activate required filed validator before displaying conform addition
    > messagebox
    >
    > i am writtng following code but it is never work please tell me that
    > what is solution behind them.
    >
    >
    > public void SetUpClientValidation(object sender, DataGridItemEventArgs
    > e)
    > {
    > if ( e.Item.ItemType == ListItemType.AlternatingItem ||
    > e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    > ListItemType.SelectedItem )
    > {
    > e.Item.Cells[4].Attributes.Add("OnClick","JavaScript:return
    > ConfirmDeletionInformation();");
    > }
    > if(e.Item.ItemType == ListItemType.Footer)
    > {
    > e.Item.Cells[3].Attributes.Add("OnClick","JavaScript:return
    > ConformAddInformation();");
    >
    > }
    >
    >
    > //
    > public void DataGridItemCommand(object sender, DataGridCommandEventArgs
    > e)
    > {
    > try
    > {
    > switch(e.CommandName)
    > {
    > case "GotoDistrict":
    > GoToDistrict(e.Item,e.Item.ItemIndex);
    > break;
    > case "AddNewRecords":
    > AddNewRecords(e.Item,e.Item.ItemIndex);
    > break;
    > }
    > }
    > catch(Exception ex)
    > {
    > ex.Message.ToString();
    > }
    > }
    >
    >
    > //
    >
    > private void AddNewRecords(DataGridItem item, int rowIndex)
    > {
    > try
    > {
    > RequiredFieldValidator stateNoRequired
    > =(RequiredFieldValidator)item.FindControl("stateNo_Requiredfieldvalidator");
    > RequiredFieldValidator stateDescRequired
    > =(RequiredFieldValidator)item.FindControl("stateDesc_Requiredfieldvalidator");
    > RangeValidator stateNoRange =
    > (RangeValidator)item.FindControl("stateNo_RangeValidator");
    > stateNoRequired.Enabled = true;
    > stateDescRequired.Enabled = true;
    > stateNoRange.Enabled = true;
    >
    > TextBox stateNo_TextBox =
    > (TextBox)item.FindControl("addStateNo_TextBox");
    > TextBox stateName_TextBox =
    > (TextBox)item.FindControl("addStateDesc_TextBox");
    >
    > string recordFound =objStateMaster.FindRec(stateNo_TextBox.Text
    > ,stateName_TextBox.Text);
    >
    > if (recordFound!="")
    > {
    > string msg = " already exists" ;
    > string space = " and ";
    > string stateNo="StateNo: ";
    > string stateDesc= "StateName: ";
    >
    >
    > switch(recordFound)
    > {
    > case "11":
    > Response.Write("<script language=javascript> alert('"+ stateNo +
    > "' +'" + stateNo +"' + '"+ space +"' + '"+ stateDesc +"' + '"+
    > stateDesc +"'+ '" + msg + "');</script>");
    > break;
    > case "10":
    > Response.Write("<script language=javascript> alert('"+ stateNo
    > +"' +'" + stateNo +"' + '" + msg + "');</script>");
    > break;
    > case "01":
    > Response.Write("<script language=javascript> alert('"+ stateDesc
    > +"' +'"+ stateDesc + "' + '" + msg + "');</script>");
    > break;
    > }
    > return;
    > }
    >
    > if(stateNo_TextBox != null)
    > {
    > objStateMaster.StateNo = stateNo_TextBox.Text;
    >
    > }
    >
    > if(stateName_TextBox != null)
    > {
    > objStateMaster.StateDesc = stateName_TextBox.Text;
    > }
    >
    > objStateMaster.AddRec();
    > BindDataGrid();
    > stateNo_TextBox.Text="";
    > stateName_TextBox.Text="";
    >
    > stateMaster_DataGrid.Columns[5].Visible = true;
    > }
    > catch(Exception ex)
    > {
    > ex.Message.ToString();
    > }
    > }
    >
    > Regards,
    > Santosh Shinde
    > Vritti Software Solution.
    >
     
    Scott M., Jun 5, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Ternier
    Replies:
    4
    Views:
    3,985
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 29, 2004
  2. Arnold Peters
    Replies:
    1
    Views:
    1,545
    Tomer Ben-David
    Oct 27, 2004
  3. Replies:
    1
    Views:
    326
    Chris Rebert
    Feb 18, 2009
  4. bbxrider
    Replies:
    2
    Views:
    104
    bbxrider
    Jul 30, 2003
  5. phal
    Replies:
    12
    Views:
    224
Loading...

Share This Page