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";
}
Related
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 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".
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 am still learning the basics of C# so any help would be appreciated. I have a series of asp:TextBox's. In the code behind, I am getting the value of these boxes. Is there a way to have a panel hidden until a user clicks submit then have the values display?
Here is the HTML for one of the boxes and the panel:
<asp:TextBox ID="txtTitle" runat="server></asp:TextBox>
<asp:Panel ID="PDFPanel" runat="server"></asp:Panel>
The button:
<asp:Button ID="btn_Submit" runat="server" Text="Button" OnClick="btnSubmit"/>
and the code-behind for it:
string Title = txtTitle.Text;
public void btnSubmit(Object sender, EventArgs e)
{
}
There are about 50 fields, so I am not showing all of it but if I can get direction on one I can replicate for the rest. Please let me know if I need to show any additional code
I am sorry if this is simple, but like I said, I am still an entry level developer. Thanks in advance!
Unless I've misunderstood what you're asking, this should be fairly simple.
<asp:Panel ID="PDFPanel" runat="server" Visible="False">
<div>
<asp:Literal id="litTitle" runat="server" />
</div>
</asp:Panel>
then in your click method:
litTitle.Text = txtTitle.Text;
PDFPanel.Visible = true;
Set the Panel's visibility to false by default
<asp:Panel ID="PDFPanel" runat="server" Visible="false">
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
</asp:Panel>
then on the Button's click event set the visibility to true
public void btnSubmit(Object sender, EventArgs e)
{
PDFPanel.Visible = true;
// do something else...
}
I have a server method that I want to run from a link inside the LayoutTemplate of my ListView. I've found that it doesn't fire unless I move the link outside the LayoutTemplate. Is there any other way to run a Serverside event from an anchor or LinkButton inside of a LayoutTemplate? I thought about trying a link with a CommandName and capturing that inside of OnItemCommand but I don't know if that works inside of the LayoutTemplate.
My anchor inside the LayoutTemplate:
<a runat="server" id="proceedCheckout" OnServerClick="startCheckout">
The server method:
public void startCheckout(object sender, EventArgs e)
{....
I attempted to replicate your issue but with no luck. It seems to work for me. I have included my test mark-up and code so that you can see if you are missing something:
Markup
<asp:ListView ID="listView1" runat="server">
<LayoutTemplate>
<div>
<asp:LinkButton ID="linkButton1" runat="server" OnClick="LinkButton_Click" Text="Link Button"></asp:LinkButton>
</div>
<div runat="server" id="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<div><%# Container.DataItem %></div>
</ItemTemplate>
</asp:ListView>
Code
protected void Page_Load(object sender, EventArgs e)
{
List<string> data = new List<string>();
data.Add("Item 1");
data.Add("Item 2");
data.Add("Item 3");
listView1.DataSource = data;
listView1.DataBind();
}
protected void LinkButton_Click(object sender, EventArgs e)
{
//...
}
Hope this helps.