How to get text from a template text field in asp.net? - c#

I have used a login-template for the page in .net application. How can I get the texts from the input text fields of the template and store it in a string variable?
<form id="form1" runat="server" role="form" method="post" class="login-form">
<div class="form-group" runat="server">
<label class="sr-only" for="form-username">Username</label>
<input type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form_username" runat="server"/>
</div>
<div class="form-group" runat="server">
<label class="sr-only" for="form-password">Password</label>
<input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form_password" runat="server"/>
</div>
<button type="submit" class="btn" onclick="btn_login_Click" runat="server">LOGIN</button>
</form>

To get the Text in a TextBox you can simply do this:
private void AnyMethod()
{
string textValue = this.MyTextBox.Text;
}

You can access the raw post data with:
string exampleInput = Request.Form["<inputElementName>"];
If your input element is having the attribute 'runat="server"' you should be able to access it with
string exampleInput = <inputElementName>.Value;
If this does not work, you would need to show us a little bit more of your code/page.
Sample form:
<form id="login" runat="server">
<asp:TextBox ID="tbInput1" runat="server"
Width="75px"
TabIndex="1">
</asp:TextBox>
<asp:Button ID="btSubmit" runat="server" Text="Submit" />
</form>
Access with
tbInput1.Text

I guess ,if runat=server is there then it should directly get value using :
form_username.value.Tostring().

After doing little research on above issue got to know that ,
we can't access the login control properties directly .
if we want to access name then we have to use : User.Identity.Name
try doing this ! it might help you .
For further reference please visit below link :
https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.redirecttologinpage.aspx

Related

Input submit onclick and onserverclick not working

I'm working on a simple login screen with the below code:
<form id="login">
<div class="formHeader">
<h1>Login</h1>
</div>
<div class="formDiv">
<input type="text" placeholder="Email" name="txtEmail"/>
<div class="inputImage fa fa-user"></div>
</div>
<div class="formDiv">
<input type="password" placeholder="Password" name="txtPassword"/>
<div class="inputImage fa fa-lock"></div>
</div>
<div class="formDiv">
<label id="remember">
<input type="checkbox" name="cbxRemberMe" value="Checked"/>
<div class="checkbox"></div><span>Remember me</span>
</label>
</div>
<div class="formDiv">
<input type="submit" value="LOGIN" onclick="btnLogin_Click" />
</div>
<div class="formFooter"><a class="forgot" href="#">Forgot Password</a></div>
and back-end code
protected void btnLogin_Click(object sender, EventArgs e)
{
}
But the button does not call the backend code at all.
Nothing happens except the URL changes from
"http://localhost:15726/Pages/Login.aspx" to
"http://localhost:15726/Pages/Login.aspx?txtEmail=&txtPassword=&ctl00=LOGIN"
I cannot put a Runat=Server attribute in the form as I have 2 forms on this page (login and register)
any help is appreciated.
What I have tried:
Playing around with Runat=Server, onclick() and OnServerClick() (swapping around and changing them up)
Making input type=submit / input type=button
none of these work.
Have you tried these two approaches?
<asp:Button runat="server" id="btnLogin" Text="LOGIN" OnClick="btnLogin_Click">
or
< button onserverclick="btnLogin_Click" runat="server" id="btnLogin">LOGIN</button>
Edit
This was supposed to be a comment and not answer. Sorry for that.
However, the entire form doesn't seem to be "webform" form compatible. If you want to be able to read the values from the form elements the form should have a runat="server" tag, too.
Firstly, you need to specify whether it is a server control or client control. Specify runat=server to tell that it should be available for the Code-Behind file.
Finally, Use any javascript function with OnClick and any code behind function with OnServerClick
Something like this,
aspx
Plain HTML
<input type="submit" runat="server" value="LOGIN" onclick="return js_btnLogin_Click()" OnServerClick="btnLogin_Click"/>
or with ASPX Control
<asp:Button runat="server" Text="LOGIN" OnClientClick="return js_btnLogin_Click()" OnClick="btnLogin_Click">
javascript
<script type="text/javascript">
function js_btnLogin_Click()
{
//return false, in case if you want to stop posting the form to the server
return true;
}
</script>
Code-Behind
protected void btnLogin_Click(object sender, EventArgs e)
{
}

HTML Button Calling ASP Class?

