reset /clear button? - asp.net

hi im using visual studio 2010 and asp
i just created simple form with submit and clear button
and i want to clear all textboxes
the sumbit button is working. but idk for clear button.
note:
the button1_click is subit button,
button2_click is clear button
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IsPostBack)
Response.Write("Success");
}
protected void Button2_Click(object sender, EventArgs e)
{
//////// -- i dont know what to put here huhuhu help
}
}

Assuming you have text boxes called Text1, Text2, Text3 (etc.) -->
<asp:textbox id="Text1" runat="server" />
<asp:textbox id="Text2" runat="server" />
<asp:textbox id="Text3" runat="server" />
then to clear the entry in the text boxes server side, you want to have the following code in the Button2_click() method (assumed to be the Clear button) -->
Text1.Text = "";
Text2.Text = "";
Text3.Text = "";

Assuming that the ID of the textboxes are TextBox1 and TextBox2, here's how it's done:
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
EDIT
To avoid the required validation when Button2 is clicked, first set the ValidationGroup property of the RequiredFieldValidator to any non empty value, let's say Submit:
<asp:RequiredFieldValidator ID="rq1" runat="server" ValidationGroup="Submit" />
then the submit button must have the same value for its ValidationGroup property so it will validate the textboxes when it's clicked:
<asp:Button ID="Button1" runat="server" ValidationGroup="Submit" />
finally do not set the ValidationGroup property of the clear button so it will ignore the validation defined in rq1 when it's clicked:
<asp:Button ID="Button2" runat="server" />
For more information regarding ValidationGroup, see here: http://msdn.microsoft.com/en-us/library/ms227424%28v=vs.100%29.aspx

Related

Adding a Value Attribute to on a AspxButton

<dxe:ASPxButton ID="getProgram" runat="server" Text="Programs" SkinID="MenuButton" OnClick="Button_Click">
</dxe:ASPxButton>
Here is my Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
this.getProgram.Attributes.Add("Value", "View Progams Button");
}
I tried this it didn't work. Is there any other way I can achieve this?
I don't know what a <dxe:ASPxButton> is but a regular <asp:Button> has its value property set via the 'Text' attribute: -
<asp:Button ID="button" runat="server" Text="click me :)" />

transfer listbox items from one asp.net page to another using session

Transfer listbox items from one asp.net page to another using session. I want to transfer the items of a list box from one asp.net page to another. The code somehow is throwing error. The items in the listbox are not being retrieved either. I want to do the same with a check box list. Hopefully the listbox issue will help[ me solve that too. Please advice.
First Page
<asp:ListBox ID="SelectedItems" runat="server" SelectionMode="Multiple"/>
<asp:Button ID="sbmtButton" runat="server" Text="Submit" Width="152px" OnClick="sbmtButton_Click" />
.cs for the First Page
protected void sbmtButton_Click(object sender, EventArgs e)
{Session["wrd"] = SelectedItems;Server.Transfer("~/aftrSubmit.aspx";);}
Second Page
.cs for the Second Page
protected void Page_Load(object sender, EventArgs e)
{if (!this.IsPostBack){Prescription_list = (ListBox)Session["wrd"];}}
First Page ASPX Code:
<asp:ListBox ID="SelectedItems" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="A"></asp:ListItem>
<asp:ListItem Text="B"></asp:ListItem>
<asp:ListItem Text="C"></asp:ListItem>
</asp:ListBox>
<asp:Button ID="sbmtButton" runat="server" Text="Submit" Width="152px" OnClick="sbmtButton_Click" />
First Page C# Code-Behind:
protected void sbmtButton_Click(object sender, EventArgs e)
{
ListItem[] x = new ListItem[SelectedItems.Items.Count];
SelectedItems.Items.CopyTo(x, 0);
Session["wrd"] = x;
Server.Transfer("~/aftrSubmit.aspx");
}
Second Page ASPX Code:
<asp:ListBox ID="Prescription_list" runat="server" SelectionMode="Multiple"/>
Second Page C# Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
ListItem[] x = (ListItem[])Session["wrd"];
Prescription_list.Clear();
Prescription_list.Items.AddRange(x);
}
}

Get current TextBox Text value in a button onClick event - asp.net

