I search A lot.. but failed to implement.
here is my code, how i prevent duplicate insertion on page refresh,, (how i use viewstate, or any other method.... resposnse.redirect is not a solution in this scenario).
protected void btn_save_click(object sender, EventArgs e)
{
System.Data.DataTable dt_plotid = new System.Data.DataTable();
dt_plotid = FghBAL.Admitting.GetMax_ByPlotID(txt_plot.Text);
string plotid = dt_plotid.Rows[0]["ID"].ToString();
if (FghBAL.Alotee.InserAllotee(txt_alotee_name.Text.ToUpper(), ddl_alotee_sw.SelectedValue.ToUpper(), txt_alotee_fname.Text.ToUpper(), alotee_cnic, txt_alotee_phone.Text, txt_alotee_cellno.Text, txt_alotee_address.Text, txt_alotee_email.Text, plotid))
{
lbl_error_3.Visible = true;
lbl_error_3.Text = "Add Successfully";
txt_alotee_name.Enabled = false;
ddl_alotee_sw.Enabled = false;
txt_alotee_fname.Enabled = false;
txt_alotee_cnic_1.Enabled = false;
txt_alotee_cnic_2.Enabled = false;
txt_alotee_cnic_3.Enabled = false;
txt_alotee_phone.Enabled = false;
txt_alotee_cellno.Enabled = false;
txt_alotee_address.Enabled = false;
txt_alotee_email.Enabled = false;
System.Data.DataTable dt_alotee_data = new System.Data.DataTable();
dt_alotee_data = FghBAL.Alotee.GetDatabyPlot_Id(plotid);
grid_alotee.DataSource = dt_alotee_data;
grid_alotee.DataBind();
grid_alotee.Visible = true;
btn_save_alotee.Visible = true;
btn_link_another_alotee.Visible = true;
lbl_grd_alotee.Visible = true;
tabcontent3.Style.Add("height", "80%");
//ViewState["dt_alotee_view"] = dt_alotee_data;
btn_save_alotee.Enabled = false;
// Response.Redirect(Request.Url.AbsoluteUri);
}
else
{
lbl_error_3.Text = "Unable to add Allottee Information";
}
}
Your question seems to indicate that a button event handler is triggered on a page refresh - which is odd. A page refresh results in a GET action. Changes to server side data should only be done in POST requests though. And button event handlers should only be called when a button is clicked. I can't tell from this code in what way your architecture is violating these stipulations.
You can use view state or session to store the flag when the page first loads and then change the flag value to true when you have inserted in the database get the full code from here :
http://dotnetready4u.blogspot.com/2015/01/avoid-repaeted-insertion-on-page-refresh.html
Related
I Have a problem with two checkBoxes!
private CheckBox rdYes;
private CheckBox rdNo;
rdYes = new CheckBox();
rdYes.Text = "Yes";
rdYes.Checked = false;
rdNo = new CheckBox();
rdNo.Text = "No";
rdNo.Checked = false;
Error:
Invalid postback or callback argument. Event validation is enabled
using in configuration or <%#
Page EnableEventValidation="true" %> in a page. For security
purposes, this feature verifies that arguments to postback or callback
events originate from the server control that originally rendered
them. If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation
I instantiating the checkboxes in :
protected override void CreateChildControls()
{
this.Controls.Add(rdYes);
this.Controls.Add(rdNo);
}
And I need to check one CheckBox.
Try this
rdYes = new CheckBox();
rdYes.ID = "chkYes";
rdYes.Text = "Yes";
rdYes.Checked = false;
rdYes.AutoPostBack=false;
rdYes.Attributes.Add("onclick","ToggleCheckboxes();return false")
rdNo = new CheckBox();
rdNo.ID = "chkNo";
rdNo.Text = "No";
rdNo.Checked = false;
rdNo.AutoPostBack=false;
rdNo.Attributes.Add("onclick","ToggleCheckboxes();return false")
Add Javascript function to your page
function CheckCheckboxes() {
if(document.getElementid('<%=chkYes.ClientID%>').checked==true)
{
document.getElementid('<%=chkNo.ClientID%>').checked=false;
}
else if(document.getElementid('<%=chkNo.ClientID%>').checked==true)
{
document.getElementid('<%=chkYes.ClientID%>').checked=false;
}
}
And I need to check only one CheckBox...
If you need to check only one checkbox then you can better try to use Radio Button.
However if you want to make sure that only one checkbox is selected then try like this:
CheckBox prevChecked;
private void clickCheckBox(object sender, EventArgs e)
{
CheckBox myCheckBox = sender as CheckBox;
if(myCheckBox != prevChecked && prevChecked!=null) prevChecked.Checked = false;
{
prevChecked = myCheckBox.Checked ? myCheckBox : null;
}
}
and if you want that the page should not be refreshed then you can try to set AutoPostBack="false" for your Checkbox property.
rdYes = new CheckBox();
rdYes.Text = "Yes";
rdYes.Checked = false;
rdYes.AutoPostBack=false;
rdNo = new CheckBox();
rdNo.Text = "No";
rdNo.Checked = false;
rdNo.AutoPostBack=false;
I'm writing a school program and I'm trying to move 3 input fields to a new page.
I can get the response.redirect to work on one field but not more.
When I click the button it takes me to the next page and only one field is brought over. Not the 3 that I'm trying to get there.
Can anyone steer my right? Thanks in advance...
Page one:
protected void btnEnterSelection_Click(object sender, EventArgs e)
{
lblBookEntered.Visible = true;
lblBookType.Visible = true;
lblPurchaseType.Visible = true;
lblBookEnteredText.Visible = true;
lblBookTypeText.Visible = true;
lblPurchaseTypeText.Visible = true;
lblBookEntered.Text = "The book you entered is: ";
lblBookEnteredText.Text = txtBoxBookTitle.Text;
lblBookType.Text = "The book type is: ";
lblBookTypeText.Text = drpDownType.Text;
lblPurchaseType.Text = "The purchase type is: ";
lblPurchaseTypeText.Text = drpDownPurchase.Text;
}
protected void btnPurchase_Click(object sender, EventArgs e)
{
Response.Redirect("turtleDoxPurchase.aspx?bookName=" + txtBoxBookTitle.Text);
Response.Redirect("turtleDoxPurchase.aspx?bookType=" + drpDownType.Text);
Response.Redirect("turtleDoxPurchase.aspx?purchaseType=" + drpDownPurchase.Text);
}
Page two:
protected void Page_Load(object sender, EventArgs e)
{
lblBookEntered.Visible = true;
lblBookType.Visible = true;
lblPurchaseType.Visible = true;
lblBookEnteredText.Visible = true;
lblBookTypeText.Visible = true;
lblPurchaseTypeText.Visible = true;
lblBookEntered.Text = "The book you entered is: ";
lblBookEnteredText.Text = Request.QueryString["bookName"];
lblBookType.Text = "The book type is: ";
lblBookTypeText.Text = Request.QueryString["bookType"];
lblPurchaseType.Text = "The purchase type is: ";
lblPurchaseTypeText.Text = Request.QueryString["purchaseType"];
lblCreditCard.Visible = true;
txtBoxCreditCard.Visible = true;
lblCreditCardChoice.Visible = true;
rdoListCreditCard.Visible = true;
btnSubmitPayment.Visible = true;
}
If I understand the problem correctly, you are trying to send three values from Page One to Page Two. In that case, you could build a Query string using the values from txtBoxBookTitle, drpDownType and DrpDownPurchase. The string should be in the follwing format:
string queryString = "?bookName={txtBoxBookTitle}&bookType={drpDownType.Value}&purchaseType={DrpDownPurchase.Value}"
Then you could append the above string to your
Response.Redirect("turtleDoxPurchase.aspx" + queryString);
Hope that helps!
I am hiding some labels and text boxes according to radio button selection. It hides the label and dropdown lists but the space is there. How can I hide this space? My radio button click is:
protected void rbllist_SelectedIndexChanged(object sender, EventArgs e)
{
if (rbllist.SelectedValue == "2")
{
lblcode.Visible = false;
ddempcode.Visible = false;
lblname.Visible = false;
ddname.Visible = false;
lbletype.Visible = false;
ddtype.Visible = false;
}
else
{
lblcode.Visible = true;
ddempcode.Visible = true;
lblname.Visible = true;
ddname.Visible = true;
lbletype.Visible = true;
ddtype.Visible = true;
}
}
Your problem lies somewhere else. If you set the Visible property of a control to false, it's not even rendered on the page. This means that it can't even take up space on your page. Check for a table cell or div that might take up the space instead.
From the Control.Visible property page on MSDN:
Gets or sets a value that indicates whether a server control is rendered as UI on the page.
Extra:
Your code can be written much cleaner:
bool isVisible = !(rbllist.SelectedValue == "2");
lblcode.Visible = isVisible;
ddempcode.Visible = isVisible;
lblname.Visible = isVisible;
ddname.Visible = isVisible;
lbletype.Visible = isVisible;
ddtype.Visible = isVisible;
May you please help me on this: I developed a web application that has Checkboxes and i want to show some controls when the user tick the box and hide them when the user untick the box. I only managed to do the showing part when the user tick the box, now i'm failing to hide them when the user untick the box.
Here is the showing part of the my code:
protected void chkboxMentor_CheckedChanged(object sender, EventArgs e)
{
lblMentorName.Visible = true;
txtMentorName.Visible = true;
lblMentorStuff.Visible = true;
txtMentorStaffNo.Visible = true;
lblMentorDate.Visible = true;
btnShowCal.Visible = true;
}
Please help me with the hiding part.
Any help please!!! It will be highly appreciated
You need to set the Visible property to the checkbox's Checked property.
lblMentorName.Visible = chkBoxMentor.Checked;
txtMentorName.Visible = chkBoxMentor.Checked;
lblMentorStuff.Visible = chkBoxMentor.Checked;
txtMentorStaffNo.Visible = chkBoxMentor.Checked;
lblMentorDate.Visible = chkBoxMentor.Checked;
btnShowCal.Visible = chkBoxMentor.Checked;
EDIT: More elegant solution would be to put these controls in a Panel and just set the Visible property of that using the checkbox's Checked property
As SLaks says
just set
<Yourontrolname>.Visible = chkboxMentor.checked
for each control you wish to toggle
protected void chkboxMentor_CheckedChanged(object sender, EventArgs e)
{
if(chkboxMentor.Checked)
{
lblMentorName.Visible = true;
txtMentorName.Visible = true;
lblMentorStuff.Visible = true;
txtMentorStaffNo.Visible = true;
lblMentorDate.Visible = true;
btnShowCal.Visible = true;
}
else
{
lblMentorName.Visible = false;
txtMentorName.Visible = false;
lblMentorStuff.Visible = false;
txtMentorStaffNo.Visible = false;
lblMentorDate.Visible = false;
btnShowCal.Visible = false;
}
}
i have radio buttons that autopostback and set panels to either visible or invisible. The entire page is in an update panel so that i can force it to update and show the invisible changes. The radio buttons are also in update panels.
It works fine except for one thing - my javascript went out the window! It can't find any of my controls after the panel is updated.
Is there some way i can fix this?
Panel PnlPersonInjury = (Panel)FormView1.FindControl("PnlPersonInjury");
Panel pnlPropertyDamage = (Panel)FormView1.FindControl("pnlPropertyDamage");
RadioButton CTypeP = (RadioButton)FormView1.FindControl("RadioButton1");
RadioButton CTypeC = (RadioButton)FormView1.FindControl("RadioButton2");
RadioButton LossLossP = (RadioButton)FormView1.FindControl("RadioButton3");
RadioButton LossLossI = (RadioButton)FormView1.FindControl("RadioButton4");
if (LossLossI.Checked)
{
// pnlPropertyDamage.Enabled = false;
PnlPersonInjury.Enabled = true;
PnlPersonInjury.Visible = true;
pnlPropertyDamage.Visible = false;
InjSummmary.Visible = false;
PropSummary.Visible = false;
}
else
{
pnlPropertyDamage.Enabled = true;
PnlPersonInjury.Enabled = false;
PnlPersonInjury.Visible = false;
pnlPropertyDamage.Visible = true;
InjSummmary.Visible = false;
PropSummary.Visible = false;
}
if (CTypeC.Checked)
{
cPanel.Enabled = true;
pPanel.Enabled = false;
cPanel.Visible = true;
pPanel.Visible = false;
}
else
{
cPanel.Enabled = false;
pPanel.Enabled = true;
cPanel.Visible = false;
pPanel.Visible = true;
}
UpdatePanel20.Update();
UpdatePanel2.Update();
I left some of the instantiation of some controls out - so that is not an issue.
Without seeing the JavaScript, or knowing what part of this code is related to the error, I'd guess that this line is part of your problem:
PnlPersonInjury.Visible = false;
If a server-side control is hidden, it doesn't render anything to the client-side markup.