how to clear gridview without reloading the page - asp.net

I have a gridview control on my asp.net page(vb.net). I also have a "cancel" button, that when pressed, is supposed to clear the gridview of it's current contents.
However whenever the cancel button is pressed, it just reloads the page and the gridview is still there with the same data that I wanted clear.
Based on suggestions that I found on stackoverflow, I set the datasource to nothing, but that is not working.
Here is my code for the cancel button:
Private Sub btnCancel_Click(sender As Object, e As System.EventArgs) Handles btnCancel.Click
gvQuizReport.DataSource = Nothing
gvQuizReport.DataBind()
End Sub
Any suggestions would be welcome!
Thanks

You might try:
gvQuizReport.Columns.Clear()
though as #Leniel Macaferi said, hiding the gridview is a possible solution as well.

since you have shown some interest in updatepanels, here is some starter code in case you are unfamiliar:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
If you already have a scriptmanager on your page you don't need to add another (you will get an error). if you have any problems implementing the updatepanel, feel free to post another question, there are plenty of people to help you with it

The code you showed should "clear" the results, but if you do not want anything displayed, you would hide the gridview by using gvQuizReport.Visible = False;

If you really want to clear without reloading the page, you could just use client side script to hide the grid object.
jquery hide

Related

ASP.NET webform Fileupload inside an ascx which is inside an updatepanel in the aspx

here is some code in my page :
<asp:UpdatePanel ID="UpdatePanelEQSelector" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc12:EQSelector ID="custEQSelector" OnEqChange="custEQSelector_OnEqChange" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Inside my user Control, I'm asked to add a fileuploader.
After coding it simply, I tested and my fileuploader is always empty.
I've searched a while and discovered that it was a normal behavior.
The solution to make it work is to add a PostBackTrigger for the updatePanel.
When I tested it in my aspx page, I achieved to do it and my fileUploader had the file.
Then I tried to add it dynamically (to finally do it in my control), it worked with that :
PostBackTrigger trigger = new PostBackTrigger();
trigger.ControlID = this.btnTest.ID;
this._UpdatePanelEQSelector.Triggers.Add(trigger);
But I can't manage to make this code work in my control (I passed my updatePanel as a parameter to my control set in Load, the fileUpload is always empty)
Do you see a solution ?
Thanks
Does it postback, but leaves the control empty? or is it not posting back at all. If it's not posting back at all I'd add this
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnTest);
to the page_load of the user control.
If it is posting back, but the control is empty, then I'd wager there is probably some dodgy html somewhere on the page and the values are getting lost.

How can I refresh a dropdownlist during an event of another dropdownlist without refreshing whole web page?

I am developing my first asp.net website, my requirement is to refresh DropDownListB at SelectedIndexChanged event of DropDownListA, I have set AutoPostBack="True" for DropDownListA. Now the problem is whole web page gets refreshed, its unnecessary for me, is there any other technique that i can use to refresh only that control or only that panel rather than refreshing whole page?
Put the dropdowns inside
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
// Dropdowns
</ContentTemplate>
</asp:UpdatePanel>
and include <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager> at the top
1- You can simply place the dropdown in an UpdatePanel, this will avoid a complete post back.
You can get more details on UpdatePanel here
2- You can use jQuery AJAX to fetch the data in JSON format and bind it to the dropdown list, this approach is more efficient but little complex in comparison to UpdatePanel
You can find so many articles on this if you search this on google , like
[EDIT]
You can find a similar implementation here

gridview delete command within updatepanel, and separate __doPostback() functionality

I have a gridview within an updatepanel, the rows have a delete button which removes that row.
Elsewhere, I run code to insert a row. Following this insert I run __doPostback() with the ID of the updatepanel, then in the updatepanel's load() event I call databind() on the gridview.
As soon as I implement the __doPostback() and databind, the inbuilt gridview delete stops working! :( The actual refresh/databind when adding the row works well.
How can I overcome this? I guess something may be awry in that when clicking on the delete button, the databind is conflicting with the inbuild delete/refresh functionality?
Thanks!
EDIT: Apologies if the question isn't described well...
Essentially, I wish to have a gridview with built-in delete functionality through the datasource and command column etc. inside of an updatepanel. I also want to update this panel seperately, but when I put in this separate update code (gridview.databind in the updatepanel.load) it breaks the standard delete functionality. Hope that is clear :)
You've tried put the UpdatePanelMode as Conditional and use UpdatePanel.Update() besides the ClientScript.RegisterStartupScript during your insertion block ?
I believe the problem is that the event is inside of the GridView and you can't access them as easily as you can with something like a Button. To register the GridView to make the Async events you need to attach it to the ScriptManager.
To do this you use the RegisterAsyncpostBackControl method.
Here is a general idea of how to do it.
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
<%-- your fields, etc --%>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
In your code behind you do
protected void Page_Load()
{
ScriptManager1.RegisterAsyncPostBackControl(GridView1);
}
It's been a while since I've done this but I believe this will allow the GridView to function as you'd expect, except you don't need the extra DataBind() I don't believe in this case.
You can also set the UpdatePanel to Conditional and fire an UpdatePanel1.Update() on top of this as Jeison suggested.
You can find some added details at http://msdn.microsoft.com/en-us/library/bb386452.aspx
If you still have trouble, let us know what happened.
It seems like you calling DataBind() of GridView every time the UpdatePanel load after the insert button click, it reload the data before delete reaches the DataSource.
EDIT
If so, you can add boolean eventArgument in __doPostBack(updatePanelId, "true"). And using this you could add a condition in your updatepanel load event like
if(this.updatepanel1.Page.Request.Params["__EVENTARGUMENT"] == "true"]
this.gridview.databind()
Hope this will resolve the issue.

