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.
Related
I'm using this right now to clear a tic tac toe table on Microsoft Windows Forms
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
but it's too long so I wanted to use a for loop like this
for (int i = 1; i < 10; i++)
{
button[i].Enable = true;
button[i].Text = "";
}
but it doesn't work so could anyone suggest a solution?
you can iterate over Controls array of parent of buttons, for example you can move all buttons to a Panel and use foreach to iterate over all controls inside it:
foreach(var control in Panel1.Controls) {
if (control is Button button)
{
button.Text = "";
button.Enabled = true;
}
}
I see what you are thinking, but you cannot dynamically change the name of the variable you are trying to edit in a for loop like that.
In a for loop, it will iterate through a starting value until it reaches a stopping condition. for loops are great for when you have a data structure like a List or an Array where you can enumerate through to get or set values.
The code you have for the for loop is starting to enumerate through either an Array or List that has a name of button. To fix this, I recommend to add the buttons to an Array and then you can use a foreach loop to set the values:
foreach (Button button in buttons){
button.Enabled = true;
button.Text = "";
}
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
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;
I have my code written like so:
private void radioSelectButton_Click(object sender, EventArgs e)
{
if (pteriRadio.Checked) // Selecting avatar to be displayed from here on out.
//This avatar will also be displayed on the game board.
{
pteriBox1.Visible = true;
xweetokBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Pteri Inventory");
player1avatar = "pteri";
}
else if (xweetokRadio.Checked)
{
xweetokBox1.Visible = true;
pteriBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Xweetok Inventory");
player1avatar = "xweetok";
}
else if (ixiRadio.Checked)
{
ixiBox1.Visible = true;
pteriBox1.Visible = false;
xweetokBox1.Visible = false;
label1.Text = ("Ixi Inventory");
player1avatar = "ixi";
}
characterSelectBox.Visible = false;
radioSelectButton.Visible = false;
characterSelectBox2.Visible = true;
radioSelectButton2.Visible = true;
}
It seems as though the visibility changes should display when I have it like this, with the changes within the button click but outside of the if statements (It doesn't matter what the user selects, once selected the option to select needs to disappear for that user.)Yet the visibility changes don't execute. What am I missing here?
If I nest the if-statements as suggested by a previous person, here's what I have:
private void radioSelectButton_Click(object sender, EventArgs e)
{
if (pteriRadio.Checked) // Selecting avatar to be displayed from here on out.
//This avatar will also be displayed on the game board.
{
pteriBox1.Visible = true;
xweetokBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Pteri Inventory");
player1avatar = "pteri";
if (xweetokRadio.Checked)
{
xweetokBox1.Visible = true;
pteriBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Xweetok Inventory");
player1avatar = "xweetok";
if (ixiRadio.Checked)
{
ixiBox1.Visible = true;
pteriBox1.Visible = false;
xweetokBox1.Visible = false;
label1.Text = ("Ixi Inventory");
player1avatar = "ixi";
}
characterSelectBox.Visible = false;
radioSelectButton.Visible = false;
characterSelectBox2.Visible = true;
radioSelectButton2.Visible = true;
}
}
}
Now, not only do the visible items not swap over, two of the character choices don't display.
It looks like the radio buttons here can be checked/unchecked together. I think you should use separate if statements for each condition instead of else if.
You need to Invalidate your form to make it redraw after you change the properties. Assuming the radioSelectButton_Click method is in the same form, calling this.Invalidate() at the end of the method should force the redraw.
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;
}
}