I have this program in asp.net using C#
.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="first" runat="server">
Text Boxes
<asp:TextBox ID="txtnr" runat="server"></asp:TextBox><br />
<asp:Button ID="btnxt1" runat="server" Text="Next" onclick="btnxt1_Click"/><br />
</div>
<div id="second" runat="server">
<asp:Table ID="tbl" runat="server"></asp:Table>
<asp:Button ID="btnx2" runat="server" Text="Next" onclick="btnx2_Click"/><br />
</div>
<div id="third" runat="server">
<asp:Label ID="lblxx" runat="server" Text=""></asp:Label><br />
</div>
</form>
</body>
</html>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
static int numr = 0;
static TableHeaderCell[] tc2;
static TextBox[] txtb;
protected void Page_Load(object sender, EventArgs e){}
protected void btnxt1_Click(object sender, EventArgs e)
{
numr = int.Parse(txtnr.Text);
TableHeaderRow tr;
tc2 = new TableHeaderCell[numr];
txtb = new TextBox[numr];
for (int i = 0; i < numr; i++)
{
tr = new TableHeaderRow();
tc2[i] = new TableHeaderCell();
txtb[i] = new TextBox();
txtb[i].Text = "w";
tc2[i].Controls.Add(txtb[i]);
tr.Controls.Add(tc2[i]);
tbl.Controls.Add(tr);
}
}
protected void btnx2_Click(object sender, EventArgs e)
{
for (int i = 0; i < numr; i++)
lblxx.Text += txtb[i].Text+"<br/>";
}
}
Program steps:
enter number of text-boxes to appear (say = 4) and click 'Next'
four text-boxes will appear (the program sets the value of textbox = "w",to understand the problem)
user can set other value for the text for the four text boxes (say: 1 , 2 , 3 , 4) then click next
finally, the program print the values of text boxes, but the problem is the program
will print: wwww not "1234" :( ??
How to fix this problem??
The problem is due to the fact that dynamic controls (like yours) do not automatically maintain their state after a post back and you should handle it on your own.
Please follow this link for a very similar question and then this link for the corresponding solution.
Related
I have just started teaching myself dotNet with C# and have hit a problem. Basically I have a variable that I want initialised when the page is first loaded. I have tried to use Session_Start to do this. However when I put a breakpoint at that point, it never seems to get there.
What I am not understanding?
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int numTries;
protected void Session_start(object sender, EventArgs e)
{
numTries = 3;
}
protected void CheckNum_ServerClick(object sender, EventArgs e)
{
if (numTries>0)
{
numTries--;
}
}
protected void Page_Load(object sender, EventArgs e)
{
Answer.InnerText = "You have "+numTries.ToString()+" changes left.";
}
}
HTML
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Guess a number between 1 & 100
<input type="text" id="Guess" runat="server" />
<br /><br />
<input type="submit" value="OK" id="CheckNum" runat="server" onserverclick="CheckNum_ServerClick" />
<br /><br />
<p id="Answer" runat="server"></p>
</div>
</form>
</body>
</html>
I am using Visual Studio Community 2013
I want this gridview to emulate a console, the text file I have is a server console log.
I want it to:
*Refresh when the file changes.(thus the timer)
*Scroll bar position maintained when updated, unless at bottom.
*When scroll bar position is at the bottom,stay at the bottom after databind update.
Current Problem: On timer, databind() moves the scrollbar position back to the top, and with a refresh so often you can't even scroll down at all.
Here is my current code. Thank you for your help!
asp.net
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="TEST CONSOLE.aspx.cs" Inherits="WebApplication1.TEST_CONSOLE" %>
<!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 runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="30" ontick="Timer1_Tick"></asp:Timer>
<div style="width: 100%; height: 400px; overflow: scroll">
<asp:GridView ID="GridView1" runat="server" MaintainScrollPositionOnPostback="true">
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication1
{
public partial class TEST_CONSOLE : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StreamReader objStreamReader = new StreamReader(Server.MapPath("TextFile.txt"));
var arrText = new List<string>();
// ' Loop through the file and add each line to the ArrayList
while( objStreamReader.Peek() >= 0){
arrText.Add(objStreamReader.ReadLine());
}
//' Close the reader
objStreamReader.Close();
//' Bind the results to the GridView
GridView1.DataSource = arrText;
GridView1.DataBind();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
GridView1.DataBind();
}
}
}
Found most of answer here
http://forums.asp.net/t/1220070.aspx?how+to+maintain+scroll+position+in+gridview+or+datagrid+during+postback
using ajax and these two functions
function Func() {//document.getElementById("hdnScrollTop").value
document.getElementById("divScroll").scrollTop = document.getElementById("hdnScrollTop").value;
}
function Func2() {
var s = document.getElementById("divScroll").scrollTop;
document.getElementById('hdnScrollTop').value = s;
the aspx code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="MyWebApplication.MyWebForm" %>
<!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 runat="server">
<title></title>
</head>
<body style="height: 334px">
<form id="form1" runat="server">
<div style="height: 338px">
<asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="True"></asp:TextBox>
<br />
<asp:CustomValidator ID="MyCustomValidator" runat="server"
ControlToValidate="MyTextBox" ErrorMessage="My error message"
onservervalidate="Foo2"></asp:CustomValidator>
</div>
</form>
</body>
</html>
the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyWebApplication
{
public partial class MyWebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
bool Foo1(string bar)
{
if (bar == "bar")
return true;
return false;
}
protected void Foo2(object source, ServerValidateEventArgs args)
{
args.IsValid = this.Foo1(this.MyTextBox.Text);
} // breakpoint
}
}
I set breakpoint in the Foo2 function (comment), but debugger hasn't visited it. I tried writing diffrent text, not writting anything and writting "bar" and pushing enter and tab after that, but it still doesn't work. I mean text changing action. Is it possible?
There's no submit button. You need to add a button to submit the form, which will then trigger page validation.
<asp:Button runat="server" id="myButton" Text="Submit" />
I'm trying to build a multilingual web site on asp.net and i need to set html tag dir attribute from rtl to ltr and vice versa programmatically in c sharp.
For now i have 3 languages english, hebrew and russian and i need to switch between them.
I need to change direction of a site every time i change from ltr to rtl language and from rtl to ltr language
here the example of my masterpage
please help me
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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 runat="server">
<title></title>
<link href="CSS/Site-RTL.css" rel="stylesheet" type="text/css" />
</head>
<body id="html" runat="server">
<form id="theForm" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</div>
<div style="margin-top: 20px;">
<asp:LinkButton ID="btnSetEnglish" runat="server" Text="English" CommandArgument="en-GB"
OnClick="RequestLanguageChange_Click"></asp:LinkButton>
<asp:LinkButton ID="btnSetHebrew" runat="server" Text="Hebrew" CommandArgument="he-IL"
OnClick="RequestLanguageChange_Click"></asp:LinkButton>
<asp:LinkButton ID="btnSetRussian" runat="server" Text="Russian" CommandArgument="ru-RU"
OnClick="RequestLanguageChange_Click"></asp:LinkButton>
</div>
<div>
<asp:SiteMapDataSource ID="smdsMaster" runat="server" OnDataBinding="Page_Load" />
<asp:Menu ID="MenuMaster" runat="server" CssClass="menu" DataSourceID="smdsMaster"
Orientation="Horizontal" StaticDisplayLevels="2" StaticSubMenuIndent="16px" MaximumDynamicDisplayLevels="1">
<DataBindings>
<asp:MenuItemBinding DataMember="SiteMapNode" NavigateUrlField="Url" />
</DataBindings>
</asp:Menu>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
here a code behind a master page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Localization.Classes;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
LinkButton senderLink = sender as LinkButton;
//store requested language as new culture in the session
if (senderLink.CommandArgument == "he-IL")
{
// some code
}
else
{
// some code
}
Session[Global.SESSION_KEY_CULTURE] = senderLink.CommandArgument;
//reload last requested page with new culture
Server.Transfer(Request.Path);
}
}
In your code behind:
this.html.Attributes.Add("dir", "ltr");
An alternative, if this is not working is to place the attribute directly on the tag and use a property to populate it:
<body dir="<%:Dir%>">
public string Dir { get; set; }
// Set Dir in the on click event
protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
LinkButton senderLink = sender as LinkButton;
//store requested language as new culture in the session
if (senderLink.CommandArgument == "he-IL")
{
Dir = "rtl";
}
else
{
Dir = "ltr";
}
Session[Global.SESSION_KEY_CULTURE] = senderLink.CommandArgument;
//reload last requested page with new culture
Server.Transfer(Request.Path);
}
.html
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
<link href="dark.css" rel="stylesheet" type="text/css" id="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:Label ID="CaptionLabel" runat="server"></asp:Label>
<asp:TextBox ID="NumberTextbox" runat="server">(empty)</asp:TextBox>
<asp:Button ID="SquareButton" runat="server" Text="Square" style="background-color:Blue; color:White;" />
<asp:Label ID="ResultLabel" runat="server" Text="(empty)" CssClass="reverse"></asp:Label>
<p>
<asp:Label ID="Label1" runat="server" CssClass="footer1"
Text="Label Label Label Label LabelLabelLabel Label Label Label Label Label Label Label"></asp:Label>
</p>
<asp:RadioButton ID="radioDark" runat="server" AutoPostBack="True"
Checked="True" GroupName="grpSelectStylesheet"
oncheckedchanged="SwitchStylesheets" Text="Dark" />
<br />
<asp:RadioButton ID="radioLight" runat="server" AutoPostBack="True"
GroupName="grpSelectStylesheet" oncheckedchanged="SwitchStylesheets"
Text="Light" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SwitchStylesheets(object sender, EventArgs e)
{
if (radioDark.Checked)
stylesheet.Href = "dark.css";
if (radioLight.Checked)
stylesheet.Href = "light.css";
}
protected void Button1_Click(object sender, EventArgs e)
{
int count=DateTime.Now.Second;
for (int i = 0; i < count; i++)
{//for
Label q = new Label();
q.ID = DateTime.Now.Second.ToString();
q.Text = DateTime.Now.Second.ToString();
string spacee = "<br />";
Label space = new Label();
space.Text = spacee;
form1.Controls.Add(q);
form1.Controls.Add(space);
}//for
}
}
When the button is clicked, it works as it should but the footer does not register the expansion of the page.
Your code is completely wrong. you cannot change the stylesheet like this for a control even if the autopostback is set to true.
UPDATE: Here's how you should do it:
1- Remove the .css reference from your page.
2- Add this method to your page:
private void UpdateStylesheet(string filepath)
{
HtmlLink newStyleSheet = new HtmlLink();
newStyleSheet.Href = filepath;
newStyleSheet.Attributes.Add("type", "text/css");
newStyleSheet.Attributes.Add("rel", "stylesheet");
Page.Header.Controls.Add(newStyleSheet);
}
3- Add this line to your page_load event:
UpdateStylesheet("dark.css");
4- Handle the SwitchStylesheets like this:
if (radioDark.Checked)
UpdateStylesheet("dark.css");
if (radioLight.Checked)
UpdateStylesheet("light.css");