How to refresh webpage after clicking on “Update” in Grid view - c#

I am working on an Asp.net, C# Application.
I want to Refresh the webpage after I clicking on “Update” LinkButton; in Grid view. I have tried the following code; however it just refreshes the page without saving the updated data in Grid view.
protected void LinkButton1_Click1(object sender, EventArgs e)
{
Response.Redirect(Request.RawUrl.ToString());
}
Name of DataSource SqlDataSourcePlan
SELECT [PlanID], [Deposit], [DepositReturn], [Discount] FROM [PaymentPlan] WHERE ([Ref] = #Ref)
UPDATE [PaymentPlan] SET [Deposit] = #Deposit, [DepositReturn] = #DepositReturn, [Discount] = #Discount WHERE [PlanID] = #original_PlanID

Use RawUrl instead:
Response.Redirect(Request.RawUrl)
The raw URL is defined as the part of the URL following the domain
information. In the URL string
http://www.contoso.com/articles/recent.aspx, the raw URL is
/articles/recent.aspx. The raw URL includes the query string, if
present.
Edit: "without saving the updated data in Grid view"
Why do you think that redirecting the page to itself should save something somewhere?
Have a look at this tutorial: Inserting, Updating, and Deleting Data with the SqlDataSource (C#)
I assume that you actually want to update the GridView instead of the whole page. Then you should call GridView.DataBind().

My guess (without some other informations in the code it is difficult to say) is that althought the information is saved in the database, the datasource for the gridview is not refreshed with the new values.
I have seen this problem often regarding this part.
You have to rebind the gridview with Page.DataBind or GridViewName.DataBind() (where GridViewName is the name of your gridview) before the redirect.
You probably don't reload the GridView at PageLoad or PageLoad does not occure (can't know without the server code). If that would work properly, you would not need the databinding before redirecting.

Related

Losing values after refreshing button asp.net

I have the following project :
It's a page that on Page_Load it fills a TextBox named Email and a TextBox named UserName with a value obtained from asking a database.
Then there is this button, if the email is not null(user is not registered) it will let you register, otherwise it will let you change the email linked to your username.
The thing is, when trying to modify the email, doing an update query, the page preloads, taking the new value placed on Textbox Email the same that is retrieved from the database, making so it will never change.
I've tried to see if it executes the query and it does.
I've tried everything, keeping the variable on a hidden label, creating two different buttons with no luck as when it reloads the code those values are empty again.
I was thinking if I could keep the variable somehow that isn't cookies.
I think You know What is happening.. On every Post back the Page_Load event resetting your Textbox Value
Use IsPostBack to bind the value only on 1st load of page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind dropdown and fill textbox here
TxtName.Text = "Your values";
GetDropdowns();
}
}
I hope this will solve your issue
I totally agree with Kanis XXX, you can use IsPostBack to fill the values only at the start page, and not on other postbacks. In my experience, there are some other advices for your problems:
Using Viewstate, Session state,... to keep your working variable. You can have more detail here: https://kimphuc.wordpress.com/2009/10/18/the-difference-between-viewstate-sessionstate-cookies-and-cache-in-asp-net/
Try to use UpdatePanel, this could be useful in some cases, let you refresh or update data just a part of your page, not the whole page. https://msdn.microsoft.com/en-gb/library/bb398864%28v=vs.100%29.aspx

ASP.NET Reference dynamically created control back in server aspx.cs server code

And my endless problems from server created controls continue.
I am have users that in different groups and each group has different user information. I am creating a page to manage users in a group using a Telerik RadGrid. Because of the unknown nature of the grid columns I am creating the grid on completely on the server. You cannot have a grid defined in the aspx page and add columns in the server aspx.cs code, all kinds of things break like sorting, filters and getting extra text
A feature I need is to output excel files with the grid's data. The problem, how do I reference the grid in server calls backs. If you look at Telerik Grid Export to Excel there is a button callback that changes grid values on the server and initiates the excel export on the grid control in ImageButton_Click. In my case RadGrid1 is created in the server in the Page_Init and added to an asp:PlaceHolder. The grid works fine.
Is there any way to reference a server added control back in the server aspx.cs code. Putting the control id will not compile.
Thanks,
George
I learned that added controls are equivalent to a page controls with runtat="server" . To get a control you can use the find control on a known container object on the page that has a runat.
protected void Page_Init(object source, System.EventArgs e)
{
RadGrid adminGrid = new RadGrid();
adminGrid.NeedDataSource += new GridNeedDataSourceEventHandler(AdminGrid_NeedDataSource);
adminGrid.ID = "AdminGrid";
// lots of code building adminGrid
this.GridPlaceHolder.Controls.Add(adminGrid);
}
protected void AdminGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
// AdminGrid would be the variable name if the control was
// in the page with a runat instead of added programmatically
// with an id of AdminGrid
RadGrid grid = this.GridPlaceHolder.FindControl("AdminGrid") as RadGrid;
grid.DataSource = DataSource;
}

Asp.net controls not updating after a postback

