Injecting javascript into a particular part in a page from inside a codebehind file.

Discussion in 'ASP .Net' started by Andy B, Apr 25, 2008.

  1. Andy B

    Andy B Guest

    How do you add javascript to a page from codebehind? I have a method I am
    working on that has a few conditions in it:

    1. if either 1 or both WordTextBox or DefinitionTextBox is empty, show a
    popup window telling the user that the textbox(s) can't be empty and return
    them to the page with no further action.
    2. The value in WordTextBox is already in the collection as a key, so show a
    popup telling the user that the word already exists and then return them to
    the page with no further action.

    How would you do this?
     
    Andy B, Apr 25, 2008
    #1
    1. Advertising

  2. Re: Injecting javascript into a particular part in a page from insidea codebehind file.

    On Apr 25, 7:19 pm, "Andy B" <> wrote:
    > How do you add javascript to a page from codebehind? I have a method I am
    > working on that has a few conditions in it:
    >
    > 1. if either 1 or both WordTextBox or DefinitionTextBox is empty, show a
    > popup window telling the user that the textbox(s) can't be empty and return
    > them to the page with no further action.
    > 2. The value in WordTextBox is already in the collection as a key, so show a
    > popup telling the user that the word already exists and then return them to
    > the page with no further action.
    >
    > How would you do this?


    1. I think you can do this with ASP.NET RequiredFieldValidator &
    ValidationSummary Controls
    2. How do you get collection? Is it a database value?
     
    Alexey Smirnov, Apr 25, 2008
    #2
    1. Advertising

  3. Andy B

    Andy B Guest

    collection is a dictionary<strin, string>

    "Alexey Smirnov" <> wrote in message
    news:...
    On Apr 25, 7:19 pm, "Andy B" <> wrote:
    > How do you add javascript to a page from codebehind? I have a method I am
    > working on that has a few conditions in it:
    >
    > 1. if either 1 or both WordTextBox or DefinitionTextBox is empty, show a
    > popup window telling the user that the textbox(s) can't be empty and
    > return
    > them to the page with no further action.
    > 2. The value in WordTextBox is already in the collection as a key, so show
    > a
    > popup telling the user that the word already exists and then return them
    > to
    > the page with no further action.
    >
    > How would you do this?


    1. I think you can do this with ASP.NET RequiredFieldValidator &
    ValidationSummary Controls
    2. How do you get collection? Is it a database value?
     
    Andy B, Apr 25, 2008
    #3
  4. Andy B

    clintonG Guest

    /*
    How to validate two textboxes to ensure only one textbox contains data. Copy
    and paste into Visual Studio for best results reading and comprehending how
    this solution may be helpful to learn from...

    */

    <%-- CUSTOM VALIDATOR --%>
    <asp:CustomValidator
    ID="ManagingEditorValidator" runat="server"
    EnableViewState="false"
    OnServerValidate="ManagingEditorTextBoxes_CustomValidator"
    ErrorMessage="" />


    // CustomValidator raises this event...
    #region Validation: ManagingEditorTextBoxes_CustomValidator...
    protected void ManagingEditorTextBoxes_CustomValidator(object
    source, ServerValidateEventArgs args)
    {
    if (ChannelManagingEditorEmailTextBox.Text.Length > 0 &&
    ChannelManagingEditorNameTextBox.Text.Length > 0)
    {
    args.IsValid = false;
    ManagingEditorValidationLabel.Text = "<span
    style='color:red;font-weight:bold;'>Only one TextBox may contain
    data.</span>";
    ChannelBuilderWizard.ActiveStepIndex = 5; //
    Step5_ChannelManagingEditor
    }
    else
    {
    args.IsValid = true;
    ManagingEditorValidationLabel.Text = "-- or --";
    }
    }//protected void ManagingEditorTextBoxes_CustomValidator(object
    source, ServerValidateEventArgs args)
    #endregion

    /*
    I also used the OnActiveStepChanged event of a Wizard control to determine
    when to call a ValidateManagingEditorOrWebmasterTextBoxes() method
    that --also-- modifies the ManagingEditorValidationLabel I have mentioned.
    You can figure out where to call the method once you study and learn how
    this example of server-side validation works.
    */


    #region Validation: ValidateManagingEditorOrWebmasterTextBoxes...
    // Both ManagingEditor and Webmaster steps display a set of
    TextBoxes; one for email and another for a name
    // but only one TextBox may be used to submit data.
    // ChannelBuilderWizard_OnActiveStepChanged calls this method when
    selecting the Wizard's Next button.
    // ManagingEditorTextBoxes_CustomValidator and
    WebmasterTextBoxes_CustomValidator are called when SideBar LinkButtons are
    selected.
    protected void ValidateManagingEditorOrWebmasterTextBoxes()
    {
    if (ChannelManagingEditorEmailTextBox.Text.Length > 0 &&
    ChannelManagingEditorNameTextBox.Text.Length > 0)
    {
    ManagingEditorValidationLabel.Text = "<span
    style='color:red;font-weight:bold;'>Only one TextBox may contain
    data.</span>";
    ChannelBuilderWizard.ActiveStepIndex = 5;
    }
    else if (ChannelWebmasterEmailTextBox.Text.Length > 0 &&
    ChannelWebmasterNameTextBox.Text.Length > 0)
    {
    WebmasterValidationLabel.Text = "<span
    style='color:red;font-weight:bold;'>Only one TextBox may contain
    data.</span>";
    ChannelBuilderWizard.ActiveStepIndex = 6;
    }
    else
    {
    if
    (String.IsNullOrEmpty(ChannelManagingEditorEmailTextBox.Text.ToString()) ||
    String.IsNullOrEmpty(ChannelManagingEditorNameTextBox.Text.ToString()))
    {
    ManagingEditorValidationLabel.Text = "-- or --";
    }
    if
    (String.IsNullOrEmpty(ChannelWebmasterEmailTextBox.Text.ToString()) ||
    String.IsNullOrEmpty(ChannelWebmasterNameTextBox.Text.ToString()))
    {
    WebmasterValidationLabel.Text = "-- or --";
    }
    }
    }//ValidateManagingEditorAndWebmasterTextBoxes()
    #endregion

    <%= Clinton Gallagher


    "Andy B" <> wrote in message
    news:%...
    > collection is a dictionary<strin, string>
    >
    > "Alexey Smirnov" <> wrote in message
    > news:...
    > On Apr 25, 7:19 pm, "Andy B" <> wrote:
    >> How do you add javascript to a page from codebehind? I have a method I am
    >> working on that has a few conditions in it:
    >>
    >> 1. if either 1 or both WordTextBox or DefinitionTextBox is empty, show a
    >> popup window telling the user that the textbox(s) can't be empty and
    >> return
    >> them to the page with no further action.
    >> 2. The value in WordTextBox is already in the collection as a key, so
    >> show a
    >> popup telling the user that the word already exists and then return them
    >> to
    >> the page with no further action.
    >>
    >> How would you do this?

    >
    > 1. I think you can do this with ASP.NET RequiredFieldValidator &
    > ValidationSummary Controls
    > 2. How do you get collection? Is it a database value?
    >
     
    clintonG, Apr 26, 2008
    #4
  5. Andy B

    Andy B Guest

    This wasn't exactly what I was looking for. I need to have a popup window
    (the javascript alert window) pop up if:

    1. The WordTextBox.Text property is empty.
    2. The DefinitionTextBox.Text property is empty.
    3. Both WordTextBox.Text and DefinitionTextBox.Text are empty at the same
    time.
    4. The value for WordTextBox.Text already exists inside the dictionary
    collection.

    Of course all of these have different messages that go along with them.


    "clintonG" <> wrote in message
    news:...
    > /*
    > How to validate two textboxes to ensure only one textbox contains data.
    > Copy and paste into Visual Studio for best results reading and
    > comprehending how this solution may be helpful to learn from...
    >
    > */
    >
    > <%-- CUSTOM VALIDATOR --%>
    > <asp:CustomValidator
    > ID="ManagingEditorValidator" runat="server"
    > EnableViewState="false"
    > OnServerValidate="ManagingEditorTextBoxes_CustomValidator"
    > ErrorMessage="" />
    >
    >
    > // CustomValidator raises this event...
    > #region Validation: ManagingEditorTextBoxes_CustomValidator...
    > protected void ManagingEditorTextBoxes_CustomValidator(object
    > source, ServerValidateEventArgs args)
    > {
    > if (ChannelManagingEditorEmailTextBox.Text.Length > 0 &&
    > ChannelManagingEditorNameTextBox.Text.Length > 0)
    > {
    > args.IsValid = false;
    > ManagingEditorValidationLabel.Text = "<span
    > style='color:red;font-weight:bold;'>Only one TextBox may contain
    > data.</span>";
    > ChannelBuilderWizard.ActiveStepIndex = 5; //
    > Step5_ChannelManagingEditor
    > }
    > else
    > {
    > args.IsValid = true;
    > ManagingEditorValidationLabel.Text = "-- or --";
    > }
    > }//protected void ManagingEditorTextBoxes_CustomValidator(object
    > source, ServerValidateEventArgs args)
    > #endregion
    >
    > /*
    > I also used the OnActiveStepChanged event of a Wizard control to determine
    > when to call a ValidateManagingEditorOrWebmasterTextBoxes() method
    > that --also-- modifies the ManagingEditorValidationLabel I have mentioned.
    > You can figure out where to call the method once you study and learn how
    > this example of server-side validation works.
    > */
    >
    >
    > #region Validation: ValidateManagingEditorOrWebmasterTextBoxes...
    > // Both ManagingEditor and Webmaster steps display a set of
    > TextBoxes; one for email and another for a name
    > // but only one TextBox may be used to submit data.
    > // ChannelBuilderWizard_OnActiveStepChanged calls this method when
    > selecting the Wizard's Next button.
    > // ManagingEditorTextBoxes_CustomValidator and
    > WebmasterTextBoxes_CustomValidator are called when SideBar LinkButtons are
    > selected.
    > protected void ValidateManagingEditorOrWebmasterTextBoxes()
    > {
    > if (ChannelManagingEditorEmailTextBox.Text.Length > 0 &&
    > ChannelManagingEditorNameTextBox.Text.Length > 0)
    > {
    > ManagingEditorValidationLabel.Text = "<span
    > style='color:red;font-weight:bold;'>Only one TextBox may contain
    > data.</span>";
    > ChannelBuilderWizard.ActiveStepIndex = 5;
    > }
    > else if (ChannelWebmasterEmailTextBox.Text.Length > 0 &&
    > ChannelWebmasterNameTextBox.Text.Length > 0)
    > {
    > WebmasterValidationLabel.Text = "<span
    > style='color:red;font-weight:bold;'>Only one TextBox may contain
    > data.</span>";
    > ChannelBuilderWizard.ActiveStepIndex = 6;
    > }
    > else
    > {
    > if
    > (String.IsNullOrEmpty(ChannelManagingEditorEmailTextBox.Text.ToString())
    > || String.IsNullOrEmpty(ChannelManagingEditorNameTextBox.Text.ToString()))
    > {
    > ManagingEditorValidationLabel.Text = "-- or --";
    > }
    > if
    > (String.IsNullOrEmpty(ChannelWebmasterEmailTextBox.Text.ToString()) ||
    > String.IsNullOrEmpty(ChannelWebmasterNameTextBox.Text.ToString()))
    > {
    > WebmasterValidationLabel.Text = "-- or --";
    > }
    > }
    > }//ValidateManagingEditorAndWebmasterTextBoxes()
    > #endregion
    >
    > <%= Clinton Gallagher
    >
    >
    > "Andy B" <> wrote in message
    > news:%...
    >> collection is a dictionary<strin, string>
    >>
    >> "Alexey Smirnov" <> wrote in message
    >> news:...
    >> On Apr 25, 7:19 pm, "Andy B" <> wrote:
    >>> How do you add javascript to a page from codebehind? I have a method I
    >>> am
    >>> working on that has a few conditions in it:
    >>>
    >>> 1. if either 1 or both WordTextBox or DefinitionTextBox is empty, show a
    >>> popup window telling the user that the textbox(s) can't be empty and
    >>> return
    >>> them to the page with no further action.
    >>> 2. The value in WordTextBox is already in the collection as a key, so
    >>> show a
    >>> popup telling the user that the word already exists and then return them
    >>> to
    >>> the page with no further action.
    >>>
    >>> How would you do this?

    >>
    >> 1. I think you can do this with ASP.NET RequiredFieldValidator &
    >> ValidationSummary Controls
    >> 2. How do you get collection? Is it a database value?
    >>

    >
     
    Andy B, Apr 26, 2008
    #5
    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. amine  zejli
    Replies:
    1
    Views:
    418
    Tom Yates
    Oct 20, 2003
  2. amine  zejli
    Replies:
    0
    Views:
    389
    amine zejli
    Oct 19, 2003
  3. Amine Zejli
    Replies:
    1
    Views:
    428
    William Ryan
    Oct 19, 2003
  4. Christopher Ambler
    Replies:
    2
    Views:
    634
    Christopher Ambler
    Aug 27, 2003
  5. Replies:
    2
    Views:
    300
    bruce barker
    Apr 20, 2008
Loading...

Share This Page