ASP.NET Setting Visible = false doesn't stay - asp.net

I have a lot of buttons that I want to appear when I tell them to and so far I have been using Visible = true or false depending on when I want them to appear or not. I want the button to disappear after the click event has occurred on it. I have two buttons in the code and when I click on btnOption131 it disappears but than when I click on btnOption132 it disappears and btnOption131 re-appears. Is there anyway I can stop this from happening?
<asp:Button ID="btnOption131" runat="server" Text="Think" OnClick="btnOption131_Click" Visible="False" CssClass="button"/>
<asp:Button ID="btnOption132" runat="server" Text="Go Crazy" OnClick="btnOption132_Click" Visible="False"/>
Code Behind:
protected void btnOption131_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[8].ToString();
btnOption131.EnableViewState = false;
btnOption131.Visible = false;
}
protected void btnOption132_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[9].ToString();
btnOption132.EnableViewState = false;
btnOption132.Visible = false;
}
I also tried:
protected void btnOption131_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[8].ToString();
btnOption131.Visible = false;
}
protected void btnOption132_Click(object sender, EventArgs e)
{
txt1.Text = statementArray1[9].ToString();
btnOption132.Visible = false;
}

You have to set the button Visibility to 'true' somewhere. My guess is that it is not wrapped inside an IsPostBack check:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
btnOption131.Visible = true;
}
}
If you do not do this the visibility will always be set to true when the next PostBack (= button click) occurs.

Related

DropDownButton conditional statement with attributes?

I'm having trouble to find out how to check if my DropDownList has the attribute Disabled
Here is my code (of how I declare my DropDownList):
<div class="col-7">
<asp:DropDownList ID="cmbProperty" runat="server" class="browser-default z-depth-5">
</asp:DropDownList>
</div>
On page load:
protected void Page_Load(object sender, EventArgs e)
{
cmbProperty.Attributes.Add("disabled", "disabled");
}
On button click:
protected void btnCheckMyProperty_Click(object sender, EventArgs e)
{
if(cmbProperty.Enabled == true)
{
// I always get a true statement
}
}
Someone has a clue about it?
Thank you
Since you commented saying setting cmbProperty.Enabled = false messes with your css, you should check the disabled attribute in your button click event instead of the Enabled property. This is simply:
protected void btnCheckMyProperty_Click(object sender, EventArgs e)
{
if(cmbProperty.Attributes["disabled"] == "disabled")
{
// Your code here...
}
}
Note: This will NOT error out if the disabled attribute is not set. It will return false in that case...

Repeater with dynamic UC

I have a formular with dynamic data and UC.
my ascx file looks like:
<asp:Repeater ID="rMyData" runat="server" OnItemDataBound="OnRepeaterItemDataBound">
<ItemTemplate>
<asp:Panel ID="panelAdditional" runat="server"/>
</ItemTemplate>
</asp:Repeater>
and the cs file:
public partial class MyFormular
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PrepareList();
DataBind();
}
}
private void PrepareList()
{
rMyData.DataSource = GetData();
rMyData.DataBind();
}
protected void OnRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem repeaterItem = e.Item;
Panel panel = (Panel)repeaterItem.FindControl("panelAdditional");
if (panel != null)
{
var datarow= (Data)repeaterItem.DataItem;
if (datarow!= null)
{
panel.Controls.Clear();
MyUC ctrl;
ctrl = (MyUC)Page.LoadControl("~/MyUC.ascx");
ctrl.Enabled = true;
ctrl.DataRow = datarow;
panel.Controls.Add(ctrl);
}
}
}
protected void SaveNewsletterSubscription(object sender, EventArgs e)
{
...
}
}
My problem is, it doesn't show my UC.
If I put my PrepareList() and DataBind() outside of the PostBack, it works perfectly fine and shows my UC. But after I press the save Button, it reloads the page and rebind my repeater. So I lost everything in the UC.
Why does it only show my UC if I rebind it in every pageload?
I hope the I posted enough information.

radio button check and uncheck other radio buttons the web form is connected with a database created using ms access

i have two radio buttons in an web application named male and female and used the following code to check a button and uncheck other one but it isnt working when i m ruunning it both are selected .what to do?
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
RadioButton2.Checked = false;
}
else
{
RadioButton2.Checked = true;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked == true)
{
RadioButton1.Checked = false;
}
else
{
RadioButton2.Checked = true;
}
You should be using a RadioButtonList for this. It will give you radio buttons that belong to the same group and automatically have the behavior that the other buttons in the group are unchecked when a different button in the group is checked.
I think what is happening is when you are doing RadioButton2.Checked =false in
protected void RadioButton1_CheckedChanged(object sender, EventArgs e) you are triggering protected void RadioButton2_CheckedChanged(object sender, EventArgs e) and therfor checking both. Your radio buttons need to be grouped so that they are 1 item and you shouldn't need any code behind to make them work.

