I need to call a JavaScript method with parameters from the code behind.
Javascript method
<script type="text/javascript">
function changeControlSample(path)
{
$find('<%= PartialUpdatePanel7.ClientID %>').set_UserControlPath(path);
$find('<%= PartialUpdatePanel7.ClientID %>').refresh();
}
</script>
<iucon:PartialUpdatePanel runat="server" ID="PartialUpdatePanel7"
DisplayLoadingAfter="500" InitialRenderBehaviour="Clientside" EncryptUserControlPath="false">
<LoadingTemplate>
<div style="margin-left: 84px; margin-top: 10px;">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/loading.gif" />
</div>
<div style="text-align: center">
Updating...
</div>
</LoadingTemplate>
</iucon:PartialUpdatePanel>
The code Behind of the page
protected Consultation controlconsultation = new Consultation();
protected void Page_Load(object sender, EventArgs e)
{
PartialUpdatePanel7.UserControlPath = "Espace_Candidat/Consultation.ascx";
controlconsultation.imageinfo += controlconsultation_imageinfo;
Session["controlconsultation"] = controlconsultation;
}
void controlconsultation_imageinfo(object sender, CommandEventArgs e)
{
PartialUpdatePanel7.UserControlPath = "Espace_Candidat/InfoEdition.ascx";
Page.ClientScript.RegisterStartupScript(this.GetType(),
"CallMyFunction",
"changeControlSample('Espace_Candidat/InfoEdition.ascx')", true);
}
Code behind of the user control
public event CommandEventHandler imageinfo ;
protected void Page_Load(object sender, EventArgs e)
{
Consultation current = (Consultation)Session["controlconsultation"];
imageinfo = current.imageinfo;
}
protected void Valider (object sender, CommandEventArgs e)
{
if (imageinfo != null)
{
string pageNumber = (string)e.CommandArgument;
CommandEventArgs args = new CommandEventArgs("Control", pageNumber);
imageinfo(this, args);
}
}
This call didn't work even I change the JavaScript method by another one.
For example, if I try
Page.ClientScript.RegisterStartupScript
(this.GetType(),
"CallMyFunction",
"alert('blabla');",
true);
I got the same result.
So, What is the error that I commited?
How can I fix my code?
If you have update panel in page then call like this,
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), Guid.NewGuid().ToString(), #"<script type='text/javascript'>changeControlSample('" + path + "');</script>", false);
It don't have update panel then call like this
Page.ClientScript.RegisterStartupScript(this.GetType(), "tabselect", "<script type='text/javascript'>changeControlSample("' + path + '");</script>");
If you wish to call JavaScript method with parameters from the code behind you can get this done using
ClientScriptManager.RegisterStartupScript Method
please check the link given below:
http://msdn.microsoft.com/en-us/library/z9h4dk8y(v=vs.110).aspx
Hope this helps.
Related
I have an html list to which I am trying to add html button to the property "innerhtml" using the code behind file. I am able to add the button however the onclick event is not getting fired
HTML:
<ul runat="server" id="list">
<li></li>
<li><a id="loginID" runat="server" href="../Accounts/login.aspx">Login</a></li>
<li id="logout" runat="server"> Register</li>
</ul>
code behind:
logout.InnerHtml = "<input type=\"submit\" id=\"b1\" runat=\"server\" onserverclick=\"b1_click\" />"; //logout is the id of the list item
}
}
public void b1_click(Object sender,EventArgs e)
{
Response.Redirect("~/Accounts/login.aspx");
}
}
This isn't the right way to dynamically create server-side controls.
Please review the example below to get a better understanding of how to do so. Keep in mind you can not assign the ClientID property as it is read only and initialized during runtime.
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Dynamic Button";
btn.ID = "b1"; // Server Side ID
btn.Click += b1_click;
logout.Controls.Add(btn);
}
public void b1_click(Object sender,EventArgs e)
{
Response.Redirect("~/Accounts/login.aspx");
}
I have included a div in my web page using HtmlGenericControl.
With a button click , i want to add text inside the div using HttpContext. I don't want to use InnerHtml because the browser hangs when the text i want to include is very long.
I tried the following way but it prints the text outside the div.
Please Help.
Thanks!
public partial class TextViewer : System.Web.UI.Page
{
public HttpContext ctx;
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl myDiv = new HtmlGenericControl("div");
myDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Blue");
this.Controls.Add(myDiv);
ctx = this.Context;
}
protected void Button1_Click(object sender, EventArgs e)
{
ctx.Response.Write("supposed to be printed inside myDiv.");
}
}
You can write to the HttpContext from code behind and access its value from jQuery.
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl myDiv = new HtmlGenericControl("div");
myDiv.ID = "myDiv";
myDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Blue");
this.Controls.Add(myDiv);
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpContext.Current.Application["MyData"] = "Supposed to be printed inside myDiv.";
}
.ASPX:
<head runat="server">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var data = '<%= HttpContext.Current.Application["MyData"] != null ? HttpContext.Current.Application["MyData"].ToString() : "" %>';
$("#myDiv").append(data);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
In such case, you can split your text into many spans and set each span's text separately
foreach(var paragraph in myText)
{
var span = new HtmlGenericControl("span");
span.InnerHtml = paragraph;
myDiv.Controls.Add(span);
}
Nothing works, The button doesn't trigger the event
code of button:
<input type="button" id="regBtn" runat="server" onserverclick="RegClick"/>
script code:
<script runat="server">
void RegClick(object sender, EventArgs e)
{
regBtn.Value += "a";
}
</script>
If you use ASP.Net Web Form, you want to use Service Control whenever posting back to server. It will go through Life Cycle Events, and trigger the appropriate event correctly.
<asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click"/>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
SubmitButton.Text += "a";
}
let say I enter a code into textbox 100 but into label Pizza should be texted in label
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtMain_TextChanged(object sender, EventArgs e)
{
lblmain.text = txtcat.text;
}
The OnTextChanged is a server side event . It would be better to use JavaScript for this purpose like this:
<script type="text/javascript">
function change() {
document.getElementById('<%= lblmain.ClientID %>').innerHTML = document.getElementById('<%= txtMain.ClientID %>').value;
}
</script>
And then in your TextBox call to change function like this:
<asp:TextBox ID="txtMain" runat="server" onkeydown="change();"></asp:TextBox>
<asp:Label ID="lblmain" runat="server" Text="Label"></asp:Label>
I am using an asp.net UpdatePanel that contains Button 1. On Button1 click, a string is created in code behind then sent back to the updatePanel. That string contains Button2 that should fire a postback to execute Button2_Click function. Unfortunately I haven't been able to execute a full postback on button 2 yet.
If I add Button2 as a PostBackTrigger control in the UpdatePanel on my aspx page I get an error that the control (Button2) is not found. This makes sense cause Buton2 is created by clicking Button1 in code behind and is not available on page load.
I'm trying to RegisterPostBackControl(Button2) on page load but again I get an error that Button2 doesn't exist in the current context.
Any help would be highly appreciated.
ASPX page
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<div runat="server" id="DIV1">Testing...<br /></div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" clientidmode="static"/>
<div runat="server" id="DIV2"></div>
<asp:Label ID="DIV2_LABEL" runat="server"></asp:Label>
</fieldset>
</ContentTemplate>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(Page).RegisterPostBackControl(Button2);
}
protected void Button1_Click(object sender, EventArgs e)
{
string DIV1_Html;
DIV1_Html= DIV1.InnerHtml;
DIV2.InnerHtml = DIV1_Html;
DIV2.InnerHtml += "<input id='Button2' type='button' value='button' runat='server' OnClick='Button2_Click' />";
}
protected void Button2_Click(object sender, EventArgs e)
{
DIV2_LABEL.Text = "Testing button2";
}
EDIT
Based on the advice below, I've added the button as follows:
Instead of:
DIV2.InnerHtml += "<input id='Button2' type='button' value='button' runat='server' OnClick='Button2_Click' />";
I've amended the code as follows:
Button Button2 = new Button();
Button2.Text = "Add";
Button2.Click += new EventHandler(Button2_Click);
PostBackTrigger pTrigger = new System.Web.UI.PostBackTrigger() { ControlID = Button2.ID };
UpdatePanel1.Triggers.Add(pTrigger);
DIV2.Controls.Add(Button2);
BUT I'm still missing something because Button2_Click function is not fired when I click on the new button with NO error. ?????
Since your Button2 is created in client-side, It's ID is not recognized by the updatePanel1.
You can create Button2 dynamically like this:
Button button2 = new Button();
PostBackTrigger pTrigger = new System.Web.UI.PostBackTrigger() { ControlID = button2.ID };
updatePanel1.Triggers.Add(pTrigger);
DIV2.Controls.Add(button2);
UPDATE: Here is a little sample that you can use:
public partial class WebForm1 : System.Web.UI.Page
{
private bool ShowButton2()
{
return ViewState["createButton"] == null ? false : (bool)ViewState["createButton"];
}
protected void Page_Load(object sender, EventArgs e)
{
if (ShowButton2())
CreateButton();
}
void CreateButton()
{
Button Button2 = new Button();
Button2.Text = "Add";
Button2.ID = "button2";
Button2.Click += Button2_Click;
PostBackTrigger pTrigger = new System.Web.UI.PostBackTrigger() { ControlID = Button2.ID };
updatePanel1.Triggers.Add(pTrigger);
DIV2.Controls.Add(Button2);
}
void Button2_Click(object sender, EventArgs e)
{
}
protected void button1_Click(object sender, EventArgs e)
{
if (!ShowButton2())
{
ViewState["createButton"] = true;
CreateButton();
}
}
}
The view state is a flag for showing Button2 of course.
There is a property on the button called OnClientClick, you can use that to call a javascript which then navigate to your page.
Example:
In your apsx page
<script type="javascript">
function reloadPage() {
window.location.href = '...';
}
In your code behind
DIV2.InnerHtml += "<input id='Button2' type='button' value='button' OnClick='ReloadPage()' />";