简体   繁体   中英

Script not working when called from .cs file

I am tryong to do the following in a Row_Command event of a gridview. But the the pop up box never comes up, I have tried it in so many different ways.. but yet no luck. Please if someone can see the issue i would really appreciate a pointer.

protected void Gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName == "Merchant")
    {
        if (ItemsAvailable)
        {
            StringBuilder sb = new StringBuilder();
            MyClass class = new MyClass();
            TList<LineItems> otherItems = MyClass.GetItems(id);
            bool IsNotAvailable = false;

            foreach (LineItems item in otherItems)
            {
                Merchandise skuMerchandise = skuMerchandise.GetMerchandise(otherItems.mid);
                if (skuMerchandise  != null)
                {
                    if (skuMerchandise.AvailableItems <= 0)
                    {
                        sb.Append(OtherItems.Name);
                        sb.Append(Environment.NewLine);
                        IsNotAvailable = true;
                    }
                }
            }

            if (IsNotAvailable)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", 
                    "function Redirect() {location.href = 'homePage.aspx';} 
                    if(confirm('The items : "+sb.ToString()+" will arrive in 1 month. 
                    Do you wish to continue?') == true){Redirect();};", true);
            }
        }
    }

Everytime i click the button, it just passes like nothing.. never prompts eveb though IsNotAvailable is true when I add a breakpoint.

You can go for a simpler way,

  1. Define the javascript function in the design/separate script file so that it accepts the name of item. eg. myFunction(itemName)

  2. And in your CS file, simply add a call to that function,

    if (IsNotAvailable) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "myFunction('" + itemName + "') } }

It will make things simpler and you would be able to confirm if this is a Javascript issue or a problem in how you are writing it through CS file.

Update:

Your first goal should be to make sure that your JS function is working for you, so before anything, add the following code in an empty html file and run it,

<script type='text/javascript'>
ItemNotInStock('item');

function ItemNotInStock(itemName)
{
var message = "The following items are no longer in stock :" + itemName + ". 

Would you like to continue?";

if (confirm(message) == true)
{ location.href = "homePage.aspx"; }

}

If you redirect correctly then do what's mentioned below.

Define the following javascript in your design file(tested it locally, working for me in chrome,)

<script type='text/javascript'>
function ItemNotInStock(itemName)
{
var message = "The following items are no longer in stock :" + itemName + ". 

Would you like to continue?";

if (confirm(message) == true)
{ location.href = "homePage.aspx"; }

}

In your C# code, add following line

if (IsNotAvailable)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", 
string.Format("ItemNotInStock('{0}');", itemName);
        }
    }

Make sure your JavaScript code is executed by using breakpoints available in the developer tools of the browser of your choice, like Chrome Developer Tools: Breakpoints

BTW: Why do you create an instance of Merchandise if you instantly discard it with the next line?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM