Clear content from fields after save .net c# - c#

I have aspx form with textboxes and gridview.
When i insert data to textbox it's shows on the gridview after clicking "Save".
I need to clear the content of fieds after i click on "Save".
I tried alot of things and searched in google but nothing works.
I am new at .net.. Can someone help please?
This is my save button fuction:
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
grdvUsers.DataBind();
}
This is my pageLoad:
protected void Page_Load(object sender, EventArgs e)
{
grdvUsers.DataSource = Context1.sp_GetAllUserTest(); // select all users into gridview. datasorce = the data we want to dispaly.
grdvUsers.DataBind();
}

Simply make a method for clearing the controls that you want to clear:
private void ClearControls()
{
txtName.Text =""; // resetting textbox
txtEmail.Tex="";
txtMobile.Text ="";
ddlSomeDropDown.SelectedIndex = -1; // reset dropdown
somecheckBox.Checked = false; // reset checkbox
someRadio.Checked = false; // reset radio
..................
.................
// more controls here
}
and call it after saving :
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
grdvUsers.DataBind();
ClearControls();
}

Just assign text fields a empty string.
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
txtName.Text = String.Empty;
txtEmail.Text= String.Empty;
txtMobile.Text= String.Empty;
txtBirthdate.Text= String.Empty;
grdvUsers.DataBind();
}

You can also do this.
You can clear date and datepicker with DatetimePicker1.Clear(); or with the provided ID

Related

display a value of the text box when the button is clicked in a web form

I want when the button is clicked a string comes on the textbox based on user selection, I don't know how to do that
this is the code:
protected void Button1_Click(object sender, EventArgs e)
{
//DECLARE A STRING VARIABLE
String choice = "";
//LET THE USER SELECT BETWEEN TWO VALUES TO DISPLAY PRICE OF THE BOOK
Response.Write("<script>choice=window.prompt('Which Currency you want the price display ? 1 : Dollar or 2: Euro');" + "if(choice=='1'){ document.writeln('$22.43');}" + "if(choice=='2') document.write(' 20.55 EUR');" + "</script>");
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
Try this:
string yourString = "heyheyhey";
TextBoxName.Text = yourString;
1.Use label and edit text from label1 to
Which Currency you want the price display ? 1 : Dollar or 2: Euro.
2.Use another label in which you want display the currency and name it as lbl_currency.
3.Use a text box and name it as txt_choice.
4.use button name it as btn,double click button and in the click event enter this code.
protected void btn_Click(object sender, EventArgs e)
{
if(Convert.ToInt32(txt_choice.Text)==1){
lbl_currency.Text="$22.43";
}
else if(Convert.ToInt32(txt_choice.Text)==2){
lbl_currency.Text="20.55 EUR";
}
else{
MessageBox.Show("Wrong Input");
}
}
You need to have control called RadioButtonList for selecting your currency then in code behide
protected void btn_Click(object sender, EventArgs e)
{
if(yourID_RadioButtonList.SelectedValue)=="Something"){
lbl_currency.Text="Your_Value1";
}
else {
lbl_currency.Text="Your_Value2";
}
}

Page_Load run Button_Click

I have a web-part in SharePoint 2013 which adds the new items from excel. The web-part contains upload control, buttons and textbox. I choose the document from upload control and click the button to load items in SP, if it was successfull I see "Successfull" in textbox or "Not successfull" in another way.
My problem: if i refresh page with web-part, textbox still contains the old text, but i want to see it empty after every refresh.
I try to use Page.IsPostBack, but I think I didn't properly use it.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
textbox1.Text = "";
}
protected void btn3_Click(object sender, EventArgs e)
{
if (!Page.IsPostBack)
return;
if(!upload.HasFile)
{
textbox1.Text += "You didn't choose an Excel file";
return;
}
...
}
<asp:Button ID="btn3" runat="server" OnClick="btn3_Click" Text="Add Items" />
In such case, you can implement a special code block to detect browser refresh as
private bool refreshState;
private bool isRefresh;
protected override void LoadViewState(object savedState)
{
object[] AllStates = (object[])savedState;
base.LoadViewState(AllStates[0]);
refreshState = bool.Parse(AllStates[1].ToString());
if (Session["ISREFRESH"] != null && Session["ISREFRESH"] != "")
isRefresh = (refreshState == (bool)Session["ISREFRESH"]);
}
protected override object SaveViewState()
{
Session["ISREFRESH"] = refreshState;
object[] AllStates = new object[3];
AllStates[0] = base.SaveViewState();
AllStates[1] = !(refreshState);
return AllStates;
}
In the button click you can do it as
protected void btn3_Click(object sender, EventArgs e)
{
if (isRefresh == false)
{
Insert Code here
}
}

