Open Popup with dynamicly generated Html from CodeBehind - c#

At a certain point i have a dynamicly generated string with html in my code-behind.
I want to open a popup with has that html as its source.
I have tried the following:
I created this method on my .aspx site (Javascript):
function OpenWindowWithHtml(html, title) {
var myWindow = window.open('', title);
myWindow.document.write(html);
myWindow.focus();
}
And in the code-behind i have this:
Response.Write("OpenPopupWithHtml(\"" + html + "\", \"" + title + "\");");
But when i try to execute this i get an error.
Does anybody see, what i do wrong here?
Or does somebody know a better way to do this?

EDIT
on button click it shoudl be like this
protected void btnAbct_Click(object sender, EventArgs e) {
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "OpenPopupWithHtml('" + html + "', '" + title + "');");
}
To execute the code ie. function you written in javascript
ClientScript.RegisterStartupScript(this.GetType(),
"newWindow", "OpenPopupWithHtml('" + html + "', '" + title + "');");
you can register client script like this
if (!ClientScript.IsClientScriptBlockRegistered("exampleScript"))
ClientScript.RegisterStartupScript(this.GetType(), "exampleScript","
<script language = "'javascript'">
alert('you just registered the start up script')
</script>
");
from the code behind file of asp.net
To Open pop-Up window , just replace this line in above code
ClientScript.RegisterStartupScript(this.GetType(),
"newWindow", String.Format("<script>window.open('{0}');</script>",
"mypage.html"));
Check this for detail : Register Client script in ASP.NET

Related

JQuery to hide a button by default

I have 2 buttons which I want to hide by default on page load. On click of any row in the grid, I want these buttons to be visible. How can I hide the buttons using JQuery?
RadButton lnkAdd = new RadButton();
lnkAdd.ID = "BtnEdit";
lnkAdd.Text = "Edit";
container.Controls.Add(lnkAdd);
container.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "hideButtons",
"\n<script type=\"text/javascript\">"
//+ "\n$(function(){"
+ "\n$(document).ready(function{"
+ "\n $(\"#" + lnkAdd + "\").hide(); "
+ "\n return false;"
+ "\n });"
+ "\n</script>"
);
The following script is generated:
<SCRIPT type="text/javascript">
$(document).ready(function ruchi(){
$("#Telerik.Web.UI.RadButton").hide();
return false;
});
</SCRIPT>
But the buttons do not hide!
Shouldn't it be + "\n $(\"#" + lnkAdd.ClientID + "\").hide(); "
Right now it looks like you are just passing it the fully qualified name of the type and not the actual ID.

image button on click image change

I have an image button on my form which opens a modal popup dialog on click. The onmouseout, onmouseover, onfocus, onblur events are set in the skin file.
What I want to achieve is, when the modal popup dialog is open the 'image button' image should be like it is on onmouseover and onfocus image set in the skin.
But I am not able to achieve this by adding the following js i.e.
$("#btnAdd").src=window.location.protocol + '//' + window.location.host + '/' + 'ClientWeb/Images/Add_item_on.png';
(The above js I have written in the js function which opens up the modal dialog)
since, on the fire of onblur/onmouseout event which happens since the focus shifts to the modal dialog, the image set due to above js is lost.
What can be the possible solution for this? Please inform me since I need to integrate this asap.
skin file :
<asp:ImageButton SkinId="Add" alt="Add" runat="server" ImageUrl="~/Images/Add_item_over.png"
onmouseout="this.src=window.location.protocol + '//' + window.location.host + '/' + 'ClientWeb/Images/Add_item_over.png';"
onmouseover="this.src=window.location.protocol + '//' + window.location.host + '/' + 'ClientWeb/Images/Add_item_on.png';"
onfocus="this.src=window.location.protocol + '//' + window.location.host + '/' + 'ClientWeb/Images/Add_item_on.png';"
onblur="this.src=window.location.protocol + '//' + window.location.host + '/' + 'ClientWeb/Images/Add_item_over.png';"/>
You can't access to the property src of a jQuery element.
If you wan't access to the HTMLDOMElement, you need to write $('#btnAdd')[0].
Moreover, if your btnAdd is an <button> tag, you need to do something like:
$("#btnAdd").css({
backgroundImage: url(window.location.protocol + '//' + window.location.host + '/' + 'ClientWeb/Images/Add_item_on.png';)
});
In case of an img tag
$("#btnAdd").attr('src', window.location.protocol + '//' + window.location.host + '/' + 'ClientWeb/Images/Add_item_on.png');

How to get the id of a dynamically created html button on its click in jquery

I have several(inside loop) dynamically created html buttons like :
sb.Append("<input type=\"button\" name=\"deleteimage" + id
+ " \" id=\"btndelete" + id
+ "\" value=\"Delete\" class=\"t-button t-grid-delete\" "
+ " style=\"margin-left:10px;\" />");
and I want to get the id of the button on which I click using jquery
Help Me
Thanks
delegate using .on()(jQuery 1.7+) or .delegate()(jQuery 1.6 and lower)
$('body').on('click','input[type=button]',function(){
alert(this.id); // <-- this refers to current clicked input button
});
or
$('body').delegate('input[type=button]','click',function(){
alert(this.id);
});
Or whatever your sb element is replace body with that since it exists on dom load
How about
$('input:button').click(function(){
alert('Clicked ' + this.id);
Try this
$("input[type=button]").click(function()
{
alert(this.id);
});
You can try the following:
$(document).click(function(event) {
var target = event.target;
if ($(target).hasClass("t-grid-delete")) { // check if element has class t-grid-delete is your button
var targetId = $(target).attr("id");
// set your image id with attribute like image_id
// it take easy to get image id $(target).attr("image_id")
}
});
Use 'live' event for dynamically generated html element's event
$("input[type=button]").live('click',function()
{
alert(this.id);
});

Dynamicly creating imageButton that calls a JavaScript function in c#

I have the following c# code that creates image buttons which should call the javaScript method below
var imageButton = new ImageButton();
imageButton.ImageUrl = "Styles/unClicked.png";
imageButton.ID = "btn" + questionName;
imageButton.OnClientClick = "buttonPressed('" + questionName + "')";
Its outputting & # 3 9; instead of the ' in the html code and there for is causing an error (Ive had to add spaces tp prevent SO changing it to a comma)
onclick="buttonPressed('question0')
how to I prevent this?
Please try this one HtmlString (not working).
imageButton.OnClientClick = new HtmlString("buttonPressed('" + questionName + "')");
If it will not work, than you will need to create custom control or use javascript (JQuery) to add handler for click event.
<script>
$("<%=imageButton.ID%>").click(function() {
buttonPressed('<%=questionName%>');
})
</script>
The OnClientClick attribute is very limiting. Not totally sure this will work, but I have use similar code before to handle ghost text in textboxes...
imageButton.Attributes.Add("onclick", "buttonPressed('" + val + "')");
The attribute should not be HTML encoded.

ASP.NET - How to display javascript alert using C#?

I have a page that contains a textbox and button, when the user clicks the submit button I want to display a message based on a bool value.
I have researched on stackoverflow and tried the code in this questions: Asp.net Webform Display Alert and redirect
But it didn't work. I have the following code:
ClientScript.RegisterStartupScript(this.GetType(),"", "alert('message')", true);
What code do I need to display the alert message?
you can use this simple method:
private void MessageBox(string msg)
{
Label lbl = new Label();
lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
Page.Controls.Add(lbl);
}
Use this apporch
Response.Write(#"<script language='javascript'>alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>");

Categories

Resources