|
You need to understand the page lifecycle.
When you create controls in Page load, they only persist 1 time. When the page post back to the server, the controls in page load will lose there values because there created again, or may not be created at all.
You need to create the controls in page.init, so the controls persist during postback.
[MSDN Page LifeCycle^]
[Code Project Page Lifecycle^]
|
|
|
|
|
Changed code as you suggested:
protected void Page_Init(object sender, EventArgs e)
{
if (Page.IsPostBack)
this.Create_MainTable();
}
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
this.Create_MainTable();
}
But the effect is the same - table disappears but the cells are staying one under the other.
Any other suggestions ?
|
|
|
|
|
I write in VB, but I wrote this as a quick example and tested it. The controls persist throughout the page life cycle.
Perhaps this example will give you an idea of how to write controls in code behind so that they persist.
I'm curious if others read this post, I use the With statement to include stuff like control properties.
I was not able to figure out how to use Style in the curly brackets like in the update panel, and was forced to use the control variable name instead. Lots of extra typing. If anyone knows how to add style using the curly brackets let me know.
<%@ Page
Title="Test"
Language="C#"
AutoEventWireup="true"
CodeFile="Default2.aspx.cs"
Inherits="Default2" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body id="bodytag" runat="server" class="PageBackground">
<form id="frmDefault" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div id="wf_container" runat="server">
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
private TextBox txt_lc_userName;
protected void Page_Init(object sender, EventArgs e)
{
var up_login_container = new UpdatePanel() {
ID = "_up_login_container",
ClientIDMode = ClientIDMode.Static,
UpdateMode = UpdatePanelUpdateMode.Conditional,
ChildrenAsTriggers = false
};
wf_container.Controls.Add(up_login_container);
var div_login_container = new Panel();
div_login_container.ID = "_div_login_container";
div_login_container.ClientIDMode = ClientIDMode.Static;
div_login_container.Style.Add(HtmlTextWriterStyle.Width, "100%");
div_login_container.Style.Add(HtmlTextWriterStyle.Height, "400px");
up_login_container.ContentTemplateContainer .Controls.Add(div_login_container);
var div_lc_username = new Panel();
div_lc_username.Style.Add(HtmlTextWriterStyle.Width, "50%");
div_lc_username.Style.Add(HtmlTextWriterStyle.Height, "26px");
div_lc_username.Style.Add(HtmlTextWriterStyle.TextAlign, "left");
div_login_container.Controls.Add(div_lc_username);
txt_lc_userName = new TextBox();
txt_lc_userName.ID = "_txt_lc_userName";
txt_lc_userName.ClientIDMode = ClientIDMode.Static;
txt_lc_userName.Style.Add(HtmlTextWriterStyle.Width, "90%");
txt_lc_userName .Style.Add(HtmlTextWriterStyle.Height, "20px");
div_lc_username.Controls.Add(txt_lc_userName);
var div_lc_submit = new Panel();
div_lc_submit.Style.Add(HtmlTextWriterStyle.Width, "50%");
div_lc_submit.Style.Add(HtmlTextWriterStyle.Height, "46px");
div_lc_submit.Style.Add(HtmlTextWriterStyle.TextAlign, "left");
div_login_container.Controls.Add(div_lc_submit);
var bt_lc_submit = new Button();
bt_lc_submit.ID = "_bt_lc_submit";
bt_lc_submit.Text = "Submit";
bt_lc_submit.Click += new System.EventHandler(bt_lc_submit_click);
div_lc_submit.Controls.Add(bt_lc_submit);
var bt_lc_submit_trigger = new AsyncPostBackTrigger();
bt_lc_submit_trigger.ControlID = "_bt_lc_submit";
bt_lc_submit_trigger.EventName = "click";
up_login_container.Triggers.Add(bt_lc_submit_trigger );
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
else
{
}
}
protected void bt_lc_submit_click(object sender, EventArgs e)
{
string m_userName = txt_lc_userName.Text.Trim();
Response.Write(m_userName);
}
}
|
|
|
|
|
I want to transfer the items of a list box from one asp.net page to another. The code somehow is throwing error. I have added my code below. Please advice.
Page 1:
<asp:listbox id="SelectedItems" runat="server" visible="true" convertemptystringtonull="true" enableviewstate="true" style="margin-left: 0px" width="244px" selectionmode="Multiple" autopostback="false" appenddatabounditems="True">
<asp:button id="sbmtButton" runat="server" text="Submit" style="margin-left: 0px" width="152px" onclick="sbmtButton_Click">
Backend:
protected void sbmtButton_Click(object sender, EventArgs e)
{
Session["wrd"] = SelectedItems;
Server.Transfer("~/aftrSubmit.aspx";);
}
Page 2:
<asp:listbox id="Prescription_list" runat="server" autopostback="false" enableviewstate="true">
backend:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Prescription_list = (ListBox)Session["wrd"];
}
}
|
|
|
|
|
You did not provide the error nor the line of code that causes it. However, I am going to guess that it is the line that reads
Session["wrd"] = SelectedItems;
And, I'll also guess at the error. Something about not being able to serialize whatever SelectedItems is. Make SelectedItems a string of delimited values and that will work.
After looking at it more, it looks like your trying to put the whole listbox object into session. That won't work. Just make delimited string and do it that way.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
At present we deployed in production site But we are facing performance issue,
how can we increase performance.
|
|
|
|
|
- Profile your application;
- Find the performance bottle-neck;
- Tune the affected code;
- Repeat until performance is acceptable;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Where? Next time onwards mention some more details to get more & quick answers. Here I have consolidated bunch of things related to performance, check it out.
Improve Everything[^]
|
|
|
|
|
Hello
In my 'register new user' ASP.NET Web form, I am experiencing two errors which I have not been able to resolve. I have a register.aspx file and a register.aspx.vb file.
I get this error in my aspx file:
BC30456: 'SubmitButton_Click' is not a member of 'ASP.account_register_aspx'.
This relates to this line:
</div>
<asp:Button ID="SubmitButton" runat="server" OnClick="SubmitButton_Click" Text="Register" CssClass="btn btn-default" Height="32px" />
<br />
</asp:Content>
and in my aspx.vb file and, presumably related to the above error in my aspx file, where I have:
Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton_Click.Click.
I get the following error:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
I have attempted to read up on Handles/WithEvents tonight, but it's a bit above me at the moment.
In context, the Handles error occurs in this snippet:
Partial Class _Register
Inherits System.Web.UI.Page
Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton_Click.Click.
Using(conn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("usersConnectionString").ConnectionString))
.Dim sql = "INSERT INTO userlist (username,password, strEmail) VALUES (@username,@password, @strEmail)"
.Using cmd = New OleDbCommand(sql, conn)
conn.Open()
cmd.Parameters.AddWithValue("@username", txtusername.Text)
cmd.Parameters.AddWithValue("@password", txtpassword.Text)
cmd.Parameters.AddWithValue("@strEmail", txtstrEmail.Text)
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
Any help would be appreciated.
|
|
|
|
|
I'm assuming the extra "." on the end of your Handles line is a typo?
It sounds like your <%@ Page ... %> directive is pointing to the wrong .aspx.vb file.
Once you've got it pointing to the right file, I suspect your handler will fire either two or three times - you've connected it to the Click event in the code-behind using the Handles clause; you've connected it again in the markup with the OnClick="..." attribute; and, if you have AutoEventWireup turned on, that will connect it a third time based on the method name.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello Richard
Thank you for your reply.
I take it that where I have:
<%@ Page Title="Register" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Register.aspx.vb" Inherits="Account_Register" %>
in my main aspx file, that 'CodeFile' must always point to the aspx.vb file and that in the aspx.vb file itself, class statements related to 'Inherit', should refer to 'Account Register' in this particular case?
Thanks.
|
|
|
|
|
Yes, CodeFile="..." needs to point to the .aspx.vb file. The class name in the Inherits="..." attribute needs to match the name of the class defined in your .aspx.vb file.
So if you have register.aspx.vb which contains:
Partial Class Account_Register
Inherits System.Web.UI.Page
...
End Class
Then your <%@ Page ... %> directive in register.aspx would look like:
<%@ Page ... CodeFile="Register.aspx.vb" Inherits="Account_Register" %>
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That's great.
Thanks very much.
Blueie
|
|
|
|
|
<html>
<body>
<h2>Update Record</h2>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
cid=Request.Form("customerID")
if Request.form("companyname")="" then
set rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM customers WHERE customerID='" & cid & "'",conn
%>
<form method="post" action="demo_update.asp">
<table>
<%for each x in rs.Fields%>
<tr>
<td><%=x.name%></td>
<td><input name="<%=x.name%>" value="<%=x.value%>"></td>
<%next%>
</tr>
</table>
<br><br>
<input type="submit" value="Update record">
</form>
<%
else
sql="UPDATE customers SET "
sql=sql & "companyname='" & Request.Form("companyname") & "',"
sql=sql & "contactname='" & Request.Form("contactname") & "',"
sql=sql & "address='" & Request.Form("address") & "',"
sql=sql & "city='" & Request.Form("city") & "',"
sql=sql & "postalcode='" & Request.Form("postalcode") & "',"
sql=sql & "country='" & Request.Form("country") & "'"
sql=sql & " WHERE customerID='" & cid & "'"
on error resume next
conn.Execute sql
if err<>0 then
response.write("No update permissions!")
else
response.write("Record " & cid & " was updated!")
end if
end if
conn.close
%>
</body>
</html>
The code above won't work, I tried removing the ' around cid and gave me other errors. Does anybody see my error? Thank you guys.
|
|
|
|
|
|
Hi,
I am using a modal pop up to check the Accessibility of user if he success fully logs in i am allowing to open a PDF File. when i am opening the PDF File i want to close the Ajax modal popup but unable to close.
can any one help me in solving this issue
Thanks,
Aditya.
|
|
|
|
|
aditya kiran maroju wrote: i want to close the Ajax modal popup but unable to close.
What (error) are you confronting?
|
|
|
|
|
Hi. I Have a master page at the back having menu. I have then created a webform from it. I want the main menu item to get highlighted on page load. Also I want the main menu item to get highlighted when a sub menu item is loaded.
My code which I have tried so far will follow.
hoping for a solution.
Thanks in advance.
<script src="../Scripts/jquery-2.1.0.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#nav').find('li a').click(function () {
$('#nav').find('li a').removeClass('active');
$(this).addClass('active');
$($(this).closest('li.menu-item').children()[0]).addClass('active');
});
});
</script>
#nav {
height: 50px;
width: auto;
position: relative;
background-color: #ad8f5d;
}
#nav ul li {
width:150px;
text-align:center;
}
#nav ul li a
{
color: Black;
font-size: 13px;
font-weight: bold;
line-height:50px;
text-decoration: none;
}
#nav li a.active
{
color:White;
}
#nav ul li a.has_submenu {
background: transparent url('../Images/submenu-item.jpg') no-repeat scroll right center;
padding-right: 0px;
}
#nav ul li a.has_submenu:hover, #nav ul li.sfHover a.has_submenu {
background-image: url('../Images/submenu-item-hover.jpg');
}
#nav ul ul li a.has_submenu {
background: transparent url('../Images/submenu-item-invert.jpg') no-repeat scroll right center;
padding-right: 0px;
}
#nav ul ul li a.has_submenu:hover, #nav ul ul li.sfHover a.has_submenu {
background-image: url('../Images/submenu-item-hover-invert.jpg');
}
#nav ul ul li a {
float: none;
width: 170px;
}
#nav ul > li:hover > a
{
background-color: #1f478d;
}
#nav ul ul > li:hover > a
{
background-color: #1f478d;
}
#nav ul > li:hover > a.has_submenu
{
background-image: url('../Images/submenu-item-hover.jpg');
}
#nav ul ul > li:hover > a.has_submenu
{
background-image: url('../Images/submenu-item-hover-invert.jpg');
}
#nav ul li a:hover, #nav ul li.sfHover a{
background-color: #1f478d;
}
#nav ul ul a
{
background-color: #ad8f5d;
}
#nav ul li li a:hover, #nav ul li.sfHover li a:hover {
background-color: #1f478d;
}
.sf-menu, .sf-menu *
{
height:50px;
margin: 0;
padding: 0;
list-style: none;
}
.sf-menu ul {
position: absolute;
top: -999em;
width: 200px;
margin-top: 0px;
padding-top: 0;
}
.sf-menu ul li {
width: 100%;
}
.sf-menu li:hover {
visibility: inherit;
}
.sf-menu li {
float: left;
position: relative;
}
.sf-menu a {
display: block;
position: relative;
}
.sf-menu li:hover ul,
.sf-menu li.sfHover ul {
left: 0;
top: 49px;
z-index: 99;
}
ul.sf-menu li:hover li ul,
ul.sf-menu li.sfHover li ul {
top: -999em;
}
ul.sf-menu li li:hover ul,
ul.sf-menu li li.sfHover ul {
left: 170px;
top: 0;
}
ul.sf-menu li li:hover li ul,
ul.sf-menu li li.sfHover li ul {
top: -999em;
}
ul.sf-menu li li li:hover ul,
ul.sf-menu li li li.sfHover ul {
left: 10em;
top: 0;
}
<div id="nav">
<ul class="sf-menu">
<li class="menu-item"><a href="Home.aspx">Home</a></li>
<li class="menu-item"><a class="has_submenu" href="#">Examples</a>
<ul>
<li><a href="Static.aspx">Static Text Page</a></li>
<li><a href="#">Static Frontpage</a></li>
<li><a href="#">Another link</a></li>
</ul>
</li>
<li class="menu-item"><a class="has_submenu" href="#">Products</a>
<ul>
<li><a href="Product.aspx">Product One</a></li>
<li><a href="#">Product Two</a></li>
<li><a href="#">Product Three</a></li>
</ul>
</li>
<li class="menu-item"><a href="#">Solutions</a></li>
<li class="menu-item"><a href="#">Contact</a></li>
</ul>
</div>
|
|
|
|
|
hi i want to develop microsoft windows kinect based web application for user like buying and playing games
can any one know how to get started with that using visual studio .Net
|
|
|
|
|
|
Hi,
I want to have a repeater control on which a row would contain button with + and - sign, when we click on + sign that will display a child that is below this row and makes the text of the button as -, when button with text - is clicked it will hide the item below the row. Just like show and hide functionality on repeater.
Can anybody please help me in this regards any kind of help is greatly helpful. Any code snippet, link or advice would help me a lot.
Thanks in advance.
Thanks & Regards,
Abdul Aleem Mohammad
St Louis MO - USA
|
|
|
|
|
|
Hi,
Thanks for your suggestion.
The grid view that you have given me doesn't have editable fields. I am looking something that I can edit, just like in repeater control. Do we have any option for it?
Thanks a lot.
Thanks & Regards,
Abdul Aleem Mohammad
St Louis MO - USA
|
|
|
|
|
Hello
I am attempting to create a 'Register new user' page in ASP.NET.
I have created an email field for the user to type in his email address, but I am getting errors. The code in my Register.aspx file for the email field looks like this:
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="strEmail" CssClass="col-md-2 control-label">Email</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="strEmail" TextMode="Email" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The email field is required." />
<asp:CompareValidator runat="server" ControlToCompare="strEmail" ControlToValidate="Email"
CssClass="text-danger" Display="Dynamic" ErrorMessage="Please enter a valid email address." />
The error I am getting is this: 'Exception Details: System.Web.HttpException: Unable to find control id 'Email' referenced by the 'ControlToValidate' property of'.
The reason I have used strEmail is because that is the name of the column in my MS Access database, and it is used by other parts of the site (to resolve forgotten passwords).
How can I correct these errors, please?
Thanks.
|
|
|
|
|
Member 8761667 wrote: ID="strEmail"
Member 8761667 wrote: ControlToValidate="Email"
From MSDN:
This property must be set to the ID of an input control for all validation controls except the CustomValidator control, which can be left blank.
In other words, you need to have the same value in ControlToValidate that you have in ID - in this case, "strEmail".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|