?
Solved

Asp.net c# ScriptManager

Posted on 2010-01-12
5
Medium Priority
?
738 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 26

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 26

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

752 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