Asp.net Session state Trying to keep contents of textbox when returned to page

ive two pages, one with a text box and a button, the other with a button a label. What i want to do is to display contents of the textbox on page 1, in the label of the page2 on button click. and then when i click the button to return to page1. preverse whats entered in the textbox on page1. sorry if its confusing. heres my code
page1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
Session["fstName"] = txtBox.Text;
Response.Redirect("Page2.aspx");
}
page2.aspx
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["fstName"].ToString();
lblPage2.Text = a;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}
Where do you set the value of the text box when returning to WebForm1.aspx? It should be very similar to what you have for the label on Page2.aspx. Something like:
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["fstName"].ToString();
txtBox.Text = a;
}
At worst, you may need to wrap some error checking around it. Maybe something like:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
if (Session["fstName"] != null)
{
string a = Session["fstName"].ToString();
txtBox.Text = a;
}
}

Asp.net listview does not display data correctly

My problem is that when i display data in listview for first time then it show correctly but when i display data second time then listview does not update the data correctly. I have made a function for databinding with listview which i have called in pageLoad and some other method.
Can anyone please give me a solution about this ?
I have also uploaded my source code for more detail.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDataIntoListView();
}
}
protected void LoadDataIntoListView()
{
Users objQuery = new Users();
string adminID = "Here is my query to get the data from MS-SQL";
objQuery.ExecuteSql(str);
if (objQuery.RowCount > 0)
{
Title = "Row affected";
lstAppointments.Items.Clear();
lstAppointments.DataSource = objQuery.DefaultView;
lstAppointments.DataBind();
}
else
{
Title = "None Row affected";
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string caseID = (string)Session["caseID"];
//string updateQuery = "update Cases set sCaseStatus='cancel' where iCaseID= '" + caseID + "'";
Cases objCases = new Cases();
objCases.LoadByPrimaryKey(Convert.ToInt32(caseID));
if (String.Equals(objCases.SCaseStatus, "cancel"))
{
Page.Title = "No Update";
ModalPopupExtender1.Hide();
}
else
{
objCases.SCaseStatus = "cancel";
objCases.Save();
Page.Title = "No Update";
ModalPopupExtender1.Hide();
lstAppointments.Items.Clear();
LoadDataIntoListView();
}
}
Thanks in advance.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDataIntoListView();
}
}
You are binding data in not Postback. That means it does not binds data when you postback to same page. If you want to bind that on every page load, call the function LoadDataIntoListView() in Page_Load

How can I redirect based on a selection of a DropDownList

I have a TextBox1 and a Search button in my application with this following code:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~\\searchpage.aspx?PatientNRIC=" + TextBox1.Text);
}
Which means, if the user type ONLY IC NO:S1234567D, then when click it will show the patient detailview.
So I now I'm doing almost the same thing but now I have a TextBox2 and a DropDownList1. Inside DropDownList1, I have "Name", "IC No", "Test_Date".
So for an example, I type "S1234567D" in the textbox1, and I choose "IC No" in DropDownList1 it should redirect me to a page of the S1234567D's patient detailview.
How could I do my code? Thanks!
Something like the following might work for you:
protected void Button1_Click(object sender, EventArgs e)
{
if(dropdownlist1.SelectedValue == "IC No")
{
// assuming this is the redirect to your patients details view
// but you MUST use only forward slashes to make it work (!)
Response.Redirect("~/searchpage.aspx?PatientNRIC=" + TextBox1.Text);
}
}
name.Text = ddl1.DataTextField;
ICNo.Text = ddl1.DataValueField;
textBox1.Text = name.text+ICno.Text;
protected void Button1_Click(object sender, EventArgs e)
{
if(textbox1 != null)
{
Response.Redirect("~/searchpage.aspx?PatientNRIC=" + TextBox1.Text);
}
}
}

Categories

Resources