I have such a page
<form runat="server" id="NewForm">
Name: <asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button ID="AddNewName" runat="server" Text="Add" OnClick="AddNewName_Click" />
<asp:Label ID="NewName" runat="server"></asp:Label>
</form>
In the code behind, I have a Page_Load which assign a value to the TextBox Name.
protected void Page_Load(object sender, EventArgs e)
{
Name.Text = "Enter Your Name Here";
}
Then upon the Click on the button AddNewName, I will write it in the Label NewName
protected void AddNewDivision_Click(object sender, EventArgs e)
{
NewName.Text = Name.Text;
}
But, no matter what I input in the Name TextBox, the Label only displays "Enter Your Name Here". It never updates to the actual content in the Name TextBox. What am I doing wrong with this code?
The problem is that you are always overwriting the changed value in Page_Load. Instead, check the IsPostBack property:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
Name.Text = "Enter Your Name Here";
}
You are re-assigning the text to Name every time Page_Load that over writes the text you entered in TextBox before reach AddNewDivision_Click event. To assign it once on page load and do not over write the subsequent calls you can use Page.IsPostBack property.
if(!Page.IsPostBack)
Name.Text = "Enter Your Name Here";
Or you can assign the text in design html and remove the statement from page_load
<asp:TextBox ID="Name" runat="server" Text="Enter Your Name Here"></asp:TextBox>
Another immediately obvious issue is:
<form runat="server" id="NewForm">
Name: <asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button ID="AddNewName" runat="server" Text="Add" **OnClick="AddNewName_Click"** />
<asp:Label ID="NewName" runat="server"></asp:Label>
</form>
Not the asterisks above. Then, you were wondering why this didn't run:
protected void **AddNewDivision_Click**(object sender, EventArgs e)
{
NewName.Text = Name.Text;
}
Again, note the asterisks. You weren't calling the correct void, in fact you were probably calling a void that didn't even exist.

Why is my repeater control empty on postback?

I think this is a "doh" moment caused by me not having dome WebForms dev for a few years..
I have a repeater which which contains a bunch of checkboxes:
<asp:Repeater EnableViewState="true" ID="IDTypesRepeater" runat="server" OnItemDataBound="IdTypesRepeaterItemDataBound">
<HeaderTemplate/>
<ItemTemplate>
<asp:CheckBox EnableViewState="true" ID="chkIdType" Text="<%# ((KeyValuePair<string,int>)Container.DataItem).Key %>" runat="server" />
<asp:HiddenField ID="idType" Value="<%# ((KeyValuePair<string,int>)Container.DataItem).Value %>" runat="server"/>
<br />
</ItemTemplate>
</asp:Repeater>
I need to get the checkboxes that are selected in the code behind:
foreach (RepeaterItem repeaterItem in IDTypesRepeater.Items)
{
if ( ((CheckBox)repeaterItem.FindControl("chkIdType")).Checked )
{
// Do something
}
}
But on postback, this code isn't working! I know about always databinding a repeater, so I've done this:
protected void Page_Load(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
So this repopulates the repeater, but the Update code never finds any checked checkboxes.. Any ideas?
Bind in the Page_Init event
protected void Page_Init(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
Be sure to use the !Page.IsPostBack method in your pageload.
Otherwise, the Repeater will keep getting reset, and all your checkboxes
will be in there default value (unchecked)
This should fix it. You are binding the control on postback hence losing the values. You can bind it after handling any event to show the updated record.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
}

Setting value in html control in code behind without making server control

Setting value in html control in code behind without making server control
<input type="text" name="txt" />
<!--Please note I don't want put 'runat="server"' here to get the control in code behind-->
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//If I want to initlize some value in input, how can I set here
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Request["txt"] // Here I am getting the value of input
}
This answer comes from memory, so I apologize if it's slightly off.
What you can do is use an ASP.NET inline expression to set the value during the loading of the page.
First, add a property in the code-behind of your page.
protected string InputValue { get; set; }
In the Page_Load event, set the value of the property.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.InputValue = "something";
}
}
Finally, add an inline expression in your page markup like this:
<input type="text" name="txt" value="<%= this.InputValue %>" />
This will let you set the value of the input element without making it a server-side tag.
Elements that are not set runat="server" are considered plain text and collected into as few Literal objects as possible (one between each serverside control). I suppose if you really really wanted to, you could try to find the correct Literal (or maybe LiteralControl) object in Page.Controls, and modify it, but I'd definately recommend against it.
What's so terrible about setting it runat="server" ?
And yes, of course you can also use <%= %>. Embedded code blocks. They're evaluated at Render time, so it should be relatively safe to do so.
Add an expression in your page like this:
<input class="field" id="locality" name="loc" value="<%= this.inputtypeCT %>"/>
Add a property in the code-behind of your page:
protected string inputtypeCT;
In the Page_Load event, set the value of the property:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.inputtypeCT = "test"
}
}
<input type="text" name="txt" value="<%= System.Web.HttpUtility.HtmlEncode(this.InputValue) %>" />
Add runat server
or use Asp controls like below
<asp:TextBox type="text" runat="server" class="demoHeaders" id="datepicker" ClientIDMode="Static" Text=""/>
Also make sure that you used ClientIDMode="Static" for the naming of control to be in clinet as like server.
Enjoy!

Resources