I'm just starting to get to grips with web development and trying to build up some experience.
I am implementing the HtmlEditorExtender in my website. I have added all the files and references necessary to use this control and I've got the control displaying correctly. The problem I'm having is I don't seem to be able to get the content of the textbox after making changes.
I have attached the control to a textbox, then populated the textbox with the content I wish to edit. Once I've made changes I have a save button that will save the current content in the HTML editor. What I'm seeing is that the Text property of the Textbox is exactly the same as before I made the changes. Is there something obvious that I'm missing.
Code is below:
Markup in UserControl:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel ID="pnlPopup" runat="server" Style="display: none" CssClass="modalPopup">
<asp:Panel ID="Panel3" runat="server" Style="background-color: #DDDDDD; border: solid 1px Gray;
color: Black;">
<p>
Edit:
</p>
</asp:Panel>
<asp:TextBox runat="server" ID="txtHTMLContent" CssClass="WhiteTextBox" TextMode="MultiLine"
Columns="50" Rows="10" />
<br />
<ajaxToolkit:HtmlEditorExtender ID="htmlEditor" TargetControlID="txtHTMLContent" Runat="server" EnableSanitization="false" />
<center>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</center>
</asp:Panel>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
string content = GetContent();
txtHTMLContent.Text = content;
}
protected void btnSave_Click(object sender, EventArgs e)
{
DatabaseManager dm = new DatabaseManager();
dm.UpdateContent(txtHTMLContent.Text);
}
I would appreciate any help.
wrap txtHTMLContent initialization code in the Page_Load method in if(!IsPostback) check:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string content = GetContent();
txtHTMLContent.Text = content;
}
}
Related
I have tried to look through many questions about the dropdownlist and still yet to resolve my issue.
I have a Dropdownlist and a Submit button. When Submit button is clicked after value selected, it should update the Label text with the selected value of the Dropdownlist. However, what happened was each time value is selected from dropdown, it refreshes the page and the value captured was the first item in dropdownlist. How do I capture the selected value correctly before it refreshes?
Am at wits end, not sure where went wrong.
*All items in dropdownlist are unique. No duplication.
My codes:
ASP.NET
<form id="form1" runat="server">
<asp:DropDownList ID="ddlCode" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCode_SelectedIndexChanged"/>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
<asp:UpdateProgress ID="updProgress" AssociatedUpdatePanelID="UpdatePanel1" runat="server">
<ProgressTemplate>
<img alt="progress" src="img/loading.gif"/><br /><h2>Loading...</h2>
</ProgressTemplate>
</asp:UpdateProgress>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Button ID="submitBtn" runat="server" cssclass="btn btn-success" OnClick="submitBtn_Click" Text="Submit"/>
<asp:Label ID="lblCode" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCode" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
</form>
Code behind
string ddlSelectedIndex = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//This is to load code into dropdownlist which works fine.
LoadCode();
}
}
protected void ddlCode_SelectedIndexChanged(object sender, EventArgs e)
{
ddlSelectedIndex =(ddlCode.Items[ddlCode.SelectedIndex].Text).Substring(0, 4);
}
protected void submitBtn_Click(object sender, EventArgs e)
{
lblCode.Text = ddlSelectedIndex;
}
Try it without the "AutoPostBack=True" on the DropDownList... the way your code is written, it causes the page to refresh any time the DropDownList value changes. What you want is a postback when submit is clicked.
I am currently using a multiview control within a web forms user control page, navigating between views, our data does not persist.
Three view, last view contains a summary of what was entered within the first two view, If I click through to the summary view on ly data from the seconds view is available.
Clicking back and forth between views, our form data is lost.
Any ideas where to begin?
.ascx Page
<asp:MultiView ID="MultiView1" ActiveViewIndex="0" EnableViewState="true" runat="server">
<asp:View ID="View1" runat="server">
Name:<br />
<asp:TextBox ID="txtName" runat="server" />
</asp:View>
<asp:View ID="View2" runat="server">
Surname:<br />
<asp:TextBox ID="txtSurname" runat="server" />
</asp:View>
<asp:View ID="View3" runat="server">
<b>Summary</b><br />
Name:
<asp:Label ID="lblName" runat="server" /><br />
Surname:
<asp:Label ID="lblSurname" runat="server" /><br />
</asp:View>
</asp:MultiView>
<asp:Button ID="btnBack" runat="server" Text="< Back " OnClick="btnBack_Click" />
<asp:Button ID="btnNext" runat="server" Text="Next >" OnClick="btnNext_Click" />
<asp:Button ID="btnSend" runat="server" Text="Submit" OnClick="btnSend_Click" />
ascx.cs Codebehind
protected void btnBack_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex--;
}
protected void btnNext_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex++;
}
protected void btnSend_Click(object sender, EventArgs e)
{
}
protected override void OnPreRender(EventArgs e)
{
if (MultiView1.ActiveViewIndex == MultiView1.Views.Count - 1)
{
FillSummary();
}
btnBack.Visible = MultiView1.ActiveViewIndex > 0;
btnNext.Visible = MultiView1.ActiveViewIndex < MultiView1.Views.Count - 1;
btnSend.Visible = MultiView1.ActiveViewIndex == MultiView1.Views.Count - 1;
base.OnPreRender(e);
}
private void FillSummary()
{
lblName.Text = txtName.Text;
lblSurname.Text = txtSurname.Text;
}
When ever i hide or show the Panel, the items i add (dynamically) to the Table are gone, and another thing is that when i try add new Row to the Table, it won't show it, it's overwriting the same Row, why ??.
Here's the CodeBehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
DropDownList1.Items.Add(TextBox3.Text);
}
protected void Button3_Click(object sender, EventArgs e)
{
if (Panel1.Visible == true)
Panel1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Panel1.Visible == false)
Panel1.Visible = true;
}
protected void Button4_Click(object sender, EventArgs e)
{
TableCell tdStudentName = new TableCell();
tdStudentName.Text = TextBox1.Text;
TableCell tdStudentCourse = new TableCell();
tdStudentCourse.Text = DropDownList1.SelectedValue;
TableCell tdStudentGrade = new TableCell();
tdStudentGrade.Text = TextBox2.Text;
TableRow tr = new TableRow();
tr.Cells.Add(tdStudentName);
tr.Cells.Add(tdStudentCourse);
tr.Cells.Add(tdStudentGrade);
Table1.Rows.Add(tr);
}
}
}
Here's the Html Source:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Student Name:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Course:"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Course" />
<br />
<asp:Label ID="Label3" runat="server" Text="Grade:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Add" />
<br />
<br />
<asp:Panel ID="Panel1" runat="server" GroupingText="Course Settings" Visible="False">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Add" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Close" />
</asp:Panel>
<br />
<br />
<asp:Table ID="Table1" runat="server" BorderStyle="Solid">
<asp:TableRow runat="server">
<asp:TableCell runat="server">Name</asp:TableCell>
<asp:TableCell runat="server">Course</asp:TableCell>
<asp:TableCell runat="server">Grade</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</form>
</body>
</html>
Here's a preview of how it looks like:
If you add anything dynamically, then you must re-add it on any subsequent post-back. They will not automatically just "be there" on the next post-back.
This requires you to store the fact that the dynamic controls have been created and added, and therefore allows you to detect that on the post-back and add the controls back in again.
It is better (if possible) to add dynamic controls as part of the init stage of the life cycle, as this will mean they will pick up view state information before the load stage.
Edit
In response to the comment by the OP...
Where do i store these controls ? and where do i add them back ? inside Page_Load ?
You don't store the controls... you store the fact that the controls have been added.
The "easiest" way would be to use the ViewState, but that puts you in a catch-22 situation... because the ViewState is not available at the init stage of the life cycle, which is the preferred place to re-add those controls.
Another option would be to "store the fact" within an <asp:HiddenField> which you can check for within the init stage. However you would have to get the value directly from the Request object, as the field would not have the correct .Value at the init stage. You'd do that like...
if (!Page.IsPostBack)
{
string hdnValue = Request[hdnFieldCtrl.UniqueId];
// Recreate controls based on the details you need
}
I am facing a problem. I place a control ascx at aspx page. At aspx page i am using UpdatePanel. And at ascx page i use a Formview. In Formview <InsertItemTemplate> i use a control asp:FileUpload for File upload. After Selecting file, when i check FileUpload.HasFile it gives me false always. I try to fire <Triggers> but not
success because file upload is on my ascx page. In below example i am showing my problem parts.
code
FileUpload _fileUpload = FormView1.FindControl("FileUpload1") as FileUpload;
if (_fileUpload != null && _fileUpload.HasFile)
{
/// some code i write here
}
ASPX
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
<asp:PostBackTrigger ControlID="imagAddNew" />
<asp:AsyncPostBackTrigger ControlID="EditProduct1" />
<asp:PostBackTrigger ControlID="ImageButton1" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnl_grid" Style="width: 100%; overflow: auto;" runat="server">
<uc1:EditProduct ID="EditProduct1" runat="server" />
</asp:Panel>
ASCX
<asp:FormView ID="FormView1" runat="server" Width="100%" ondatabinding="FormView1_DataBinding">
<InsertItemTemplate>
<table>
<tr>
<td class="label-col">
Image
</td>
<td class="data-col">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True" CommandName="Insert"
ImageUrl="~/images/save.gif" ValidationGroup="Inser" />
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FromView>
try this 100% working
Here Your FileUpload Control is Nested Inside the FormView so i find that control ImageButton and then added PostBackTrigger to that control. So you can Apply Same logic to GridView,Repeater,DataList,etc and your can use either ToolScriptManager
or ScriptManager to Register a PostBackControl
use Image Button like this
<asp:ImageButton ID="ImageButton1" runat="server"
OnClick="ImageButton1_OnClick" CausesValidation="True" CommandName="Insert"
ImageUrl="~/images/save.gif" ValidationGroup="Inser" />
in Your .aspx page where your are importing user control on page load use this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FormView ProductsFormView = (FormView)EditProduct1.FindControl("ProductsFormView");
FindAllTextBoxes(ProductsFormView);
}
}
private void FindAllTextBoxes(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType().ToString() == "System.Web.UI.WebControls.ImageButton")
{
ImageButton ImageButton1 = (ImageButton)c.FindControl("ImageButton1");
if (ImageButton1 != null)
{
ToolScriptManager1.RegisterPostBackControl(ImageButton1);
//or ScriptManager.RegisterPostBackControl(ImageButton1);
}
}
if (c.Controls.Count > 0)
{
FindAllTextBoxes(c);
}
}
}
protected void ImageButton1_OnClick(object sender, EventArgs e)
{
ImageButton ImageButton1 = (ImageButton)sender;
FormViewRow row = (FormViewRow)ImageButton1.Parent.Parent;
FileUpload FileUpload1 = (FileUpload)row.FindControl("FileUpload1");
if (FileUpload1.HasFile)
{
}
}
if You are using ContentPlace Holder with Master page then
ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
ToolkitScriptManager ToolScriptManager1 = (ToolkitScriptManager)ContentPlaceHolder1.FindControl("ToolScriptManager1");
ToolScriptManager1.RegisterPostBackControl(ImageButton1);
What is analog in C#?
js:
$('#MyDiv').show();
var d = document.getElementById('MyDiv');
d.innerHTML = someLargeHtmlCode;
html:
<div id="MyDiv" runat="server" style="display: none;">
</div>
Can I use ?
Some like that:
html:
<asp:Panel id="MyDiv" runat="server" style="display: none;">
</asp:Panel>
But I don't know what C# code must be.
<asp:Panel id="MyDiv" runat="server" Visible="false">
<asp:Literal ID="literal" runat="server" />
</asp:Panel>
and in your code behind:
protected void Page_Load(object sender, EventArgs e)
{
MyDiv.Visible = true;
literal.Text = "Hello";
}