How to avoid textbox space when it is hidden from the page? - c#

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;

Related

Prevent duplicate record insertion on page refresh

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

How do i get an information label to appear as i type and disappear when i delete the text

I have a couple of text boxes that i have initially hidden and what I want to do is as the users type I would like the next text box along with the label that goes with it to appear to notify of the next question.
At the same time if they change their mind and delete their response to the first question the next text box and label will disappear once the text has been deleted.
Here is my current code:
private void CreditScoreBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar))
e.Handled = false;
else
e.Handled = true;
if(CreditScoreBox.Text == "")
{
MakeBox.Visible = false;
MakeLabel.Visible = false;
ModelBox.Visible = false;
ModelLabel.Visible = false;
CreditLevelLabel.Visible = false;
}
else
{
MakeBox.Visible = true;
MakeBox.Enabled = true;
MakeLabel.Visible = true;
CreditLevelLabel.Visible = true;
}
I have tried using the TextChanged event with the same result.
I created a Form, with 2 textbox and 1 label. If text exist in textbox1 then label1 and textbox2 become available:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
textBox2.Enabled = false;
textBox2.Visible = false;
label1.Visible = false;
}
else
{
textBox2.Enabled = true;
textBox2.Visible = true;
label1.Visible = true;
}
}

Where do I put these visible changes for them to display

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.

CheckBox Control in Visual studio asp.net 2005 C#

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;
}
}

Object not set to instance....blah

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.

Categories

Resources