Clear GridView without using the EmptyDataTemplate message - c#

I have an ASP.Net page with a GridView control (id = GeneralGridView), that has an EmptyDataTemplate:
<EmptyDataTemplate>
<strong>No records found!</strong>
</EmptyDataTemplate>
When I first load the page, nothing is bound to the GridView so this message doesn't show.
I have a Clear method that currently does this:
GeneralGridView.DataSource = Nothing
GeneralGridView.Columns.Clear()
GeneralGridView.DataBind()
But using that technique will show the EmptyDataTemplate
Is there any way I can clear the results from a GridView without showing the Empty Template Message and without just reloading the entire page?

Well, you could remove the empty template - you don't need it. It not clear if you simply don't want that template ever showing, or in fact you want to clear the grid, and not show the empty template WHEN you decide to clear the grid?
However, if the user can say enter data, and hit search, then it is RATHER NICE to have the no data show.
but, if you whack your own custom clear button, then the no data will show - so I kind of get your point. You can do this:
GridView1.DataSource = null;
GridView1.EmptyDataTemplate = null;
GridView1.DataBind();
so, your clear button will clear the grid - and NOT show no data template.
You find that if you do a search, and re-bind, then you still get/see the "no data" template, and as noted this is often desired for a search criteria that returns no data, but a clear button should not show "no data".
thus above should work fine.

Related

Store GridView to session C#.Net [duplicate]

I have a page for searching when I write my name for example in textbox it will search and display the results in a GridView. I'm using an EntityDataSource with a QueryExtender so I didn't write any code in C#.
The problem is: in the GridView I have a hyperlink when I click it, it will go to another page, ok fine then when I return to the previous page the GridView does not show the results of the search when I was left because of the postback (update panel) it show the whole data from EntityDataSource.
I think I have to use session but I don't know how I can do it, I mean how I can save the GridView in session and how I can retrieve it in page_load.

How to save the drop down list selected index in asp.net?

