Pergunta

I have created a radiobutton list followed by one button. I can select one of the item from the radiobutton list and then click the button to execute something. And I want the page to pop up a window if the button is clicked without selecting any of the options from the radiobutton list. My codebehide is like this:

    protected void ButtonPP_Click(object sender, EventArgs e)
    {
        if (radioBtnPP.SelectedIndex < 0)
        {
            String csname1 = "PopupScript";
            Type cstype = this.GetType();

            // Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = Page.ClientScript;

            // Check to see if the startup script is already registered.
            if (!cs.IsStartupScriptRegistered(cstype, csname1))
            {
                StringBuilder cstext1 = new StringBuilder();
                cstext1.Append("<script type=text/javascript> alert('Please Select Criteria!') </");
                cstext1.Append("script>");

                cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
            }
        }
   }

and my html part is:

    <asp:RadioButtonList class ="radioBtn" ID="radioBtnACO" runat="server">
           <asp:ListItem Text="All Dim" />
           <asp:ListItem Text="Select Dim" />
    </asp:RadioButtonList>
    <asp:Button id ="ButtonPP" class ="buttonRight" runat="server" Text="Run PP" OnClick="ButtonPP_Click" />

This works fine. However, now I don't want to refresh the whole page when i click the button, so I use updatepanel. And I changed my html code to the following:

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="ControlUpdatePanel" runat="server" Visible="True">
        <ContentTemplate>  
           <asp:RadioButtonList class ="radioBtn" ID="radioBtnACO" runat="server">
               <asp:ListItem Text="All Dim" />
               <asp:ListItem Text="Select Dim" />
           </asp:RadioButtonList>
           <asp:Button id ="ButtonPP" class ="buttonRight" runat="server" Text="Run PP" OnClick="ButtonPP_Click" />
        </ContentTemplate>
   </asp:UpdatePanel>  

But now my code doesn't work anymore... If I don't select any option from the radiobutton list and just click the button, no window would pop up :( I am sure my codebehind is still executed but seems like it just doesn't passed to my .aspx page...

Anyone please help me out? I don't know what to do now... Millions of thanks!

Foi útil?

Solução

You need to use ScriptManager.RegisterStartupScript for Ajax.

protected void ButtonPP_Click(object sender, EventArgs e)
{
    if (radioBtnACO.SelectedIndex < 0)
    {
        string csname1 = "PopupScript";

        var cstext1 = new StringBuilder();
        cstext1.Append("alert('Please Select Criteria!')");

        ScriptManager.RegisterStartupScript(this, GetType(), csname1,
            cstext1.ToString(), true);
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top