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();
Related
I have a bunch of GridView on one page, and each have their own coordinating SqlDataSource.
They all load their coordinating data when initializing the page (on the page load). I want to Load the data for specific SqlDataSource(s) on the page when the user clicks a button, so that it is not trying to load ALL the data for all the different grids all at the same time when the page initially loads.
Note: I need to also have Paging working, and anything I have tried so far has broken the paging functionality.
Looking to tell the SqlDataSource what data to bind to it after the user clicks a button, and then have the specific GridView Bind that data to it and have the Paging functionality work with the data it was told to load.
Environment: C#, WebForms, ASP.NET, .NET 3.5, Controls SqlDataSource / GridView
From what I can tell it correctly binds the data, but the 'gridview' control keeps disappearing when I click one of the pages at the bottom for the paging.
Note: If I click the button again, it will show the correct page and the correct data for the page.
protected void btn_LoadData2_Click(object sender, EventArgs e) { Gridview2.DataSourceID = "DataSource2"; Gridview2.SelectCommand = "SELECT * FROM dbo.DataNeeded"; Gridview2.DataBind(); Gridview2.DataBind(); }
Add a Selecting event to each of your data sources. Then in the code behind check if IsPostBack and if IsPostBack is false then cancel the operation.
protected void MyDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
if (!IsPostBack)
e.Cancel = true;
}
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;
}
}
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");
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.
}
}
How to programmatically browse through the pages of a data-bound DetailsView?
I am using this code:
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
}
The page is changing. But the problem is, the next record item is not showing up. The old item is still visible on the new page.
You have to re-bind your details view.