Asp.net c# ScriptManager

I'm currently using the code below to get set the values for a hidden field and later executing a statement by reading the values set by the JavaScript code to the hidden field. The button in which executes this code is residing inside an Ajax UpdatePanel. When executed, we get the prompt, are able to select OK or Cancel, but will not execute execute the Page_Load to set the new values to lblStatus.

*What is the cause and what is a good fix?

protected void Page_Load(object sender, EventArgs e)
 {
    if (hdnFlag.Value == "OK")
        {
            //'run some code
            lblStatus.Text = "You selected OKAY";
        }
        else if (hdnFlag.Value == "Cancel")
        {
            //'run some other code
            lblStatus.Text = "You selected CANCEL";
        }
}

protected void DeleteButton_Click(object sender, EventArgs e)
 {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script language='javascript'> ");
        sb.Append(@"function getConfirmation() {");
        sb.Append(@"var hdn = document.getElementById('hdnFlag'); ");
        sb.Append(@"var result = confirm('Are you sure you want to proceed?'); ");
        sb.Append(@"if (result == true) { ");
        sb.Append(@"hdn.value = 'OK'; } ");
        sb.Append(@"else{hdn.value = 'Cancel'; } ");
        sb.Append(@"return true; } ");
        sb.Append(@"getConfirmation(); ");
        sb.Append(@"</script>");

         
        //sb.Append(@"<script language='javascript'> confirm('testing'); </script>  ");

        ScriptManager.RegisterStartupScript(this, this.GetType(), "JSScript", sb.ToString(), false);
}
LVL 1
supportrpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
If your intention is to confirm that the user wants to delete something, attach the javascript directly to the DeleteButton's onclick attribute. You can then use the DeleteButton_Click event to set the status (or perform your delete routines):

In you page load event:
DeleteButton.Attributes.Add("onclick", "<script type='text/javascript'>return confirm('Are you sure you want to do this?');</script>");

And then you DeleteButton_OnClick event:
protected void DeleteButton_Click(object sender, EventArgs e)
{
lblStatus.Text = "You selected OKAY";
}

By returning the value of the confirm box to the client-side onclick event, you can "cancel" the post-back of the page.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
supportrpAuthor Commented:
Unfortunately, We are using our buttons in a way that they can't have any ID's. They are inside a GridView and are used as a button field.
0
supportrpAuthor Commented:
I was able to get the button field by using the following:

    foreach (GridViewRow row in Grdview1.Rows)
        {
            Button btnButton = null;
            btnButton = (Button)row.Cells[1].Controls[0];
            btnButton.Attributes.Add("onclick", sb.ToString());
        }

Later inside the gridview options I added the RowCommand to be "Grdview1_RowCommand1"

protected void Grdview1_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "AddNewTest")
        {

            if (hdnFlag.Value == "OK")
            {
                //'run some code
                lblStatus.Text = "You selected OKAY";
            }
            else if (hdnFlag.Value == "Cancel")
            {
                //'run some other code
                lblStatus.Text = "You selected CANCEL";
            }
        }
   }


This is our code residing on our .aspx page

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="lblStatus" runat="server"></asp:Label>
            <asp:HiddenField ID="hdnFlag" runat="server" />
            <asp:GridView ID="Grdview1" runat="server" onrowcommand="Grdview1_RowCommand1">
                <Columns>
                    <asp:ButtonField ButtonType="Button" Text="Test" CommandName="AddNewTest" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

Everything else was organized as Shaun_Kline posted.

Thank You for the response.
       
0
Shaun KlineLead Software EngineerCommented:
If you use the OnRowDataBound event, you can attach the script to the button when the row is created.
Also, if you are including a delete button for each row in the gridview, you should also consider using the OnRowDeleting event of the gridview.

Beyond that, the only glaring issue that I see in your script is this line:
sb.Append(@"var hdn = document.getElementById('hdnFlag'); ");
Depending on where the hdnFlag field exists in your document structure,  when .NET renders the page, it will assign an ID to that field based on any containers to which that field belongs. (Look at the rendered source code.)

You should consider using:
sb.AppendFormat(@"var hdn = document.getElementById('{0}');", hdnFlag.ClientID);
0
supportrpAuthor Commented:
In the previous post, the btnButton was pointing to the wrong cell. This is the correct version:

foreach (GridViewRow row in Grdview1.Rows)
        {
            Button btnButton = null;
            btnButton = (Button)row.Cells[0].Controls[0];
            btnButton.Attributes.Add("onclick", sb.ToString());
        }
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.