click on button in asp.net

I am creating a button dynamically and place it in a placeholder as below
<asp:Button ID="generateTableSchema" runat="server" Text="Generate Table" OnClick="generate_Click" />
protected void generate_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "Generate Table";
button.ID = "generateTable";
button.OnClick = hello();
PlaceHolder1.Controls.Add(button);
}
but onclick event is not firing.
this is the error i am getting
System.Web.UI.WebControls.Button.OnClick(System.EventArgs)' is inaccessible due to its protection level
hello is as below...
public void hello()
{
Label1.Text = "heellllllllllo";
}
What's wrong here????
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
else
{
button.Click += ButtonClick;
}
}
#Daren u mean like this...
Because you are adding the button programmatic ally you have to add the event handler.
So this would work..
EDIT
Wrapped the button INSIDE Page_Load
protected void Page_Load(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "Generate Table";
button.ID = "generateTable";
button.Click += hello; /// THIS is the handler
PlaceHolder1.Controls.Add(button);
}
ButtonClick would be the name of your method.
protected void hello(Object sender, EventArgs e)
{
// ...
}
Also, as you're generating this at runtime you need to makesure this gets called on postbacks too.
The OnClick is a protected method. You should use the event Click.
button.Click += new EventHandler(Click);
public void hello(object sender, EventArgs e)
{
Label1.Text = "heellllllllllo";
}
By the way, make sure you create and add the control in every postback, otherwise the event won't work.
Change button.OnClick = hello(); to:
button.Click += new EventHandler(hello);
And change the definition for hello() to:
protected void hello(object sender, EventArgs e)
{
Label1.Text = "heeellllllo";
}
The event is called Click. You need to add the event handler with the correct signature:
button.Click += new EventHandler(hello);
and the signature is:
protected void hello(Object sender, EventArgs e)
{
// ...
}
How to: Add an Event Handler Using Code
Note that you need to recreate dynamical controls on every postback.
You assigning the result of executing hello().
Try assigning:
button.OnClick = hello;
-- edit--
apparantly, that does not clarify your error.
Add the handler to the event handler in stead:
button.Click += hello;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
else if(Page.IsPostBack && Label11.Text=="yes")
{
Button button = new Button();
button.Text = "Generate Table";
button.ID = "generateTable";
button.Click += ButtonClick;
PlaceHolder1.Controls.Add(button);
}
}
setting
Label11.Text = "yes";
in generate_click.
protected void ButtonClick(object sender, EventArgs e)
{
Label1.Text = "heeellllllo";
}

I want RowCommand executed before RowCreate reloading OR something like this

I have
<asp:GridView>
<asp:TemplateField HeaderText="PsyHealth">
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="PsyHealth" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="-">
<ItemTemplate>
<asp:LinkButton ID="Gen" CommandName="Gen" runat="server" Text="gen" />
</ItemTemplate>
</asp:TemplateField>
and
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dataItem = e.Row.DataItem as ViewModels.UserTestorViewModel;
var psyHealth = e.Row.FindControl("PsyHealth") as PlaceHolder;
if (psyHealth != null)
{
psyHealth.Controls.Add(dataItem.PsyHeath);
}
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//bla bla bla
}
but when I clicked the Gen LinkButton on the page. The GridView1_RowCreated was triggered first and threw the error Object reference not set to an instance of an object because the e.Row.DataItem was null.
Edit: The Code Behind
protected void Page_Load(object sender, EventArgs e)
{
List<ViewModels.UserTestorViewModel> utViewModelList = new List<ViewModels.UserTestorViewModel> { };
utViewModelList = utRepo.GetUserTestorViewModelListByHrId();
this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();
if (!IsPostBack)
{
}
}
protected void Page_Init(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
}
Can you store the utViewModelList in Session first time you get it? If so then you can get the UserTestorViewModel instance from saved by the selected row's DataKey value.
When you click any button in the gridview , your page is postbacked and the page load event is called before it goes into the RowCommand event. In the page load event you are binding your gridview again and that's why your RowCreated Event is called.
You have to bind your gridview under if (!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<ViewModels.UserTestorViewModel> utViewModelList = new List<ViewModels.UserTestorViewModel> { };
utViewModelList = utRepo.GetUserTestorViewModelListByHrId();
this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();
}
}
Edit: Now I got your issue after you posted code..
The problem is here in Page_Init, can you remove the event handler from here and try the following:
protected void Page_Init(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
}
add here
<asp:GridView onrowcreated="GridView1_RowCreated">

Resources