how to run asp.net code from string variable at runtime? - asp.net

i have following code in a String variable at run-time
<form runat="server">
<asp:textbox runat="server"
ID="a1"
Text="enter text here"
/>
<asp:button runat="server"
Text="enter text here"
/>
<br />
</form>
so how can i run it to at run time on a new page by clicking on some button or so?
earliest help will be highly appreciated.

You mentioned loading from a text file. I think you should review usercontrols. It is the preferred method to load asp.net content and controls from an included file.
http://msdn.microsoft.com/en-us/library/y6wb1a0e(v=vs.100).aspx

Put this code into a user control, and pass the control name as a string instead.
void Page_Init(object sender, System.EventArgs e)
{
//your string
string controlName = "TempControl_Samples1.ascx.cs";
// instantiate usercontrol object
MyControl myControl1 = (MyControl)LoadControl(controlName);
PlaceHolder1.Controls.Add(myControl1);
}

Related

runat server field not filled when clicking on button?

I'm using an aspx file as my main page with the following code snippet:
<input type="text" runat="server" id="Password" />
<asp:Button runat="server" id="SavePassword"
Text="Save" OnClick="SavePassword_Click"/>
Then in the code behind I use:
protected void SavePassword_Click(object sender, EventArgs e)
{
string a = Password.Value.ToString();
}
Setting Password.Value in the Page_Load works as expected, but setting a to the value results in an empty string in a regardless what I type in on the page befoere I click the save button.
Am I overlooking here something?
You should add label field with name labelShow in aspx page like this
<asp:Label ID="labelShow" runat="server"></asp:Label>
Then add the string into .cs file
protected void SavePassword_Click(object sender, EventArgs e)
{
string a = Password.Value.ToString();
labelShow.Text = a.ToString();
}
I can figure several errors :
You say something about Page_Load. You should always remember Page_Load is executed at every postback. So if you write something into the textbox at Page_Load, it will erase the value typed by user. You can change this behavior by veriyfing
if(!IsPostBack)
{
// Your code
}
around your Page_Load content.
input runat="server" is not the proper way to do TextBox in ASP.Net, in most cases you should use asp:TextBox
You don't do anything with your "a" string, your current code sample does nothing, whether the Password field is properly posted or not. You can do as suggested by Ravi in his answer to display it.
I suggest you to to learn how to use VS debugger, and to read a good course on ASP.Net Webforms to learn it.

Simple HTTP Post returning empty Request.Form

I am trying a simply HTTP post to post data from one form to another in asp.net.
sender page code
<form id="form1" runat="server" method="post" action="CILandingPage.aspx">
<asp:TextBox name="txtUname" runat="server" Width="180px"></asp:TextBox>
<asp:TextBox name="txtPassword" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
<asp:TextBox name="txtTransaction" runat="server" Width="180px"></asp:TextBox>
and the receiver page has code
lblUserName.Text = Request.Form["txtUname"].ToString();
lblPassword.Text = Request.Form["txtPassword"].ToString();
lblTransactionID.Text = Request.Form["txtPassword"].ToString();
it throws NullReferenceException because Request.Form object is empty.
what am i missing?
Set the PostBackUrl property for the control to the URL of the page to which you want to post the ASP.NET Web Forms page.
Remove action and add PostBackUrl into Button.Instead name use ID property value.
In Default.aspx
<form id="form1" runat="server" method="post">
<div>
<asp:TextBox ID="TextBox1" name="txtUname" runat="server" Width="180px"></asp:TextBox>
<asp:TextBox ID="TextBox2" name="txtPassword" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
<asp:TextBox ID="TextBox3" name="txtTransaction" runat="server" Width="180px"></asp:TextBox>
<asp:Button ID="button" PostBackUrl="~/CILandingPage.aspx" runat="server" />
</div>
</form>
In CILAndinaPage.aspx.cs
using System;
public partial class CILandingPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Response.Write(Request.Form["TextBox1"].ToString() +Environment.NewLine);
Response.Write(Request.Form["TextBox2"].ToString() + Environment.NewLine);
Response.Write(Request.Form["TextBox3"].ToString());
}
}
}
You may use PreviousPage reference as below:
protected void Page_Load(object sender, EventArgs e)
{
// first check if we had a cross page postback
if ( (PreviousPage != null) && (PreviousPage.IsCrossPagePostBack))
{
Page previousPage = PreviousPage;
TextBox UserName= (TextBox)previousPage.FindControl("txtUname");
TextBox Password= (TextBox)previousPage.FindControl("txtPassword");
// we can now use the values from TextBoxes and display them in two Label controls..
lblUserName.Text = UserName.Text;
blPassword.Text = Password.Text;
}
}
This code in Page_Load will get referenced to previous page that has posted the data & helps you get the same on the destination page.
Hope this helps!!
Since you are posting cross-page, it is likely that collection item (txtPassword) doesn't exist. You can try setting the ClientIdMode of each control to static so that the id used in the HTTP post matches what you are looking for in the .Form collection on the target page.
Check out this article for more info on cross-page posting: https://msdn.microsoft.com/en-us/library/ms178139%28v=vs.140%29.aspx
Use your browser debugging tool (F12) to see what is transmitted in the HTTP Post body.

onchange for the asp.net textbox is not working

