Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Asp.net c# ScriptManager

Posted on 2010-01-12
5
Medium Priority
?
741 Views
Last Modified: 2012-05-08
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);
}
0
Comment
Question by:supportrp
  • 3
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
Shaun Kline earned 2000 total points
ID: 26295962
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
 
LVL 1

Author Comment

by:supportrp
ID: 26296645
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
 
LVL 1

Author Comment

by:supportrp
ID: 26297649
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
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 26297721
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
 
LVL 1

Author Comment

by:supportrp
ID: 26297740
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question