How to save tinymce using asp button - c#

I'm using asp.net and c# to build a website that can upload,edit,save the file. I have use tinymce as the texteditor but the problem is the I dint know how to configure the save plugins in tinymce..I have add asp button to use as save button but how to configure it to save into file .html?
javascript to replace tiny with texeditor:
$('#<%=btnsave.ClientID%>').show() //save button
tinymce.init({selector: "textarea"}); //replace texteditor
this is markup code:
<textarea name="textarea" cols="100" rows="5" style="visibility: hidden;">
</textarea>
<br /><asp:Button Text="Save" runat="server" ID="btnsave" style="display:none;" onclick="btnsave_Click" />
This in code behind
protected void btnsave_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/WordExcelPPointToHtml/test.html");
File.WriteAllText(path,HtmlTextArea); //this line have error!!
}
Can anyone help me on this ?. Thanks in advance. Eagerly waiting for answers.

You need to replace the html control with an ASP.NET web control, and they you will be able to access the value programmatically in the code behind.
E.g.
<asp:TextBox ID="txt" Name="textarea" Columns="100" Rows="5" TextMode="MultiLine" runat="Server"></asp:TextBox>
And in the code behind:
protected void btnsave_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/WordExcelPPointToHtml/test.html");
File.WriteAllText(path, txt.Text);
}
You should also consider some error handling in btnSave_Click

Related

Is it possible to open popup window in button click event without using jquery in asp.net C#?

Hi I want to open popup window in button click event without using j-query in asp.net C#.
This is simple task i am familiar with Asp.Net MVC. But i am not familiar in Asp.Net so only i am struggling for this simple task.
Please any one tell me how to open popup window by button click event without using jquery in asp.net C#.
My web form
SpecilalityMaster.Aspx
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="specialitymaster.aspx.cs" Inherits="popupwindow.admin.specialitymaster" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<form id="frmPopupExample" runat="server">
<asp:Button ID="btnOpenPopupWindow"
runat="server"
Text="Open Popup Window" OnClick="btnOpenPopupWindow_Click"
/>
</form>
<div class="popup">
<asp:TextBox ID="_lname" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="save" OnClick="Button1_Click"
/>
</div>
</asp:Content>
specialitymaster.aspx.cs
public partial class specialitymaster : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOpenPopupWindow_Click(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
i just tried simply. i designed one button and one popup window.if i click open popup window in button click event i need to open popup window. i donno it is possible or not.i created button click event in CS page but i donno how to call that popup window name in button click event .
example
Desirable result: Please see image.
If I understand you correctly, you don't need a popup, you just trying to activate some div on your page. You will need to make this div server side
<div class="popup" runat="server" style="display:none" id="divPop">
<asp:TextBox ID="_lname" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="save" OnClick="Button1_Click"
/>
</div>
and then in your server side click
protected void btnOpenPopupWindow_Click(object sender, EventArgs e)
{
divPop.Attributes.Add("style", "display:block);
}
this will make your div visible
EDIT
better solution in your case would be using asp panel instead of div.
<asp:panel id="divPop" visible="false" runat="server">
<asp:TextBox ID="_lname" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server"
Text="save" OnClick="Button1_Click"
/>
</asp:panel>
and then you will be able to manipulate visibility in your server side code very easy
protected void btnOpenPopupWindow_Click(object sender, EventArgs e)
{
divPop.Visible=true;
}
you are using server side control in your code.
<asp:Button ID="btnOpenPopupWindow"
runat="server"
Text="Open Popup Window" OnClick="btnOpenPopupWindow_Click"
/>
click of this button will trigger immediate post to the server and server (your ASP.NET application) will handle the event. The only way you will be able to open a popup from the server if you will register script to do this. Your CLick should look something like
protected void btnOpenPopupWindow_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(),
"yourWindowName",
String.Format("<script>window.open('{0}');</script>", yourURL));
}
Use this:
var DefineWindowSize= "location=yes,height=570,width=520,scrollbars=yes,status=yes";
var URL = "https://www.google.com";
var win = window.open(URL, "_blank", DefineWindowSize);
Create a function and add it inside the function.

All control are fired in asp.net