Was hoping someone would be able to help me. I'm basically attempting to call a method within an class using a HTML button. The reasoning behind this is because I'm intending to make it work similar to a login (and wanting it to hold sessions upon logging in), as well as keeping the current, visual effects of the button.
Upon research, there is some places basically saying you can do it, however it simply won't work for me for some reason. Where it says you can do it-
HTML Button like a ASP.NET Button
Here is my code-
<form runat="server">
<div class="input">
<asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
<span><i class="fa fa-envelope-o"></i></span>
</div>
<div class="input">
<asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
<span><i class="fa fa-lock"></i></span>
</div>
</form>
<button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>
With the C# code containing
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void doIt(object sender, EventArgs e)
{
Response.Redirect("index.html");
}
}
Am I going about this the totally wrong way? (The redirect was just there as a test for now, until I'm able to get it to run the method since it doesn't run it.)
Any help is appreciated.
Below is all my HTML code:
<form class="login">
<fieldset>
<legend class="legend">Login</legend>
<form runat="server">
<div class="input">
<asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
<span><i class="fa fa-envelope-o"></i></span>
</div>
<div class="input">
<asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
<span><i class="fa fa-lock"></i></span>
</div>
<button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>
</form>
</fieldset>
<div class="feedback">
login successful <br />
redirecting...
</div>
</form>
You have a nested form in your HTML, which is invalid markup. Remove the inner form, and add runat="server" to the form remaining. Then place your button inside the form like so:
<form class="login" runat="server">
<fieldset>
<legend class="legend">Login</legend>
<div class="input">
<asp:TextBox ID="txtUser" runat="server" type="email" placeholder="Username"></asp:TextBox>
<span><i class="fa fa-envelope-o"></i></span>
</div>
<div class="input">
<asp:TextBox ID="TextBox1" runat="server" type="password" placeholder="Password"></asp:TextBox>
<span><i class="fa fa-lock"></i></span>
</div>
<%-- place the button here --%>
<button id="button1" runat="server" onserverclick="doIt" class="submit"><i class="fa fa-long-arrow-right"></i></button>
</fieldset>
<div class="feedback">
login successful
<br />
redirecting...
</div>
</form>
Each web form - .aspx - should contain one form, and must contain runat="server" for the .cs file to manage. All server-side elements - elements with the runat="server", such as your <button> must be contained within it for the code behind to be able to point to it. If you don't, you should receive a Invalid postback or callback argument exception yellow screen of death.

Value of input returning blank

I am trying to retrieve the value of an input in my code behind, but it is returning "". I am using the remodal plugin to display a popup, and then I have a textbox within this modal popup
Here is the ASP.NET code I am using.
Add Customer
<div class="remodal" data-remodal-id="addCustomer">
<button data-remodal-action="close" class="remodal-close"></button>
<h1>Add Customer</h1>
<h4>Customer</h4>
<div class="form-group has-feedback">
<label class="sr-only" for="name2">Customer</label>
<input type="text" class="form-control" id="TXT_CreateCustomer" placeholder="Customer" name="name2" runat="server"/>
<i class="fa fa-building-o form-control-feedback"></i>
</div>
<br />
<button id="BTN_NewJob_AddCustomer" data-remodal-action="confirm" class="remodal-confirm" runat="server" onserverclick="BTN_NewJob_AddCustomer_ServerClick">Create Customer</button>
</div>
This is the C# server code that I am using to try and retrieve that value from the textbox.
protected void BTN_NewJob_AddCustomer_ServerClick(object sender, EventArgs e)
{
string example = TXT_CreateCustomer.Value;
}
The server code is very simple and should work, but it only returns "" when text is typed into the textbox.
Any ideas on what would prevent getting the text from the textbox?
Thanks.
You are not using the asp: on your form elements, therefore they are not filling in. asp:input instead of input, also runat='server' is required.
Edit: Sorry, let me clarify. Not asp:input, asp:textbox.
So, for example:
<asp:Textbox id="myTextBox" runat="server" />
I don't know exactly, will it help or no, but try to use following in the code behind:
string example = TXT_CreateCustomer.innerText;

How to access value of of input type="text" control of HTML5 in asp.net usercontrol + Umbraco

I am using ascx page. which I am injecting in UMBRACO.
following is my .ascx Code.
<form id="main-contact-form" name="contact-form" method="post" action="#">
<div class="form-group">
<input type="text" runat="server" name="name" id="txtName" class="form-control" placeholder="Name" required />
</div>
<button type="submit" class="btn-primary">Send Message</button>
</form>
I have try to get value of text box (txtName) from back end but I am not abe to access it.
I have tried following method to do so..(Where strtxtName is a string variable)
1) strtxtName = txtName.Value
2) strtxtName = Request.QueryString("txtName")
3) Request.Form.AllKeys -> return Nothing.
But not able to get value of text Box ,
I don't think Umbraco is making any issue.?

