So, I need to get a string from a textbox to use in a backend program, but I'm unable to get it when im one the site, however it works if i set Text="Test1" prior, even though i change the text in the textbox, "Test1" always gets sent to to my function and works! (Talking about Box3)
Code to the text boxes below
<div class="row">
<form action="#" method="post" class="contact-form">
<div class="col-md-4 col-sm-6">
<asp:TextBox ID="Box3" Text="Test1" runat="server" placeholder="Gamepin..."></asp:TextBox>
</div>
<div class="col-md-4 col-sm-6">
<asp:TextBox ID="Box2" runat="server" placeholder="Name ..."></asp:TextBox>
</div>
<div class="col-md-4 col-sm-12">
<asp:TextBox ID="Box1" runat="server" placeholder="Amount ..."></asp:TextBox>
</div>
<div >
<asp:CheckBox ID="CheckBoxREPLY" runat="server" style="vertical-align: central" Text="YesNo" TextAlign="Left" />
</div>
<div class="col-md-12 col-sm-12">
<input type="button" runat="server" onserverclick="Funcactivate" class="button big green" value="Launch!"/>
</div>
</form>
</div>
And my code to get textbox.text
protected void Funcactivate(object sender, EventArgs e)
{
//Get textbox(s)
string Game = Box1.Text;
string Namepref = Box2.Text;
string Amount = Box3.Text;
}
Changing your button could work.
<asp:Button ID="btnLaunch" runat="server" Text="Launch!" class="button big green" OnClick="Funcactivate" CausesValidation="False"/>
If you are using web forms, you should be able to capture the text box entry in the "if(!IsPostBack){...}" event and set it to a variable and use it in your "Funcactivate" method.
Hello Sleepwalker, here is a code sample that works for me, I am not using the isPostBack. I am also using an asp:button Instead of using an HTML input control.
<div>
<asp:TextBox ID="txtBox1" runat="server"></asp:TextBox>
</div>
<div>
<asp:TextBox ID="txtBox2" runat="server"></asp:TextBox>
</div>
<div>
<asp:TextBox ID="txtBox3" runat="server"></asp:TextBox>
</div>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Button" OnClick="btnSubmit_Click" />
</div>
protected void btnSubmit_Click(object sender, EventArgs e)
{
string game = this.txtBox1.Text.ToString();
string Namepref = this.txtBox2.Text.ToString();
string Amount = this.txtBox3.Text.ToString(); //box3;
}
I fixed it by moving my form to only get the < asp:X, because i discovered that if the < asp:x was nested too deep within a ton of divs, it didnt work properly.
It may have been due to something else, but it worked for me.
Related
I'm trying to add a new seller into database but all the values i get from the input text are null
this is the form
<div class="mb-3">
<label for="SNameTb" class="form-label">Seller FirstName</label>
<input type="text" class="form-control" id="Snametb" runat="server">
</div>
And this how i'm trying to get the values
string Sname = Snametb.Value;
You missing the closing "/> in that input box.
So, this:
<div class="mb-3">
<label for="SNameTb" class="form-label">Seller FirstName</label>
<input type="text" class="form-control" id="Snametb" runat="server" value="" />
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
and button click code:
protected void Button1_Click(object sender, EventArgs e)
{
Debug.Print("Text box value = " + Snametb.Value);
}
So, add the "/>" to your markup (maybe a cut+paste issue).
And not required, but good idea to add the value="" attribute also.
add runat= 'server' to the text input
I have page where switch toggle is used and when toggle is switched to on and off update value in database.
<div class="card rounded" style="margin-bottom: 20px">
<asp:Image ID="coverpic" runat="server" CssClass="card-img" Height="500" AlternateText="Cover Picture" />
<div class="card-img-overlay card-inverse social-profile d-flex " style="top: auto">
<div class="align-self-center rounded" style="margin: auto">
<asp:Image ID="profilepic" runat="server" CssClass="img-circle" Height="100" Width="100" AlternateText="Profile Picture" />
<h4 class="card-title">
<asp:Label ID="fullname" runat="server"></asp:Label></h4>
<h6 class="card-subtitle">
<asp:Label ID="unametop" Font-Size="Large" runat="server"></asp:Label></h6>
<div class="switch">
<label>
User
<input id="userrole" type="checkbox" runat="server"><span class="lever switch-col-red"></span>Admin</label>
</div>
</div>
</div>
</div>
As you can see input checkbox with switch class is used, when i turn switch on i want to update user role in database to admin and when switch is turned off i want to update user role to normal user. Please help if you can.
By design the input control doesn't have a click event, so in my opinion the most elegant way to do it is with jQuery, if you're using it. See this answer.
Alternatively you could do something like this:
<script>
function triggerpostback(obj) {
//check if textbox is readyonly
if (obj.readOnly) {
__doPostBack('Button', "");
}
}
</script>
<asp:Button ID="Button" runat="server" Text="Button" OnClick="Button_Click" style="display:none" />
<input id="userrole" type="checkbox" runat="Server" onclick="triggerpostback(this)" />
Code-behind:
protected void Button_Click(object sender, EventArgs e)
{
// Add the code to update user role in database
}
I have this form where there is a dropdown list. When user select the list, the corresponding data appeared in the text boxes.
Now, I need to make the data disappear if the user clicks on the radio button - Yes in that section; not when user click submit at the end of the form.
My code below doesn't make the data disappear.
Here's my code that I have attempted :
protected void OtherVendor_SelectedIndexChanged(object sender, EventArgs e)
{
// use User entered infor - use Insert ?
if (OtherVendor.Text == "Yes")
{
txtSupplierEmail.Text = string.Empty;
txtSupplierName.Text = string.Empty;
txtSupplierName.Text = string.Empty;
txtSupplierCttPerson.Text = string.Empty;
txtSupplierCttNumber.Text = string.Empty;
txtSupplierEmail.Text = string.Empty;
txtSupplierName.Text = string.Empty;
txtSupplierCttPerson.Text = string.Empty;
txtSupplierCttNumber.Text = string.Empty;
}
}
And my html code :
<div class="row">
<div class="col-lg-12">
<div id="divAP"
class="panel panel-primary">
<div class="panel-heading">Supplier Information</div>
<div class="panel-body">
<div class="col-sm-6 col-lg-6">
<asp:UpdatePanel ID="UpdatePanel4"
runat="server">
<ContentTemplate>
<div class="form-group row">
<label class="col-md-4 control-label"
for="inputRefNo">Vendor :
<span style="color: red">*</span>
</label>
<div class="input-group col-md-8">
<asp:DropDownList ID="ddlSupplier"
OnSelectedIndexChanged="ddl1_SelectedIndexChanged"
runat="server"
CssClass="form-control"
AutoPostBack="true"></asp:DropDownList>
</div>
</div>
<div class="form-group row">
<label class="col-md-4 control-label"
for="radios">Others Vendor
<span style="color: red">*</span>
</label>
<div class="col-md-4">
<label class="col-md-4 control-label">
<asp:RadioButtonList ID="OtherVendor"
runat="server"
OnSelectedIndexChanged="OtherVendor_SelectedIndexChanged"
RepeatColumns="2"
RepeatDirection="Horizontal"
AutoPostBack="true">
<asp:ListItem Text="Yes"
value="Yes"></asp:ListItem>
<asp:ListItem Text="No"
value="No"></asp:ListItem>
</asp:RadioButtonList>
</label>
</div>
<%--To insert code for letting user filling up Supplier email - to have auto-pop up and enable overwrite etc. --%>
</div>
<div class="form-group row">
<label class="col-md-4 control-label"
for="inputRefNo">Vendor Email:
<span style="color: red">*</span>
</label>
<div class="col-md-4 control-label"
for="inputRefNo">
<asp:TextBox ID="txtSupplierEmail"
runat="server"
Text="Label"
Width="100%"
CssClass="form-control"></asp:TextBox>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
I'm not sure whether it is a typo or not, Anyway in your markup you were specified as AutoPostBck but actually it should be AutoPostBack; which means Bck should be Back
One more suggestion for you, instead of using OtherVendor.Text in your condition you can format the condition as like this:
if (OtherVendor.SelectedValue == "Yes")
{
// Clear controls here
}
else
{
// Do something else
}
Help!
I have a login and and a signup html form, each toggle on clicking respective links on the form itself and there is no postback or page refresh event.
the problem is asp.net does not allow me to have two runat=server forms.
i can access input fields of the forms.
i have added asp:Button in place of input type="submit" in the forms in order to acces onclick methods, but then again asp.net does not allow me to add asp button (server side control) when i remove runat="server" from any of the forms which is having this asp button!
The question:
How can i access submit button of the forms so that i can perform necessary code behind operations for signup and login
Is there a way to achieve my goal? (may be some way to hide one of the forms in starting and show it when i click toggle link )
Here's the login register form's
Code:
<div>
<header>
</header>
<section>
<div id="container_demo" >
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<form id="form1" action="#" runat="server" >
<h1>Log in</h1>
<p>
<label for="username" class="uname" data-icon="u" > Your email </label>
<input id="username" name="username" runat="server" required="required" type="text" placeholder="myusername or mymail#mail.com"/>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<input id="password" name="password" runat="server" required="required" type="password" placeholder="eg. X8df!90EO" />
</p>
<p class="keeplogin">
<input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
<label for="loginkeeping">Keep me logged in</label>
</p>
<p class="login button">
<asp:Button Text="Submit" runat="server" OnClick="Submit" />
<!--<input type="submit" value="Login" />-->
</p>
<p class="change_link">
Not a member yet ?
Join us //toggle link
</p>
</form>
</div>
<div id="register" class="animate form">
<form id="form2" action="#" runat="server" >
<h1> Sign up </h1>
<p style="margin-top:4px;margin-bottom:2px;">
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<input id="usernamesignup" name="usernamesignup" runat="server" required="required" type="text" placeholder="mysuperusername690" />
</p>
<p style="margin-top:4px;margin-bottom:2px;">
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<input id="emailsignup" name="emailsignup" runat="server" required="required" type="email" placeholder="mysupermail#mail.com"/>
</p>
<p style="margin-top:4px;margin-bottom:2px;">
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<input id="passwordsignup" name="passwordsignup" runat="server" required="required" type="password" placeholder="eg. X8df!90EO"/>
</p>
<p style="margin-top:4px;margin-bottom:2px;">
<label for="mob1" class="uname" data-icon="u">Your mob no.</label>
<input id="mob" name="mob" runat="server" required="required" type="text" placeholder="9450.." />
</p>
<p class="signin button">
<asp:Button Text="Submit" runat="server" OnClick="Submitr" />
<!--<input type="submit" value="Sign up"/> -->
</p>
<p class="change_link">
Already a member ?
Go and log in //toggle link
</p>
</form>
</div>
</div>
</div>
</section>
</div>
Ok finally i got it after applying my brains!
this is what i did:
applied asp buttons on both server side forms
on signup form:
<asp:Button Text="tologin" runat="server" OnClick="changetologin" ForeColor="#1DA2C1" BackColor="#F7F8F1" />
on login form:
<asp:Button Text="Join us" runat="server" OnClick="changetosignup" ForeColor="#1DA2C1" BackColor="#F7F8F1" />
on page load:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["form2"] == null && Session["form1"] == null) //show login hide signup
{
form1.Visible = true;
form2.Visible = false;
}
if (Session["form2"] != null && Session["form1"]==null ) //show signup hide login
{
form1.Visible = false;
form2.Visible = true;
Session["form2"] = null;
}
if (Session["form1"] != null && Session["form2"] == null) //show login hide signup
{
form1.Visible = true;
form2.Visible = false;
Session["form1"] = null;
}
}
on signup form on click of toggle button:
protected void changetologin(object sender, EventArgs e)
{
Session["form1"] = "clicked";
Response.Redirect("#tologin");
}
on login form on click of toggle button:
protected void changetosignup(object sender, EventArgs e)
{
Session["form2"] = "clicked";
Response.Redirect("#toregister");
}
In short:
combo of form visible property and session variable did the trick !!
I am using jquery ui tabs and tooltip and it works perfectly.Heres the code for jquery-ui tabs
$(function() {
$( "#tabs" ).tabs();
$(document).tooltip();
});
I wrote another function which i want to call from code behind on button click event
function setDiv(type){
alert(type);
}
Heres my codebehind function.
protected void btnNext_Click(object sender, EventArgs e)
{
pnlResult.Visible=True;
ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "script type='text/javascript' language='javascript'",
"setDiv('highlight')();", true);
}
The problem is when i click the btnNext button ,alert saying highlight is showing.After that i am getting the object expected error on the aspx page and the error points on
<div id="tabs-1">(div used for creating tabs)
Here's my aspx page
<div id="tabs">
<ul>
<li>Nunc tincidunt</li>
<li>Proin dolor</li>
<li>Aenean lacinia</li>
</ul>
<div id="tabs-1">//Here's where i am getting the error
<p>
<asp:Panel ID="pnlResult" runat="server" Visible="false">
<div id="divResult" runat="server">
<span id="spIcon" style="float:left;margin-left:3px;"></span>
Successfully inserted
</div>
</asp:Panel>
<table style="width:50%" id="tbl" runat="server" >
<tr>
<td>
Your Name:
</td>
<td>
<input id="name" title="Enter your name here" />
</td>
</tr>
<tr>
<td >
<span class="ui-icon ui-icon-alert" style="float:left;margin-left:3px;"></span>
<input type="button" id="btnSubmit" value="Submit" />
</td>
</tr>
</table>
</p>
</div>
<div id="tabs-2">
<p>Tab2.</p>
</div>
<div id="tabs-3">
<p>Tab3.</p>
<p>Tab is placed.</p>
</div>
</div>
Your script that you are building in your code behind is malformed, try this:
ScriptManager.RegisterClientScriptBlock(
Page,
typeof(Page),
"setDiv",
"<script type='text/javascript'>setDiv('highlight');</script>",
false);