Event issue with ASP.net Update Panel

I am completely stumped on this and would really appreciate any help.
I am working on a user control that is situated inside of an update panel. There is a button on the form which loads some data. This is working correctly.
There is also a drop-down box to filter the data. Changing this does initiate a post back, however nothing happens. The drop-down box goes back to it's default value the OnSelectedIndexChanged function is never called.
I've put break points in page_prerender and page_preload and both are being hit the post back is definitely occuring. Breakpoints withing the dropdownGroup_changed function are never hit.
Removing the update panel solves the problem, however it breaks the rest of the page so I can't use that for anything other than testing.
I've also verified that there is nothing in my prerender / page load that is resetting the page's state.
Here is the update panel code:
<asp:UpdatePanel ID="UpdatePanel6" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
<ContentTemplate>
<ucControlName:ControlName ID="ControlName1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Here is the drop-down in question - It is located inside of the user control
<asp:DropDownList ID="dropdownGroup" runat="server" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="dropdownGroup_changed"></asp:DropDownList>
It is of course visible and databound by the point in the code where the issue is occuring
A bit more info-
added Both a hard coded dropdown (To rule out a stupid databinding issue) and a textbox to the same control. I have the same issue.
It appears that the event isn't triggering because the values are never changing as far as .net is concerned. I've checked the control during page_init and page_load - the value is always the same.
The fact that the button works but the other controls don't makes me think that there is a view state issue here somewhere but I can't quite ferret out what is causing it. Viewstate is enabled for the page and the panel- don't know if anything else could be overriding / corrupting it.
Did i mention that I hate update panels with a passion? because I hate update panels with a passion.
I suggest checking the 'Value' property for each 'ListItem' in the 'DropDownList' control. If they are all the same even if the 'Text' properties are different, then the 'OnSelectedIndexChanged' will not fire at all since ASP.NET cannot tell if anything has changed (See this related question for more info.)
This was the real cause of my problem even though I, too, had a 'UserControl' with a 'DropDownList' inside an 'UpdatePanel' and the 'AutoPostBack' was firing as expected. I thought the UpdatePanel was the culprit but it was not the case. Each of the items in my DropDownList had the same underlying value of "10" even though they had distinct 'Text' values. I changed them to each have a unique value which then allowed for the OnSelectedIndexChanged event to fire thus fixing the problem.
Two answers for the price of one:
Are you calling DataBind() in your Page_Load? If you do that on a PostBack, you will lose events. Replace the call with the following:
if (!IsPostBack) {
DataBind();
}
If your DropDownList is outside your UpdatePanel, you need to add a Trigger as follows:
<asp:UpdatePanel ID="UpdatePanel6" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="dropdownGroup" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<ucControlName:ControlName ID="ControlName1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Have you tried UpdatePanel.Update (); after your databind.

ASP.NET AJAX No update of screen on partial postback

We have an issue on our page whereby the first time a button posts back (we have ASP.NET ajax to enable partial updates) nothing happens, then on every subsequent click, the information is updated as if the previous event fired.
Here's the code for the page. The events are button clicks fired from within the table. Which is rerendered at the backend.
<asp:ScriptManager EnablePartialRendering="true" ID="scrptMgr1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="folderContainer">
<ContentTemplate>
<asp:Table id="FolderTable" CssClass="FolderTable" runat="server" CellSpacing="0"></asp:Table>
</ContentTemplate>
</asp:UpdatePanel>
Any ideas?
Thanks.
Did you try an HTML table, with or without runat="server" ?
Do you add or remove controls inside the table in the postback?
Inspired by Robert's answer:
In which event handler do you build your table? If you build it in Page_Load without checking IsPostBack), then the button's click event has not been handled yet.
You need to build the table in the button click handler.
Your table is probably being populated too late in the page lifecycle. Try creating the table in PreInit.

Resources