i have one text box beside which there is one date picker and one dropdownlist in one user control which i'm using in one aspx page.I want to enable the regular field validator of the dropdownlist if we select the date from the datepicker in the textbox otherwise it should it should be false.
So how i should go ??
Would something basically like this work for you:
Markup:
<asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="true"
OnTextChanged="MyTextBox_TextChanged">
<asp:RegularExpressionValidator ID="MyValidator" runat=server
ControlToValidate="MyTextBox"
ErrorMessage="Must be in correct format"
ValidationExpression="[a-zA-Z]" /><!-- use whatever regex you need here -->
Code behind:
protected void MyTextBox_TextChanged(object sender, System.EventArgs e)
{
MyValidator.IsEnabled = !String.IsNullOrEmpty(MyTextBox.Text);
}

Pass username & password to an invisible Login Control to validate?

In my master page, on the menu there is an icon which uses Jquery to slide, showing 2 textboxes (username, password) for users to enter & 1 submit button. My idea is that after submitting, I get values of these 2 fields to assigned it to an invisible Login Control in my MasterPage, then validate automatically.
I could get values and assign but problem is I don't know how to trigger the Login button in Login Control (how to force it to process information)? the DataBind() func doesn't work
Master.master
<div id="login">
<p>
<asp:Login ID="Login2" runat="server" DestinationPageUrl="~/Index.aspx" LoginButtonStyle-CssClass="button_login"
TextBoxStyle-CssClass="input-name" Visible="false">
</asp:Login>
<asp:TextBox ID="inputUser" CssClass="input-name" Text="Username" runat="server"></asp:TextBox>
<asp:TextBox ID="inputPassword" CssClass="input-pass" Text="Password" runat="server"
TextMode="Password"></asp:TextBox>
<asp:Button ID="btn_login" CssClass="button_login" runat="server" OnClick="triggerLogin" />
</p>
</div>
Main.master.cs:
protected void triggerLogin(object sender, EventArgs e)
{
Login2.UserNameLabelText = inputUser.Text;
Login2.PasswordLabelText = inputPassword.Text;
Login2.DataBind();
}
Actually I already have a login page which processes individually, is it possible to pass information to that page to process?
You can add your username and password int o a session.
Something like:
Session["user"] = inputUser.Text;
Session["pass"] = inputPassword.Text;
By using that you can access the username,password in your pages.
var myusername = Session["user"].ToString();
var mypassword= Session["pass"].ToString();
Check this on MSDN:
ASP.NET State Management Overview
How to: Pass Values Between ASP.NET Web Pages
Regards
Yeah, I've found out the solution, without having to use Login Control. I can do it manually.
<asp:TextBox ID="inputUser" CssClass="input-name" Text="Username" runat="server"></asp:TextBox>
<asp:TextBox ID="inputPassword" CssClass="input-pass" Text="Password" runat="server"
TextMode="Password"></asp:TextBox>
<asp:Button ID="btn_login" CssClass="button_login" runat="server" OnClick="triggerLogin" />
Codebehind:
protected void triggerLogin(object sender, EventArgs e)
{
TextBox txtbxInputUser = (TextBox)Page.Master.FindControl("inputUser");
TextBox txtbxInputPass = (TextBox)Page.Master.FindControl("inputPassword");
Label samplelabel1 = (Label)Page.Master.FindControl("sampleLabel1");
if (System.Web.Security.Membership.ValidateUser(txtbxInputUser.Text, txtbxInputPass.Text))
{
System.Web.Security.FormsAuthentication.SetAuthCookie(txtbxInputUser.Text, false);
}
else
{
Response.Redirect("Login.aspx?Attempt=wrong");
}
}

asp.net set textbox control in panel contol

I want to set the textbox contol located in the panel control via code
I know to retrieve the inputted value in the textbox control:
string myVal = Request.Form["txtResult"];
I want to set the txtResult.text = "some text";
makeup snippet:
<asp:Panel ID="Panel1" runat="server" Style="display: none" Width="233px">
<asp:TextBox ID="txtResult" runat="server" AutoPostBack="True"></asp:TextBox>
<br />
<div align="center">
<asp:Button ID="OkButton" runat="server" Text="OK" />
<asp:Button ID="CancelButton" runat="server" Text="Cancel" />
</div>
</asp:Panel>
txtResult is not available within code, I tried to see if it is available in the page_load, it's not
texReults was a typo, its txtResult, I updated the ID
the intellisense does not recognize any cntr by the name txtResult
its a new web application and the panel visibility=True
maybee this wil help, above the snipet, I use ScriptManager from the AJAX Exstension
I am aware of he Asnchronius affects, partial potback, etc.
It's a managed control, you should be able to set it on the Page_Load event:
protected void Page_Load(object sender, System.EventArgs e)
{
txtResult.Text = "some text";
}
Update: Based on your update, there are a couple of things that you would need to check:
Spelling: Are you sure you're spelling the control name correctly?
Its ID in your code is "txtResults", but you're referencing it as
"txtResult".
Designer: Did you copy the aspx page or bypass VS in some way for this page? If so check the .designer file for the reference to the control: i.e. "Page1.aspx.designer.cs"
Visibility: Is the Panel control's visibility set to true? If not, then it won't render the controls that are contained within it.
Update 2: If you're doing this through scriptmanager, then I highly recommend that you read through this: http://www.wrox.com/WileyCDA/Section/Using-the-ASP-NET-AJAX-ScriptManager.id-305492.html

Resources