since I am new to ASP .NET I wonder whether this is normal behavior or something strange. I am trying to convert C# WinForms app to web app and it is behaving not as I expected.
In desktop app I get data after user press enter on textbox. Data are loaded I can modify it and then, when I am done changing data I can press one of two buttons and data are saved to database or form is cleared. Something like this.
private int idOrder;
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(load_data)
{
label1.Text = "some loaded data";
...
else{ MessageBox.Show("Something went wrong. No data loaded");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//save to database;
}
But When I try to do the same in ASP.NET Web form, whenever I press enter on textbox all controls are fired (as it is written in source code line after line) when page is refreshed. Could you please advice we what should I do to replicate same behavior as in winforms e.g. user input - enter - load data - modify - press button when I want. Thank you.
//Is the view state thing I am looking for?
Here is a snippet to get you started. Keep in mind that posting a HTML form when pressing enter is default behavior for websites. So the first thing is preventing that by wrapping the TextBox and Submit Button with a Panel with a DefaultButton assigned to it. Now when the enter is pressed in TextBox1, it will post the form as dummyButton
Next we add an OnTextChanged event to TextBox1 and set AutoPostBack to true. This will fire when the focus of TextBox1 is lost. You could remove this and just use the enter with dummyButton.
The dummyButton is added to ensure the form will PostBack when enter is pressed in TextBox1 to get the desired behaviour.
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:LinkButton ID="dummyButton" runat="server" OnClick="TextBox1_TextChanged"></asp:LinkButton>
</asp:Panel>
And then in code behind.
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
//load from database
Label1.Text = TextBox1.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
//save to database
Label1.Text = "Data saved!";
TextBox1.Text = "";
}
You can also prevent PostBack with enter like this. The JavaScript will block all enters. Depending on your page design you could set this in the <body> tag for example.
<div onkeydown="return (event.keyCode!=13);">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
</div>

How to trigger c# method from anchor tag in asp.net

I have an anchor tag in .aspx page as below:
Click
I want to tigger c# method using this tag.
protected void divrecentQInit(object sender, EventArgs e)
{
log.Debug("divrecentQInit is called");
}
Problem is: divrecentQInit method is not getting called.
Thanks for help.
Not: I do not want to use LinkButton of asp.net since it doesnt work very well with bootstrap.
The Method call event(onServerClick) is case-sensitive, Use
onServerClick="divrecentQInit"
instead of
onserverclick="divrecentQInit"
and additionally I would suggest using <asp:LinkButton to achieve the same(you would not need onServerClick in this case, OnClick instead )
<asp:LinkButton runat="server" OnClick="divrecentQInit" />
Use asp:LinkButton
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
In your code behind:
public void LinkButton_Click(Object sender, EventArgs e)
{
//stuff.
}

Displaying a div on a button click in asp.net

I want to display a particular div on the click of a button
My asp code is as follows
<asp:Button ID="Button1" class="btn" runat="server" OnClick="Submit" />
<div runat="server" id="signup" Visible="false">Some Content </div>
Now in the CodeBehind i have written the following code
protected void Submit(object sender, EventArgs e)
{
Button1.Visible = false;
signup.Visible = true;
}
But every time i get the error as
the name signup does't exist in the current context
i am not able to figure out the problem in the code..
You probably have wrong html for the div, you can also use style="visible:none" instead of Visible="false"
Change
visible:"false"
To
Visible="false"
This has to be done through JavaScript, I believe in the onClientClick attribute of the button. I'm currently searching for the code to jog my own

File Upload Issue - how to fix?

I'm trying to upload file in tow scenarios
First:
<input id="File2" runat="server" name="name" type="file" clientidmode="Static" />
<asp:Button ID="Button4" runat="server" clientidmode="Static"
Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />
This works correctly and the postedFile not null in code behind C#
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile postedFile= Request.Files[0];
}
Second:
I want to change the browse button "text" and I know already , can't do that directly so i worked around it like that:
<b>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent" >
<input id="File1" runat="server" name="name" type="file" clientidmode="Static" onchange="setHiddenValue()" style=" visibility:hidden;" />
<br />
<input id="Button2" type="button" clientidmode="Static" onclick="triggerFileUpload()" value="HTML Button" />
<br />
<asp:Button ID="Button3" runat="server" clientidmode="Static" Text="Go CodeBehind To Get Input Value" OnClick="btnUploadClick" />
<script language="javascript">
function triggerFileUpload() {
document.getElementById("File1").click();
}
</script>
</asp:Content>
<b>
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile postedFile= Request.Files[0];
}
When I press the Button2 the fileDialog open, I select file and everything Ok.
But when I press Button3 to get file in server side c# the Request.Files[0] is null
and Found no file posted.
I want the Request.Files[0] because I want save it in database as byte
so please if u have any idea I'll appreciate it
Thank You in advance
I did the same code (second scenario) in a blank project, and this works fine. You could do the same and check it.
Probably something around this slice of code is breaking this. Check if masterpage or current page Page_Load() method have something that break the file 'postback'. You can try:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Page_Load code
}
}

Categories

Resources