I have tried this:
Inside AddClass.aspx
<input type="text" id="txtClass" value="ClassName"/>
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
Inside AddClass.aspx.cs:
When Button (ID:btnSave) is clicked:
protected void btnSave_Click(object sender, EventArgs e)
{
string a=Request.Form["txtClass"];
}
And I am not getting value in string 'a' .
Is there any way of getting value of html textbox in .cs code.
You need to add name attribute in your text box,
<input type="text" id="txtClass" name="txtClass" value="ClassName"/>
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
than you will get the value on submit,
protected void btnSave_Click(object sender, EventArgs e)
{
string a=Request.Form["txtClass"];
}
Try this:
<input type="text" id="txtClass" value="ClassName" runat="server" />
protected void btnSave_Click(object sender, EventArgs e)
{
string a = txtClass.Value;
}
Without the runat="server" attribute being added, the server-side code has no knowledge of this HTML control. Note also, that the property to fetch the content server-side for a HTML text input is "Value", not "Text".
Related
I have a textbox filled at the page load. I want to check the value of the textbox is changed or not in the "update" button press. Any solution? TIA.
Well, you could say use client side javascript.
But, you could also do this:
Say this text box:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Done - continue" OnClick="Button1_Click" />
And our code could be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// code here to setup and load controls
TextBox1.Text = "Dog";
ViewState["TextBox1"] = "Dog";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != (string)ViewState["TextBox1"])
{
// then text box was changed
}
}
Hi i am busy creating a asp.net project that needs to get three values from the user. I am using a textbox with a range textmode to get these values and the first time the user moves the slider it works perfectly but after that the user can.t adjust any of the textboxes anymore.
I tried using a slider extender but this does not seem to work and only a textbox shows.
my html code to create the textboxes and labels showing the value
body>
<form id="form1" runat="server">
<p>
Quantum 1 Length:
<asp:TextBox ID="txtVal1" runat="server" TextMode="Range" min="1" Max="8" OnTextChanged="txtVal1_TextChanged1" AutoPostBack="True"></asp:TextBox>
<asp:Label ID="lblVal1" runat="server" Text="/////"></asp:Label>
</p>
<p>
Quantum 2 Length:
<asp:TextBox ID="txtVal2" runat="server" TextMode="Range" min="1" Max="8" OnTextChanged="txtVal2_TextChanged1" AutoPostBack="True"></asp:TextBox>
<asp:Label ID="lblVal2" runat="server" Text="/////"></asp:Label>
</p>
<p>
Quantum 3 Length:
<asp:TextBox ID="txtVal3" runat="server" TextMode="Range" min="1" Max="8" AutoPostBack="True" OnTextChanged="txtVal3_TextChanged1"></asp:TextBox>
<asp:Label ID="lblVal3" runat="server" Text="/////"></asp:Label>
</p>
<p>
<asp:Button ID="btnNext" runat="server" AutoPostBack="true" Height="24px" OnClick="btnNext_Click" style="margin-left: 0px" Text="Next" Width="150px" />
<script type="text/javascript" __designer:mapid="129">
function NextPage() {
window.open('Execution.aspx', 'Execution');
}
</script>
<asp:Label ID="lblError" runat="server" ForeColor="Red" Text="Invalid input; Please ensure that Quantum 1 <= Quantum 2 <= Quantum 3" Visible="False"></asp:Label>
</p>
</form>
</body>
My C# code used to control the boxes and change their values
public partial class Quantums : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
txtVal1.Text = HttpContext.Current.Session["Q1"].ToString();
}
catch { }
try
{
txtVal2.Text = HttpContext.Current.Session["Q2"].ToString();
}
catch { }
try
{
txtVal3.Text = HttpContext.Current.Session["Q3"].ToString();
}
catch { }
lblVal1.Text = txtVal1.Text;
lblVal2.Text = txtVal2.Text;
lblVal3.Text = txtVal3.Text;
//UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger() { ControlID = "btnNext" });
}
protected void txtVal1_TextChanged1(object sender, EventArgs e)
{
HttpContext.Current.Session["Q1"] = txtVal1.Text;
}
And then the same code for textbox 2 and 3. Any suggestions on why the textboxes vaklue keeps resetting and autopostback is enabled on all three buttons.
I just noticed you were missing if(Page.IsPostBack()) return;. Your problem is that every time you change your textbox, the 'Page_Load' event is enacted, then the respective event handler (see page life cycle). So you were setting the text to the sessions, then setting the sessions to the text, effectively doing nothing.
You actually don't need anything for this page in the Page_load method, at least for now. Try the following:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtVal1_TextChanged(object sender, EventArgs e)
{
Session["Q1"] = txtVal1.Text;
lblVal1.Text = txtVal1.Text;
}
protected void txtVal2_TextChanged(object sender, EventArgs e)
{
Session["Q2"] = txtVal2.Text;
lblVal2.Text = txtVal2.Text;
}
protected void txtVal3_TextChanged(object sender, EventArgs e)
{
Session["Q3"] = txtVal3.Text;
lblVal3.Text = txtVal2.Text;
}
You can then access the user input values using the respective session values. For example:
protected void Submit(object sender, EventArgs e)
{
List<string> answers = new List<string>();
answers.Add(Session["Q1"].ToString());
answers.Add(Session["Q2"].ToString());
answers.Add(Session["Q3"].ToString());
// Do stuff with answers
}
I have a WebForms page that I would like to inject some additional controls into at runtime. Currently I am achieving this in the Page_Load event using a Literal control.
For example the page looks like this (note that the TextBox1 is not an asp control just to show that it works):
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<input id="TextBox1" type="text" runat="server"/>
<asp:Literal ID="Literal1" runat="server" Visible="false"></asp:Literal>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</asp:Content>
And the code behind:
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Visible = true;
if (!IsPostBack) Literal1.Text = "<input id=\"TextBox2\" type=\"text\" runat=\"server\"/>";
}
This works fine and both textboxes appear on the screen but if I type a value in to both and trigger a postback only the value of TextBox1 is retained.
I have tried moving my code to OnPreRender and OnPreLoad but still have the same issue.
I have noticed that when I view the page source TextBox1 has a UniqueId (e.g. ctl00$MainContent$TextBox1) while Textbox2 still has runat="server" as an attribute.
You can't inject server controls like this. You would need to add them as suggested in #Arvin's answer.
However, you use inject non-ASP.NET HTML controls similar to what you are doing and get their values.
From your code change the input's id to a name and drop the runat="server":
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Visible = true;
if (!IsPostBack) Literal1.Text = "<input name=\"TextBox2\" type=\"text\" />";
}
Then you can get it's value on postback:
string textbox2value = Request.Form["TextBox2"];
Then, if you want to add the control on postback with it's value:
Literal1.Text = "<input name=\"TextBox2\" type=\"text\" value=\"" +
Server.HTMLEncode(textbox2value) + "\" />";
if you want to inject a textbox you should use placeholder like this :
<input id="TextBox1" type="text" runat="server"/>
<asp:PlaceHolder ID="plh" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
protected void Page_Load(object sender, EventArgs e)
{
TextBox TextBox = new TextBox();
TextBox.ID = "TextBox2";
plh.Controls.Add(TextBox);
}
protected void Button1_Click(object sender, EventArgs e)
{
var Text1 = TextBox1.Value;
var Text2 = Request.Form["TextBox2"];
}
I have a repeater that has a linked button inside it. I want to get some data in linked button click event. how should I set my extra data and get them in click event? (consider that i want to concat some items in my property)
aspx code:
<asp:Repeater ID="rpSliderRest" runat="server">
<ItemTemplate>
<!-- ITEM-->
<div class="span2">
<div class="thumbnail product-item">
<img src='<%# Eval("PrintTemplate_URL").ToString().Replace("~", "../..") %>'>
</div>
<h6><%# Eval("PrintTemplate_Desc") %></h6>
<asp:LinkButton ID="lbtn1" runat="server" class="btn btn-large btn-block" OnClick="LinkButton1_Click"
Prperty='<%# string.Format("{0};{1}",Eval("PrintTemplate_URL").ToString(),Eval("PrintTemplate_ID").ToString()) %>'>Select »</asp:LinkButton>
</div>
<!-- ITEM-->
</ItemTemplate>
</asp:Repeater>
aspx.cs code:
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lbtn = sender as LinkButton;
string MyProperty=??????????
}
You can use the Attributes collection.
For example:
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lbtn = sender as LinkButton;
String MyProperty = lbtn.Attributes["PropertyName"];
}
Personally I'd go down the route of using the link button commandArgument property - as that's what it's there for.
So:
<asp:LinkButton ID="lbtn1" runat="server" class="btn btn-large btn-block" OnClick="LinkButton1_Click"
CommandArgument='<%# string.Format("{0};{1}",Eval("PrintTemplate_URL").ToString(),Eval("PrintTemplate_ID").ToString()) %>'>Select »</asp:LinkButton>
Then
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lbtn = sender as LinkButton;
string MyProperty= lbtn.CommandArgument;
}
I added a checkbox to my form.
When the checkbox is checked, some information (TextBox, Label etc.) should appear right under the checkbox.
How can I do this?
Code:
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" />
C#:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
}
Don't forget autopostback = true
<asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="true" />
<asp:Panel runat="server" ID="panel1"></asp:Panel>
-
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
panel1.Controls.Add(new Label { Text = "Text goes here" });
}
This allows you to add any control you want.
Just add TextBox, Label etc. controls and make them invisible.
Then in the CheckBox1_CheckedChanged function make them visible.
This is done by setting the bool Visible property
<asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" />
<asp:TextBox ID="textBox" runat="server" Visible=false />
and
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
textBox.Visible = true;
}
Add a new literal control under your checkbox tags,
<asp:Literal id="lblMsg" runat="server"/>
then in your CheckBox1_CheckedChanged event to this:
lblMsg.Text = "Checked";
and yes set the AutoPostBack property of your checkbox to true
I would suggest doing this using javascript. Your users won't have to "postback" and the feeling on the application will be better, and you will reduce the server charge.
Using jQuery, you can use something like this:
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#chk").onclick(function() {
$("#content").toggle();
});
});
</script>
<input type="Checkbox" id="chk"/>
<div id="content" style="display:none">
<asp:TextBox runat="Server" id="oneOfYourControls" />
</div>
jQuery is not mandatory... you can use standard simple getElementById().
The only drawback, is that you cannot dynamically build the content, but in most case it's a not actually a matter
In Checked Changed Event write your code like this:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Label1.Text = "Text";
}