i want that after clicking a button saves the index i have selected in order to storage that number to my database. My drop-down-list is in a comment below.
The drop-down-list is filled by using a query. And so far what it does, is that after clicking the button it storages the first index (which is 0), even when i want to save another index.
I have done some research, and so far i cannot solve it, hope someone can help me out.
Code for Dropdown list:-
<asp:DropDownList ID="ddlCategory" runat="server" Height="25px" Width="428px" Font-Size="Large"> </asp:DropDownList>
here's how i fill the drop-down-list
ClsUtil clsutil = new ClsUtil();
DataSet ds = clsutilerias.FillDDL(Category);
ddlCategory.DataTextField = ds.Tables[0].Columns["Name"].ToString();
ddlCategory.DataValueField = ds.Tables[0].Columns["Category_ID"].ToString();
ddlCategory.DataSource = ds.Tables[0];
ddlCategory.DataBind();
The DropDownList has properties. You need to set SelectedIndex, SelectedItem or SelectedValue properly (and in the correct event in relation to when you do the binding etcetera).
Edit: Maybe you updated your question, or maybe I misunderstood, but either way it seems now that you don't want to set the SelectedIndexperhaps as much as get the SelectedIndex. Even so, if you read the documentation and look at the examples provided in the links, you should have enough information to know what to do.
Basically, one of the <option> elements in the <select> on the client (the HTML code) will be selected when the data is posted back to the server. If your <asp:DropDownList> is AutoPostBack="True" you can go for a callback in the OnSelectedIndexChanged event, but there are other ways too, with or without auto-post-back. (You don't really need the view state, or even the <asp:DropDownList> at all, but can trigger a POST by submitting the form data in any way that suits your needs or preferences, and then read any values your interested in server-side.)
It seems that you found your solution in checking IsPostback in the Page, and not re-populate the list if the value is true. That's good. If you don't need to data-bind every time you render the page (even after a POST), that's a viable and common solution.
Another option, of course, would be to read the posted data in any event that happens before you do the data-binding. You may also want to handle the value of SelectedIndex to re-select the correct option in your drop-down after having populating it again, if you don't like having ASP.NET doing it for you.
When you are clicking the button, the entire page is refreshing that why you got the first index all the time
The best solution is :
if(IsPostBack == false)
{
//Save your data code here
}
You have other tricky solution which is :
put the dropboxmenu inside an update panel control
While binding datasource to combo, mention DataMember and DataValue property for combo box so that selecting elements from combo you get selectedValue as DataValue.
Well i have solved my problem, my problem was that when i clicked the button, the pageLoad method executed again, and there was where i had the code to fill my drop-down-list. so i just added:
if(!IsPostBack)
{
//here goes the code to fill the Drop down list
}

namingContainer ID with dynamic gridviewrow

This will not be a very clear explanation of my problem, but I don't know how to explain it better.
I have a gridview which I create dynamically on PreInit. This gridview has textboxes dynamically added on each row.
Everytime I push the button, I loop inside the gridview cells and get the Text of the textboxes -and update the database.
the first time the gridview is created, row uniqueID's are like this:
ctl03, ctl04, ctl05, ctl06 (thus, the textbox ID's are ctl03$txt0 etc..)
The first time I push the button, the row UniqueID's are still the same, so that I can find the controls by FindControl(ID) method, or using Request.Form[txt.UniqueID]
However; after the first time, whenever I push the button, the row ClientId's are created like the following: ctl02, ctl03, ctl04, ctl05.. So that I cannot find the Textboxes and cannot catch the Text written on them.
When I look at the rendered HTML code, I see that the rowClientID's are still the same with the first created ones (ctl03, ctl04, ctl05, ctl06)
Does anyone have any idea why the rowIDs (naming container IDs) change after the first update?
Thanks in advance.
One solution is to create your textboxes with static ids and not let it dynamic create by asp.net. This can be done if you use asp.net ver 4.
Other solution is to just render a simple <input name="KnowName01" id="KnowId01" type="text" value="your value here" maxlength="100" etc... >
and then on post back you just capture the return with the old way and get the value.
Request.Form["KnowName01"]
At the end, the TextBox is nothing more than a render of the input, plus some checks for what write on it, including Anti-XSS safe.

PostbackUrl of dynamic GridViews not what I think they should be

I have a couple GridViews that are dynamically created and placed into a PlaceHolder. When I mouse over the Select button, it shows __doPostBack('ctl00$bodyPlaceHolder$ctl0X','Select$Y'), where X = what I think is the GridView/Control index for the page and Y = row number of that GridView.
Since it is dynamically creating the GridViews, it makes sense that names them ctl0X, but on the PostBack how do I use this information?
I wouldn't even have this problem if adding the SelectedIndexChanged EventHandler worked, but it never gets called.
I found one other question like this, but the answer involved adding a GridView within my GridViews, which would also have to be dynamic, which brings me back to the original problem.
Edit
Okay, so I set gridViewDynamic.ID = "blahblah" + r.LastName, thus giving each GridView a unique name, so on mouseover in the page I get __doPostBack('ctl00$bodyPlaceHolder$blahblahSmith',Select$Y, I still can't access the items on PostBack because they no longer exist. So, I added the same GridView creation code to an if(IsPostBack), then called GridView gView = (GridView)this.Page.FindControl(blahblahSmith). Great, gView isn't null. But all the data in the rows are. Calling gView.Rows[0] returns null.
Use Page.FindControl("TheNameYouGaveTheDynamicGridView")
GridView grid = Page.FindControl("TheNameYouGaveTheDynamicGridView") as GridView;
If you are using MasterPages, you need to take a different approach to find the control on the page, but it is the same premise.

How to hide the empty Column

Using C# & asp.net
if there is no data's in the table, gridview displaying one rows (it was empty)
I want to hide the gridview empty row.
How to do this.
Assuming that you can normally delete that one visible row just check that if a field that would normally have a value is empty and the row count is 1 then delete that row.
if(String.IsNullOrEmpty(mydatagrid.Rows[0][0].ToString()) && mydatagrid.Rows.Count==1) //Check a field that would normally have a value
{
mydatagrid.Rows.RemoveAt(0);
}
Let me know if this helps/works
If you are manually data binding than you can check at that time and hide or disable the control if there is no data. If you are data binding using the design view than you should subscribe to the DataBinding or the PreRender events on the control and check then.
you can check if the datatable doesn't have any rows
use:
mydatagrid.DataSource=null;
mydatagrid.DataBind();
As the other two comments you can either check in code and set MyDataGrid.Visible to true or false to hide the entire table, or you can not bind the datasource, or you can EmptyDataTemplate option to display whatever you want when there is no data for the GridView to display normally.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
The normal behavior for GridView is to render NOTHING if there are no data rows. (Well, almost nothing; it insists on rendering a <div> wrapper, but that'll be empty.)
If you specify an EmptyDataTemplate, however, a one-celled wrapper table is generated to contain whatever is in your template. Even if your template is empty, you'll get that wrapper table (with its one cell, empty). Sounds like the answer to your question is don't specify an EmptyDataTemplate.

Categories