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;
}
}
Related
I have a listview and created a textbox to search itmes in that list!
everything is ok about searching!
but problem is I want my search box to be like this:
at first Searchbox.Text="Search ..." and if user started typing in that searchbox change to that keyword! else(if) searchbox got empty Searchbox.Text change to "Search ..." again!
maybe it's a little complicated! but i tried 1-2 hours on it! and couldn't make it!
I have used Timers,checkboxchange event,booleans,...! but couldn't make it! :(
Please Help!
*my Searchbox name here is textbox1.
Some Codes I tested:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string str = textBox1.Text;
/*
if (!keywordentered_bool)
{
textBox1.Text = "";
}
*/
if (str != "")
{
//Doing Search operations!
search_bool = true;
}
else
{//Doing Search operations!
search_bool = true;
// keywordentered_checkbox.Checked = true;
Searchtextbox_Timer.Interval = 100;
Searchtextbox_Timer.Enabled = true;
Searchtextbox_Timer.Tick += Searchtextbox_Timer_Tick;
//textBox2.Visible = false;
}
}
else
{
if (search_bool)
{
listView1.Items.Clear();
label1.Visible = false;
listView1.Items.AddRange(Originalplaylist_list.ToArray());
if (!search_bool)
{
listView1.Items[MusicLogindex_list[MusicLogindex_list.Count - 1]].ForeColor = Color.Cyan;
}
else if (search_bool)
{//Doing Search operations
search_bool = false;
Searchtextbox_Timer.Interval = 100;
Searchtextbox_Timer.Enabled = true;
Searchtextbox_Timer.Tick += Searchtextbox_Timer_Tick;
//textBox2.Visible = true;
// keywordentered_checkbox.Checked = false;
}
}
void Searchtextbox_Timer_Tick(object sender, EventArgs e)
{
if (!search_bool)
{
textBox2.Visible = true;
textBox2.Location = textBox1.Location;
//textBox1.Text = "Search ...";
//textBox1.ForeColor = Color.Gray;
//textBox1.Font = new Font(textBox1.Font, FontStyle.Italic);
}
else
{
textBox2.Visible = false;
// textBox1.Text = "";
// textBox1.ForeColor = Color.Black;
// textBox1.Font = new Font(textBox1.Font, FontStyle.Regular);
}
Searchtextbox_Timer.Enabled = false;
//throw new NotImplementedException();
}
This is just psuedocode but the concept is there and you need to implement the text change event for searching , you can do additional changes in event handler.
Textbox myTxtbx = new Textbox();
myTxtbx.Text = "Enter text here...";
myTxtbx.OnFocus += OnFocus.EventHandle(RemoveText);
myTxtbx.LoseFocus += LoseFocus.EventHandle(AddText);
public RemoveText(object sender, EventArgs e)
{
myTxtbx.Text = "";
}
public AddText(object sender, EventArgs e)
{
if(string.IsNullorEmpty(myTxtbx.Text))
myTxtbx.Text = "Enter text here...";
}
This is an example shows for a dynamic textbox control, you can add these events to your control and use the same code for make it work.
Or you can use this plugin
Visual Studio gallery plugin
Another plugin you can use for this purpose
Textbox with placeholder
Hope this helps.
thanks to #Frebin Francis my problem solved!
I downloaded the source code from this link TextBox With Placeholder
and added it to my project! then add it to my form and yeah! :)
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.
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.