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.
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:
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
12 Points
30 Posts
How to open popup when check box checked in gridview?
Aug 24, 2016 05:55 AM|SuperRyden|LINK
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
This is code behind page
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
When I using this code only, it works well....
Please... somebody help us...
Member
160 Points
87 Posts
Re: How to open popup when check box checked in gridview?
Aug 24, 2016 06:14 AM|JLaxmi|LINK
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.
Member
12 Points
30 Posts
Re: How to open popup when check box checked in gridview?
Aug 24, 2016 08:18 AM|SuperRyden|LINK
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
Member
160 Points
87 Posts
Re: How to open popup when check box checked in gridview?
Aug 24, 2016 09:19 AM|JLaxmi|LINK
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);
}
}
Member
12 Points
30 Posts
Re: How to open popup when check box checked in gridview?
Aug 25, 2016 02:45 AM|SuperRyden|LINK
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..
All-Star
45489 Points
7008 Posts
Microsoft
Re: How to open popup when check box checked in gridview?
Aug 31, 2016 06:42 AM|Zhi Lv - MSFT|LINK
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:
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:
Best regards,
Dillion