Gridview Rowcommand event firing but show hide controls including Gridview not happening - c#

Here is what I am trying to do in two simple steps:
1) New Row (trNewPost) which has table inside and controls in it to add new post or to update existing post.
Default Visible=false;
2) Add Button to make above row visible = true;
3) trMyPosts has Gridview in it and displays all the posts.
Default visible = true.
When user click on editing any row of the gridview (RowCommand event) I just want to hide this grid (trMyPosts) and show trNewPost.
That's all. events firing, but nothing happening.

I think you've got viewstate problem.
One of the things you can do is this :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// do things here
}
}
Because whenever anything happens, the page posts back. By encapsulating your Page_Load with ! Page.IsPostBack, you prevent those things from happening over and over again.
Now, if your variable is a global variable, you will have this same problem. Consider instead using a Session variable.
Also, I just wanted to show you this piece of code just in case :
protected void HideShowClicked(object sender, EventArgs e)
{
// toggle the visibility of the control
// (that is, if visible then hide, if hidden then show)
myControl.Visible = ! myControl.Visible;
}

Related

How to prevent AutoPostBack from resetting my page?

I am building a page that includes some components that are static on the page (dropdowns, buttons, a table to hold them in), but one table cell is filled with variably generated CheckBoxes. When a button is pressed, the code for the page calculates what checkboxes to place and creates a new CheckBox object for each one needed and then adds it to an existing Div on the page.
I am trying to trigger some code to run when any of these are checked or unchecked, but checkBoxName.CheckedChanged += cbCheckedChanged wasn't working. I researched and found two suggestions: enabling viewstate and enabling autopostback.
checkBoxName.EnableViewState = true; seems to make no difference, nor did checkBoxName.ViewStateMode = ViewStateMode.Enabled; or any variations on that i tried. checkBoxName.AutoPostBack = true; did SOMETHING, but it's not allowing it to run the code I want. I think that's because it doesn't reach that point because of the next problem:
With AutoPostBack = true, whenever I check or uncheck a box, cbCheckedChangedis not being executed, and the entire page is reloading, resetting back to it's initial state, therefore removing the checkboxes completely from the table.
How can I fix these problems, or at least where might I start looking?
Edit:
This is where the checkboxes are created:
CheckBox cb = new CheckBox();
cb.Text = CBName;
cb.EnableViewState = true;
cb.ViewStateMode = ViewStateMode.Enabled;
cb.AutoPostBack = true;
cb.CheckedChanged += CBCheckedChanged;
and this is where CBCheckedChanged is:
private void CBCheckedChanged(object sender, EventArgs e)
{
\\Stuff
}
When I use breakpoints to step through it, it never reaches CBCheckedChanged. I have tried every possible combination of commenting out and leaving in the AutoPostBack, ViewStateMode, and EnableViewState lines.
Page_Load is currently empty, nothing runs until the user hits a button.
ViewState is by default enabled for server side controls. You don't have to tinker around with ViewState to solve your problem. ViewState is basically used to restore the state of a control after a postback happens. For example readding all entries to a ListBox control. This is why most control population code is within such a construct in the Page_Load method.
private void Page_Load()
{
if (!Page.IsPostBack)
{
// populate controls here
}
}
ViewState is a very misunderstood concept. There is a great article here that goes into it in detail. But as I said, for your problem ViewState is not a concern.
To solve your problem:
The problem in your case is that the triggered button is creating the CheckBox controls, but when the page is reloaded, because of the CheckedChanged event of these controls, the next page life cycle has no clue of the CheckBox controls that were placed on the page in the previous page life cycle. Dynamic controls need to be generated for every page life cycle!
So what I would do is create a method that:
creates the CheckBox controls and
sets AutoPostback = true for them and
sets the event handler for CheckedChanged
Let's call this method AddDynamicCheckBoxes(). Now you need to call this method in the Page_Load event of your Page when the button was already pressed and also in the event handler of the button's click event. You could do this like follows:
private void Page_Load()
{
if (ViewState["button_was_clicked"] != null)
{
AddDynamicCheckBoxes();
}
}
private void Button_OnClick()
{
AddDynamicCheckBoxes();
ViewState["button_was_clicked"] = true;
}

Gridview Textbox doesnot maintain Enable/Disable state but maintains Text during postbacks

