Items in Table are gone after a Postback - c#

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
}

Related

How do i pass a command argument of a button in Data List to another page?

Actually i am trying to redirect command argument of a button present in a data list to another page. I am using Request.QueryString method to access the command argument on another page with the help of command name of the button. Please help me with it...
this is code of button present inside Data List
<asp:Button ID="Button1" runat="server" Text="Read" CommandArgument='<%# Eval("id")%>' OnClick="Button1_Click" CommandName="content"/>
this is code present in DataList Item command function
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
Response.Redirect("content.aspx?content=" +e.CommandArgument.ToString());
}
this is the onclick function code
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("content.aspx");
}
this is the code on another page(content.aspx)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String id = Request.QueryString["content"];
Label1.Text = id;
}
}
this is entire datalist code
<asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource1" Height="657px" RepeatColumns="4" RepeatDirection="Horizontal" Width="1248px" OnItemCommand="DataList1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<ItemStyle ForeColor="#000066" />
<ItemTemplate>
<table class="auto-style2">
<tr>
<td style="text-align: center">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Id") %>' Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:Image ID="Image2" runat="server" Height="250px" ImageUrl='<%# Eval("image") %>' Width="250px" />
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<br />
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="addtofav" CommandArgument='<%# Eval("id")%>' Height="30px" Width="20px" />
</td>
</tr>
<tr>
<td style="text-align: center">
<asp:Button ID="Button1" runat="server" Text="Read" CommandArgument='<%# Eval("id")%>' OnClick="Button1_Click" CommandName="content"/>
</td>
</tr>
</table
<br />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
it does redirect to another page(content.aspx) but the label does not show the querystring text.
Try updating the DataList1_ItemCommand event to this:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "content")
{
Response.Redirect("content.aspx?content=" +e.CommandArgument.ToString());
}
}
also make sure you are checking IsPostBack in Page_Load method of this page code behind.
Yes i got the desired output. Actually i was also using another button to redirect to a diiferent page in DataList1_ItemCommand function. So i just needed to seperate the Response.redirects of the 2 buttons by putting them in if loop.
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "content")
{
Response.Redirect("content.aspx?content="+e.CommandArgument.ToString());
}
if (e.CommandName == "addtofav")
{
Response.Redirect("sortbyAZ.aspx?addtofav=" + e.CommandArgument.ToString());
}
}
Thanks You everybody for helping me out with this one
I think you are not casting the Request.QueryString["content"] to string format, try this
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String id = Request.QueryString["content"];
Label1.Text = id;
}
}
You can find all information you need in this tutorial. from #microsoft msdn
Best of luck
<%# Page Language="C#" AutoEventWireup="True" %>
<%# Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>DataList Select Example</title>
<script runat="server">
ICollection CreateDataSource()
{
// Create sample data for the DataList control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("Item", typeof(Int32)));
dt.Columns.Add(new DataColumn("Qty", typeof(Int32)));
dt.Columns.Add(new DataColumn("Price", typeof(double)));
// Populate the table with sample values.
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = i * 2;
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsList.DataSource = CreateDataSource();
ItemsList.DataBind();
}
}
void Item_Command(Object sender, DataListCommandEventArgs e)
{
// Set the SelectedIndex property to select an item in the DataList.
ItemsList.SelectedIndex = e.Item.ItemIndex;
// Rebind the data source to the DataList to refresh the control.
ItemsList.DataSource = CreateDataSource();
ItemsList.DataBind();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>DataList Select Example</h3>
Click <b>Select</b> to select an item.
<br /><br />
<asp:DataList id="ItemsList"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
OnItemCommand="Item_Command"
runat="server">
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>
<SelectedItemStyle BackColor="Yellow">
</SelectedItemStyle>
<HeaderTemplate>
Items
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton id="SelectButton"
Text="Select"
CommandName="Select"
runat="server"/>
Item <%# DataBinder.Eval(Container.DataItem, "Item") %>
</ItemTemplate>
<SelectedItemTemplate>
Item:
<asp:Label id="ItemLabel"
Text='<%# DataBinder.Eval(Container.DataItem, "Item") %>'
runat="server"/>
<br />
Quantity:
<asp:Label id="QtyLabel"
Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>'
runat="server"/>
<br />
Price:
<asp:Label id="PriceLabel"
Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}")
%>'
runat="server"/>
</SelectedItemTemplate>
</asp:DataList>
</form>
</body>
</html>
By the way I think you don't need define Button Click event. Already defined path with variable. Just convert button to Link Button and remove Button Click Event

