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");
Related
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
}
}
}
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.:
I have one asp.net page with two gridview using updatepanel.when i click the submit button data inserting in db and update the gridview. i'm handling duplicate insertion of same data when refresh the page. but the problem is when i refreshing the page previous data in the gridview showing not present data. again if i'm clicking the menu its reloading and showing actual data.i'm handling button click but i don't know how to handle this gridview problem when i refresh the page. if anybody pass through same problem please show some light on this or please give some suggession.
I would try:
gridviewObj.datasource = null
gridviewobj.datasource.databind();
... hope this helps
you could do this
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
//here you bind again the datagrid
}
}
Use if(IsPosback) in Page_Load event
then
UpdatePanel.Update();
I have an asp.net CheckBox, now I want to reload page after check or uncheck and use CheckBox.Checked information to choose sql query for gridview. I have put code like this in Page_Load method:
if (CheckBox1.Checked)
{
query = "select ...";
}
But nothing happen. I set AutoPostBack also. Tried to use event. Don;t know how this system works:/
EDIT:
Checkbox works ok, but the problem is in something different. After I click checkbox, in Page_Load method I will use my query to setup SqlDataSource. Looks like page is reloaded, but gridview is not refreshed. When i click on gridview's column mame (to sort this column), gridview is refreshed by new sql query. So i need to think how to refresh grid view after click check box.
It seems that you are not using IsPostBack property on page load event. If you not use this your CheckBox will be reset on every page load
Try this way
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Here do your stuff.
}
}
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;
}