I have a textbox inside a gridview.
It was initially set to Disable state.
I am enabling and disabling it in javascript.
But the problem arises during the postbacks.The grid view was able to persist the textbox data but not the Enable/Disable values.During every postback it comes back disabled even when it is enabled before the postback.
I have explored around the use of hidden field to maintain the state in these scenarious but I wish there is something less tiresome...
Can some one enlighten me on this issue...
Seeing the code could really help but generally take off the Enable="False" from the HTML then in the Page_Load event add something like:
Protected void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
tetxBox.enabled = false;
}
}

ASP.NET listbox selectedValue in PageLoad

I have a problem with listbox. I would like to fill it with data and select Value (listbox.SelctedValue), using postback, after textbox is filled and Page.Validate() is fired. I try to use it in Page_Load. Everything works fine until I dont mark another user. It goes back to the the first one. I know its because I mark the first one again and again it in Page_Load, but how can I mark user after postback in other place? I cant use any buttons.
To be more clear, I have one text box, which causes postback after user put text there. After that I would like to check if Page.Isvalid and is yes, add that user to listbox (which also causes postbacks) and mark him. Without any buttons. How can I do it only once, using autopostback, not every PageLoad ?
Try this
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Page.IsValid)
{
//Mylistbox.SelctedValue = set Your Selected Value
}
}
}

how to programmatically add more than one item in a webForm asp.net

I try to programmatically add item (like label, table, ...) in my webForm. I tried to create an event click on a button, to add labels each clicks. But every time, it stops to one label, and then nothing happens for the next clicks.
Here's my code for those who want to see what I tried :
protected void btn_Valid_Click(object sender, EventArgs e)
{
this.Page.Form.Controls.Add(new Label() { Text = "test" });
}
Can we do this in asp.net or should I use another language (like javascript) ?
It only creates one Label because that one Label is lost during PostBack.
In your click-event, you have to increase a counter for labels (and/or other controls), and recreate those in Page_Init. EACH and EVERY time the Page posts back. Memorize that counter in the ViewState.
And as usual, I recommend this article about the ASP.NET Page Life Cycle
For those who want a real anwser to this follow this step, it works for me :
Make creation (and not instanciation) in the Page_Init or Page_Load, because for each events those methods are called,
For the example above, don't do kind of this.Page.Forms.Controls.Add(lbl_test), try to find another way like storing the data of those label (in a String Array for example). Here's the code example :
protected void btn_test_Click(object sender, EventArgs e) {
tabLabel[count_tabLabel] = "hello world";
// Do the if(IsCallBack) {} for avoid having your first label skipped
count_tabLabel++;
}
In the Page_Load() function (basically written in the .cs) or by creating the function bellow, do a loop to add those labels to your webForm like this :
protected void Page_Init(object sender, EventArgs e) {
for (int i = 0; i < cpt_l; i++)
{
Page.Form.Controls.Add(tabLabel[count_tabLabel]);
}
}
In fact the page is automatically reloaded, that is the asp page cycle life. So you need at each modifications on your webForm to reload all your items like labels, or more complexes things.
Hope it helps !
P.S.:

how to see DB changes without refreshing page in ASP.NET C#

I have this button event:
protected void AddDetails_Click(object sender, EventArgs e)
{
DataSetTableAdapters.SelectFriendsTableAdapter sft = new DataSetTableAdapters.SelectFriendsTableAdapter();
try
{
sft.AddFriend(current, newFriend, false);
}
catch
{
error.Text = "Something happened. Bummer!";
}
}
in the try section, I'm adding entries in the Database. In the page there are Labels / Textboxes with the corresponding values.
Everything works fine. However, I need to refresh the page in order to see the changes after I click on this submit button.
I have added if(!IsPostBack) at the beginning of the PageLoad code, but I still need to visit another page / come back to see the changes.
Any ideas?
Thanks.
Thank you for your replies. I'm using a ListView:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSetTableAdapters.SelectFriendsTableAdapter sdt = new DataSetTableAdapters.SelectFriendsTableAdapter();
DataTable tab = sdt.SelectFriends();
ListView1.DataSource = tab;
ListView1.DataBind();
}
}
, so the ListView content should get updated.
If you don't want to refresh the page and you're using asp.net webforms you can use an UpdatePanel and place all your controls inside of it, etc... That will keep the changes made in the form / display when you submit the info. I'm assuming the form is on submitting the changed/new data as you didn't specifically state how the data was being updated/changed.
If you don't want to use an update panel, then when the page is posting back you will have to set the values for the UI controls that you want to update with the values from the 'newFriend' object (I'm assuming that has the changed values).
Found it!
The solution for this issue is to add this code at the end of your submit button click event:
Server.Transfer("currentpage.aspx");

Categories

Resources