Ajax UpdatePanel inside repeater is missing post back

I can't do post back of the UpdatePanel inside the repeater.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Repeater ID="Posts" runat="server" OnItemCreated="Posts_OnItemCreated">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Label ID="lblComentarios" runat="server" Text='<%# Eval("LabelComentarios") %>'></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
<asp:TextBox ID="txtComentario" runat="server" CssClass='<%# Eval("PostId") %>'></asp:TextBox>
<asp:Button ID="Button1" ClientIDMode="Static" runat="server" OnCommand="btnComentar_OnCommand" CommandName='<%# Eval("PostId") %>' class='<%# Eval("PostId") %>' />
</ItemTemplate>
</asp:Repeater>
Code for repeater OnItemCreated():
protected void Posts_OnItemCreated(object sender, RepeaterItemEventArgs e)
{
var control = e.Item.FindControl("Button1");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}
Then in my codebehind OnCommand ..
protected void btnComentar_OnCommand(object sender, CommandEventArgs e)
{
MyCode();
foreach (RepeaterItem item in Posts.Items)
{
var panel = (UpdatePanel)item.FindControl("UpdatePanel1");
panel.Update();
}
}
everything is doing what it should except panel.update().
I have done below code and its updating test values to label
ASPX:
<%# Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Test.aspx.cs" Inherits="WebApplication1.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Repeater ID="Posts" runat="server" OnItemCreated="Posts_OnItemCreated">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblComentarios" runat="server" Text='<%# Eval("LabelComentarios") %>'></asp:Label>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("LabelComentarios") %>'></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:TextBox ID="txtComentario" runat="server" CssClass='<%# Eval("PostId") %>'></asp:TextBox>
<asp:Button ID="Button1" ClientIDMode="Static" runat="server" OnCommand="btnComentar_OnCommand" CommandName='<%# Eval("PostId") %>' class='<%# Eval("PostId") %>' />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("LabelComentarios");
dt.Columns.Add("PostId");
dt.Rows.Add("esresrer");
dt.Rows.Add("esresrer");
dt.Rows.Add("esresrer");
Posts.DataSource = dt;
Posts.DataBind();
}
}
protected void Posts_OnItemCreated(object sender, RepeaterItemEventArgs e)
{
var control = e.Item.FindControl("Button1");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}
protected void btnComentar_OnCommand(object sender, CommandEventArgs e)
{
//MyCode();
foreach (RepeaterItem item in Posts.Items)
{
var lbl = (Label)item.FindControl("Label1");
lbl.Text = "testest";
var panel = (UpdatePanel)item.FindControl("UpdatePanel1");
panel.Update();
}
}
}
}
Also check if !IsPostback proper or not

Static DropDownList in DataGrid returns wrong selected value after button click event C# ASP.NET

I want to grab the selected value from the DropDownLists in my DataGrid when a submit button is clicked, but they always return the first option in the dropdown (approve). How can I get the selected value when using static dropdown items like this?
.aspx code:
<!-- several BoundColumns were here -->
<asp:TemplateColumn HeaderText="Actions">
<HeaderStyle CssClass="ProfDataGridHeader" BorderStyle="Solid" BorderWidth="1"></HeaderStyle>
<ItemStyle Width="45%" CssClass="ProfDataGridRow" BorderStyle="Solid" BorderWidth="1"></ItemStyle>
<ItemTemplate>
<asp:DropDownList ID="ddlApprovalStatus" AppendDataBoundItems="True" runat="server" Width="150px" EnableViewState="true" ViewStateMode="Enabled">
<asp:ListItem Value="approve" Text="Approve"></asp:ListItem>
<asp:ListItem Value="reject" Text="Reject"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" CssClass="ally-btn" OnClick="btnSubmit_Click" />
.aspx.cs code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
DropDownList DDLP;
string acceptStatus;
debugLabel.Text = "";
for (int i = 0; i < dgApprovals.Items.Count; i++)
{
DDLP = (DropDownList)dgApprovals.Items[i].FindControl("ddlApprovalStatus");
acceptStatus = DDLP.SelectedValue;
debugLabel.Text += acceptStatus + ", ";
}
}
The debugLabel.Text always ends up being "accept, accept, accept..." even when the DropDownLists have "Reject" selected.
Reproduced and fixed your problem by handling post-back events.
default.aspx.cs
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace DropdownClicks
{
public partial class WebForm1 : System.Web.UI.Page
{
static List<string> itemsToInsert = new List<string> { "first", "second", "third" };
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//only do this binding on page load, otherwise you'll "reset" the grid every time there is a postBack.
mydg.DataSource = itemsToInsert;
mydg.DataBind();
}
}
protected void Unnamed_Click(object sender, EventArgs e)
{
DropDownList DDLP;
string acceptStatus;
string retVal = "";
for (int i = 0; i < mydg.Items.Count; i++)
{
DDLP = (DropDownList)mydg.Items[i].FindControl("ddlApprovalStatus");
acceptStatus = DDLP.SelectedValue;
retVal += acceptStatus + ", ";
}
lbl_1.Text = retVal;
}
}
}
default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DropdownClicks.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 Text="text" runat="server" ID="lbl_1" />
<asp:DataGrid runat="server" ID="mydg" EnableViewState="true">
<Columns>
<asp:TemplateColumn HeaderText="Actions">
<HeaderStyle CssClass="ProfDataGridHeader" BorderStyle="Solid" BorderWidth="1"></HeaderStyle>
<ItemStyle Width="45%" CssClass="ProfDataGridRow" BorderStyle="Solid" BorderWidth="1"></ItemStyle>
<ItemTemplate>
<asp:DropDownList ID="ddlApprovalStatus" AppendDataBoundItems="True" runat="server" Width="150px" EnableViewState="true" ViewStateMode="Enabled">
<asp:ListItem Value="approve" Text="Approve"></asp:ListItem>
<asp:ListItem Value="reject" Text="Reject"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button Text="click me" runat="server" OnClick="Unnamed_Click" EnableViewState="true" />
</div>
</form>
</body>
</html>

