locked
How to open popup when check box checked in gridview? RRS feed

  • Question

  • User-1355475649 posted

    Hello, I` making board using GridView.

    And now, I want to make like this : 

    1. If Checkbox is checked  in GridView and click modify Button, open popup windows

    2. Else alert message 'Please check your item'

    Now I build a code below, but it`s not working

    This is aspx page

    <asp:LinkButton ID="lnbEdit" runat="server" Text="수정" OnClick="lnbEdit_Click">
    
    <asp:GridView ID="grvList" DataKeyNames="BoardItemID" runat="server" AutoGenerateColumns="False" >
        <Columns>
            <asp:BoundField HeaderText ="boardItemID" DataField="BoardItemID" HeaderStyle-CssClass="hidden">
                 <ItemStyle CssClass="hidden" />
            </asp:BoundField>
            <asp:BoundField HeaderText="conNo" Visible="false" DataField="indexNo" />
            <asp:TemplateField>
                 <ItemTemplate>
                     <asp:CheckBox ID="chk" runat="server"/>
                 </ItemTemplate>
            </asp:TemplateField> 
             ...
            </Columns>
    </asp:GridView>


    This is code behind page

            protected void lnbEdit_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow gRow in grvList.Rows)
                {
                    CheckBox chk = (CheckBox)gRow.FindControl("chk");
                    if (chk.Checked)
                    {
                        int boardItemID = Convert.ToInt32(gRow.Cells[0].Text);
                        string popURL = "UserBoard_Edit.aspx?BoardItemID=" + boardItemID;
                        string openPopUp = @"
                        <script type='text/javascript'>
                            popup = window.open('" + popURL + "', 'open_window', 'width = 880, height = 500, left = 0, top = 0'); popup.focus();</script>";
                        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", openPopUp);
                        //Response.Redirect("UserBoard_Edit.aspx?BoardItemID=" + boardItemID);
                    }
                    else
                    {
                        string message = @"
                        <script type='text/javascript'>
                            alert('Please check your Item');
                        </script>";
                        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", message);
                    }
                }
            }

    When I try to choose nothing, alert message showed, and  I checked checkbox and press button, also alert message showed......

    But as you can see 

    Response.Redirect("UserBoard_Edit.aspx?BoardItemID=" + boardItemID);

    When I using this code only, it works well....

    Please... somebody help us...

    Wednesday, August 24, 2016 5:55 AM

Answers

  • User-271186128 posted

    Hi SuperRyden,

    From your description, since you could get the relevant value, I suggest you could refer to the following code to open a popup window from code behind:

    protected void OpenWindow(object sender, EventArgs e)
    {
        string url = "Popup.aspx" ; //You could add parameters from here.
        string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
        ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
    }

    More details information, see: http://www.aspsnippets.com/Articles/Open-New-Window-from-Server-Side-Code-Behind-in-ASPNet-using-C-and-VBNet.aspx

    If you want to display an alert, you could use the following code:

                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('hi');", true);

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2016 6:42 AM

All replies

  • User702547207 posted

    Hi,

    I think since you are registering the script block 2 times it is causing the problem. You can have only one javascript function and pass true(checked) or false(unchecked) value and show alert if the value is false and open the popup when it is true.

    Wednesday, August 24, 2016 6:14 AM
  • User-1355475649 posted

    Thanks your comment, JLaxmi.

    It means it`s not possible using multiple ScriptBlock at same time?

    So, could you show me the examples or sources about that please.  Because i`m not good at Javascript

    Wednesday, August 24, 2016 8:18 AM
  • User702547207 posted

    Hi,

    You can try the below one and let me know.

    protected void lnbEdit_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow gRow in grvList.Rows)
                {
                    CheckBox chk = (CheckBox)gRow.FindControl("chk");
                    bool condition;
                    int boardItemID;
                    if (chk.Checked)
                    {
                        condition = true;
                        boardItemID = Convert.ToInt32(gRow.Cells[0].Text);
                    }
                    else
                    {
                        condition = false;    
                    }
                     string popURL = "UserBoard_Edit.aspx?BoardItemID=" + boardItemID;
            string openPopUp = @"<script type='text/javascript'>if (" + condition.ToString() + "){ popup = window.open('" + popURL + "', 'open_window', 'width = 880, height = 500, left = 0, top = 0'); popup.focus();}else{ alert('Please check your Item'); }</script>";
                        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", openPopUp);
                }
            }

    Wednesday, August 24, 2016 9:19 AM
  • User-1355475649 posted

    Thanks your reply but sadly, it`s not work. 

    At first. <<string popURL = "UserBoard_Edit.aspx?BoardItemID=" + boardItemID;>> - this line boardItemID didn`t get the value inside if statement,

    So I changed the code, and it got the boardItemID value, but javascript is not working. Nothing happened...

    Also I changed if (" + condition.ToString() + ") -> if('" + condition.Equals(true) +"') when I checked checkbox and press button it works well

    but when I unchecked any box, it still opened....

    it`s really confusing and now I still try to change my cord but no alert message..

    Thursday, August 25, 2016 2:45 AM
  • User-271186128 posted

    Hi SuperRyden,

    From your description, since you could get the relevant value, I suggest you could refer to the following code to open a popup window from code behind:

    protected void OpenWindow(object sender, EventArgs e)
    {
        string url = "Popup.aspx" ; //You could add parameters from here.
        string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
        ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
    }

    More details information, see: http://www.aspsnippets.com/Articles/Open-New-Window-from-Server-Side-Code-Behind-in-ASPNet-using-C-and-VBNet.aspx

    If you want to display an alert, you could use the following code:

                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('hi');", true);

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2016 6:42 AM