I'm writing code to read data from asp controls to update records in a database. I've been debugging the last day and I've tracked it back to something that I ought to have noticed before.
The code first populates the controls with the existing values from the database.
When I click SAVE, it should read the current values from the controls and save with those.
Unfortunately, what it's actually doing is using the values of the controls before a change was made to them. It's not seeing the change to the controls.
Here's a sample:
<asp:TextBox ID="OtherCourseName_5" runat="server"></asp:TextBox>
Here's the corresponding behind code in the btnSave_onClick() function:
int object_number=5;
string other_course_name_string
= "OtherCourseName_" + object_number.ToString().Trim();
TextBox ocn = utilities
.utils
.FindControlRecursive(this.Master, other_course_name_string) as TextBox;
I'm using the FindControlRecursive() I found somewhere on the web. It works, I'm sure, but just in case, I tried to address the control directly as OtherCourseName_5.Text.
Even if I just display the value in OtherCourseName_5.Text, it gives the original value.
I use this same page for both entering new data and for editing data. It works fine when I enter the data. That is, it correctly sees that the TextBox control has changed from empty to having data. It's only when I invoke the edit function on the page (by passing edit=true). I invoke it this way by adding the switch edit=true as a query string (the program correctly reads that switch, gets to the appropriate area of code, prints out all the correct values for everything - except the contents of the controls!).
The page is far too complicated to post the entire thing. I've tried to convey the essential details. It's entirely possible that I've made a simple coding error, but it's seeming more a possibility that I fundamentally misunderstand how pages are processed.
Is there anything known that can make it seem as though the value of a control has not been changed?
Note 1: I thought perhaps I had to go to another field after I entered the data, but I tried that and it's still a problem.
Note 2: I'm using both TextBox and DropDownList controls and have the same problem with both.
Note 3: These controls are on a panel and the page is using a SiteMaster. I haven't had any problem with that and don't think the problem is there, but I'm down to questioning the laws of the physics at this point.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//populate controls with data from database
}
}
When you do a postback before the postback handler is evaluated the PageLoad event is raised
so if you don't avoid to rebind your control they will be loaded with the values from the database. And then the postback event will save them to db
Asp.net Page Lifecycle
(source: microsoft.com)

How to update a dropdown list on parent page after closing modal window

Okay, so I have an ASP.NET WebForms page with C# code behind. I have a dropdown list bound to a data table in SQL. I am adding a maintenance screen (modal) for users to manage the data in the table/dropdown (add/inactivate records). The user clicks a '+' button next to the dropdown list, the modal comes up, they add or remove a record, and then close the modal. Upon closing (in the 'Close:' property) I'd like to have the dropdown list on the parent page rebind to the updated table data, preferrably without posting back.
I'm having a tough time doing this, anyone have any suggestions?
Code behind to bind drop down to original data on parent Page_Load:
var fundingTypes = client.GetFundingTypeAll();
var onlyActiveFundingTypes = fundingTypes.FindAll(x => x.IsActive == true);
EncryptionHelper.EncryptProperties(onlyActiveFundingTypes);
ddlFundingType.DataSource = onlyActiveFundingTypes;
ddlFundingType.DataValueField = "Id_X";
ddlFundingType.DataTextField = "Name";
ddlFundingType.DataBind();
Could this be done using a WebMethod call to the code behind to do the rebind?
Thank you!
Either use an UpdatePanel that can cause a partial post back or use AJAX calls that can bind the new data on your page (via templating).
I am guessing option #2 (AJAX with templating) will not be a viable solution for you as it would involve re-working the approach to your UI.

Keep Refreshed Table after Refresh Button is Clicked

I have a mock website that I'm creating just for practice. I'm working with AJAX, loader icons, and file uploads. For this, I have a File Upload, an ASP Button to load the file, and HTML Button to refresh the table with AJAX, and a table whose HTML is always built using a StringBuilder.
Here's what I need to happen; right now I'm just setting up my button events. The upload file WILL send a Postback due to a file being uploaded (eventually) but the refresh button will NOT. The page loads the table BEFORE data is added to the database. Then that table is replaced by a new table that displays new data after the refresh button is clicked and new data has been entered. The upload button should not reload the table. The issue I have right now is that I can click the upload after manually adding data and the table remains the same. The refresh shows that data. However, when I hit the upload button AGAIN, the table returns to it's original set of data, before I manually added stuff. Why? I changed the div's html to contain the new table, so why is it going back to the old?
Not sure if I explained this well enough but... there ya go. If anyone can help, that would be great~ I'd love to increase my knowledge and skill-set whenever possible. :)
Here is an image (Please note that I am NOT a web designer and that the overall design is simple and well... just plain bad):
Here is a piece of the back-end .cs code. This is just the Page_Load event, it prevents the table from reloading on a postback which is called from the Upload Button. "GetData()" is just the method that creates the table using a StringBuilder which is then injected into a div with the help of an asp:Literal. I don't see it necessary to post the "GetData()" method but will if requested.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowData.Text = GetData();
}
}
Here is the script portion that uses AJAX to refresh the table by using .get() which refers to an .ashx file that produces the same table as "GetData()". The code in the .ashx is also unnecessary but will post if requested.
<script type="text/javascript">
$("#refresh").click(function (event) {
$.get("helpers/TableLoad.ashx", function (data) {
$("#tableHolder").html(data);
});
});
</script>

Categories

Resources