C# : Web page reloaded every time a button clicked

I have a C# web application. In event handler of a link, I add a user control named addUser.ascx to application's Home page as follows:
addUser au = (addUser)LoadControl("UserControls/addUser.ascx");
content.Controls.Add(au);
The problem is that when I click on a button of user control, The entire page will be refreshed and the user control removed. How can I solve that? The button definition in user control is as follows:
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" Width="125px" Height="30px" BackColor="Green" Font-Bold="False" Font-Names="Tahoma" TabIndex="7" />
Edit:
Home.aspx
<%# Page Title="Home" Language="C#" MasterPageFile="~/Site.Master" CodeBehind="Home.aspx.cs" Inherits="WebApplication1.Home" %>
<%# Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI"%>
<%# Register TagPrefix="au" TagName="AddUser" Src="~/UserControls/addUser.ascx" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<telerik:RadSkinManager ID="QsfSkinManager" runat="server" ShowChooser="true" />
<div>
<asp:LinkButton ID="AddLink" OnClick="AddLink_Click" runat="server" Text="Add New User" />
<telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource" PageSize="10">
<MasterTableView DataKeyNames="Id" Font-Names="Tahoma" Dir="RTL">
<Columns>
<telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" />
<telerik:GridBoundColumn DataField="Password" HeaderText="Full Name" />
</Columns>
</MasterTableView>
<PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>
</div>
<asp:PlaceHolder runat="server" ID="content" />
</asp:Content>
Home.aspx.cs
namespace WebApplication1
{
public partial class Home : Page
{
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = new Domain().getAllUsers();
}
// some irrelevent methods here
//
protected void AddLink_Click(object sender, EventArgs e)
{
content.Controls.Clear();
addUser au = (addUser)LoadControl("UserControls/addUser.ascx");
content.Controls.Add(au);
}
}
}
addUser.ascx
<%# Control Language="C#" CodeBehind="addUser.ascx.cs" Inherits="WebApplication1.UserControls.addUser" %>
<%# Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<div>
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>
<div style="float: right;">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click"/>
</div>
</asp:TableCell>
<asp:TableCell>
<div style="float: right;">
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click"/>
</div>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
addUser.ascx.cs
namespace WebApplication1.UserControls
{
public partial class addUser : System.Web.UI.UserControl
{
protected void btnCancel_Click(object sender, EventArgs e)
{
this.Visible = false;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//add somthing to db
// when this button clicked, the whole page will be
// refreshed and the addUser user control will be removed
}
}
}
you should use ajax and asp:updatePanel around the parts you want to be updated. A very nice example is described here : A nice example on asp:updatePanel
On button click event it is always send page to server side so it is refreshed each time when you click on button..
But you can use onclientclick instead of onclick..so it wont refresh your page ..

AJAX HtmlEditorExtender and getting text from the textbox?

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;
}
}

Categories