ASP.NET AJAX Modal Popup - Dynamic Control Values

I am working on adding a modal popup to ASP.NET pages. The popup will give the user some textboxes to fill in, with a Cancel and Submit button.
The issue I'm running into is that the textboxes are being created dynamically, as the number of textboxes needed and what they are asking for will change depending on what is clicked on the page. When attempting to retrieve the values that have been entered after clicking Submit on the modal window (which is not tied to the modal window so that it will do a postback), the textboxes are gone by that point and the data cannot be retrieved.
Here's the code for the modal popup:
<div id="divModalContainer">
<div id="PromptContentHeader">
<asp:Label ID="lblHeader1" runat="server">
</asp:Label>
<br />
<asp:Label ID="lblHeader2" runat="server">
</asp:Label>
<asp:Label ID="lblPassFileName" runat="server">
</asp:Label>
</div>
<!--<ul id="ulTabModalPrompt" class="tabnav" runat="server">
</ul>-->
<div id="divModalPrompts" runat="server">
<table id="PromptTable" runat="server">
</table>
</div>
<div id="divModalButtons" style="width:230px;">
<div style="float:left">
<asp:Button ID="btnCancelDocPrompts" runat="server" Text="Cancel" OnClick="btnCancelDocPrompts_Click" />
</div>
<div style="float:right">
<asp:Button ID="btnSubmitDocPrompts" runat="server" Text="Submit" OnClick="btnSubmitDocPrompts_Click" />
</div>
</div>
</div>
</asp:Panel>
<ajaxtoolkit:ModalPopupExtender ID="modalDocPrompt" runat="server"
TargetControlID="btnOpenPromptWindow"
PopupControlID="panelPrompts"
OkControlID="btnHiddenOkButton"
CancelControlID="btnCancelDocPrompts"
BackgroundCssClass="ModalPromptBackground"
DropShadow="true" />
<asp:Button ID="btnOpenPromptWindow" runat="server" Text="Test Modal" Style="display: none;" />
<asp:Button ID="btnHiddenOkButton" runat="server" Text="Test Modal" Style="display: none;" />
Before the modal popup is shown, rows will be added to PromptTable, with a label and textbox in each row.
The btnSubmitDocPrompts_Click method will attempt to go through each row in PromptTable and retrieve the values entered, but once Submit is clicked, there are no rows anymore.
Thanks for the help!
You could try using JavaScript to write the values of the textboxes to a HiddenField (which will get posted back if it exists when the page loads).
You'll have to encode the values and comma-separate them (or similar), then parse the values out server-side.
Edit: Example
OK, I'd personally use jQuery for the JavaScript part, but I'll assume you're doing it 'raw'.
Say your markup (with a few added dynamic textboxes) looks like this:
<input type="hidden" id="hdnFormValues" />
<div id="dynamicForm">
<div id="textBoxArea">
<input type="text" id="newField1" /><br />
<input type="text" id="newField2" /><br />
<input type="text" id="newField3" /><br />
<input type="text" id="newField4" /><br />
</div>
<input type="submit" onclick="saveValues()" value="Save Values" />
</div>
and you have a JavaScript function that looks like this:
function saveValues()
{
theBoxes = document.getElementById('textBoxArea').getElementsByTagName('input');
hdnValues = document.getElementById('hdnFormValues');
hdnValues.value = "";
for(var i = 0; i < theBoxes.length; i++)
{
hdnValues.value += escape(theBoxes[i].value) + '|';
}
}
Then when the submit button gets pressed, the value of the HiddenField will become a pipe-delimited string of encoded values.
For example, if text boxes 1 through 4 had the values:
I'm
encoding
dynamic
values!
then the HiddenField's value would become I%27m|encoding|dynamic|values%21|
Remember, you'll need to output the function above from ASP.NET server side. Look at the ScriptManager documentation for how to do this. The reason is that the HiddenField's ID will be dynamic, so you can't (reliably) predict it before runtime.
Finally, in your server-side code that recieves the postback, you split out the delimited string and unencode it, then do what you want with the values.
The biggest caveat here is security and validation - although I've encoded the string, you need to do your own validation and security checks!

Categories