Empty gridview before user selction? - asp.net

I've set up an .aspx gridview that shows data from an SQL database based on the user selecting the parameter in a drop-down list.
What I'd like to achieve is that the gridview is empty before the user has made a selection. Is this possible?
There will always be data in the database, so the question is NOT about what to display when there are no rows to show.
Thanks in advance for any assistance!
//Eva-Lotta

ASP.Net 4.0 added the boolean ShowHeaderWhenEmpty property.
<asp:GridView runat="server" ID="GridView1" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</Columns>
</asp:GridView>
The headers will not appear unless DataBind() is called with something other than null.
GridView1.DataSource = New List(Of String)
GridView1.DataBind()

I've solved it in a really simple way! I set gridview visible to false on page load, and true when the user clicked a button. Sometimes it's much easier than expected.

Related

Dropdownlist autopostback property is getting triggered even when I click any other options in gridview or on any other control on the same page

The dropdownlist and the gridview are on the same page.
Code for dropdownlist :-
<asp:DropDownList OnSelectedIndexChanged="drplist_SelectedIndexChanged"
AutoPostBack="true" ViewStateMode="Disabled" AppendDataBoundItems="true"
ID="drplist" runat="server" DataSourceID="datasource1"
DataTextField="User_ID" DataValueField="User_ID">
<asp:ListItem Value="" Text="Make a selection"/>
</asp:DropDownList>
Code for gridview :-
<asp:GridView ID="gview" runat="server" Width="100%" AutoGenerateColumns="false" DataKeyNames="User_ID" DataSourceID="datasource">
<Columns>
<asp:BoundField DataField="User_ID" HeaderText="User ID" />
<asp:BoundField DataField="User_Name" HeaderText="User Name" />
<asp:BoundField DataField="User_Email" HeaderText="User e-mail" />
<asp:BoundField DataField="User_Mob" HeaderText="User mobile number" />
<asp:BoundField DataField="User_Created" HeaderText="Created Date" />
<asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
</Columns>
</asp:GridView>
The dropdown has User IDs from the database and when a selection is made from the dropdown it fires it selectedIndexChanged event , code for the same:-
protected void drplist_SelectedIndexChanged(object sender, EventArgs e)
{
var index = drplist.SelectedValue;
drplist.SelectedIndex = 0;
Response.Redirect("~/Demo/TableDemo.aspx?User_ID="+index);
}
Now when I click browser's back button , the dropdown still retains its selected value, which I do not want, I want it to reset to index 0. As I return to the dropdownlist from the TableDemo.aspx page and click on the edit button or delete button in gridview , the postback for the dropdown is triggered again taking me to the TableDemo.aspx page. Only a manual page refresh sets things straight. I tried to search and implement JS code for forcing page refresh on pressing browser back button but it didn't solve my issue and created more. I have tried using the update panel to my frustration and it happens to do nothing to solve my issue here. Sorry for the long post I couldn't do better in making the scenario shorter in writing here.
You probably have your page cached, thats the reason it doesn't behave the way you want when you click the back button. Try to add this code to your page_load on the page you don't want to be cahched.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Hope this helps!

Gridview ASP.NET is not shown when loading webpage

well I'm so new to ASP.NET I've been working on this project for about two weeks now, but I can't get a GridView with an objectdatasource to show when webpage loads, I can see all columns in design time.
The method returns a List and in design time I can see all columns with the right column name but when loading the service and the webpage afterwards nothing shows up. Any help will be appreciated. Regards to all readers.
The method receives a string value I've set a default to 'q'.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="Contraseña" HeaderText="Contraseña"
SortExpression="Contraseña" />
<asp:BoundField DataField="Id_User" HeaderText="Id_User"
SortExpression="Id_User" />
<asp:BoundField DataField="Nombre" HeaderText="Nombre"
SortExpression="Nombre" />
<asp:BoundField DataField="Tipo" HeaderText="Tipo" SortExpression="Tipo" />
<asp:BoundField DataField="Usuario" HeaderText="Usuario"
SortExpression="Usuario" />
</Columns>
</asp:GridView>
Add the following to your GridView:
<asp:GridView ID="GridView1" runat="server"
EmptyDataText="DOH! No Data!"
ShowHeaderWhenEmpty="True"
The code looks fine. You don't actually need to call databind() with Data Source Control, instead perhaps you need to check the return value in SelectMethod of objectdatasource.

Editing a gridview using the updating event in VB.net

What im trying to accomplish is to try to edit a gridview by using the updating event but not sure how to do this. The way im populating the datagrid is putting it into a dataSet and using that as the dataSource and doing a databind. Here is my datagrid below. Any help would be very helpful.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="AIRCRAFT" HeaderText="ProductID" />
<asp:BoundField DataField="ENGINE" HeaderText="Product" />
<asp:BoundField DataField="LEMAC" HeaderText="Price" />
<asp:CommandField ShowEditButton="true" />
</Columns>
<SelectedRowStyle BackColor="#99CCFF" />
</asp:GridView>
When the user clicks on the "Update" button, the page does a Postback and then changes the controls of the row in question, allowing the Update.
Besides, it enables 2 buttons: Update and Cancel.
Cancel, will leave the GridView as it was before the first click.
Update, will send the user modified' data to saving.
In order to allow update, you have to capture the RowUpdating event. Something like this:
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
' Do your Update here
End Sub

asp grid view check box to presrver on neigation(paging)

In a grid view i have used paging for that i have used the view state to store datatale to bind it on GridView1_PageIndexChanging event every thing works fine but the problem happens with the first column which is having the checkbox placed in each row .
On navigation all checked check box becomes unchecked how to maintain the state of check box as well.
this is the aaspx code
<Columns>
<asp:TemplateField HeaderText="Select Student">
<ItemTemplate>
<asp:CheckBox id="Chek" runat="server" Text="select" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Enrollment No." DataField="enrollment_no" />
<asp:BoundField HeaderText="Course Name" DataField="course_name"/>
<asp:BoundField HeaderText="Branch Name" DataField="branch_name"/>
<asp:BoundField HeaderText="Email Id" DataField="email" />
<asp:BoundField HeaderText="Mobile" DataField="mobile"/>
<asp:BoundField HeaderText="Name" DataField="first_name"/>
<asp:BoundField HeaderText="Surname" DataField="last_name" />
</Columns>
</asp:GridView>
Viewstate is intended for postbacks to the same page.
To preserve state when navigating to other pages here are 3 options:
Put your checkbox (or simply true/false) in the Session
Use the PreviousPage property
Or use cookies
Summary of option #2
If you have ot post accross pages,
cookies can be used, and also Cross
Page Posting by setting the
PostBackURL property of a button, then
the POST request is directed at the
specified page, and you can get the
values from the PreviousPage property
of the next page.
Example of using option #3, the Session:
//Set
Session["mySessionVariableName"] = myCheckBox;
//Get
CheckBox myCheckBox = (CheckBox)Session["mySessionVariableName"];
I summarized in more detail here and here

RadGrid not firing postback on ItemCommand events

I'm currently evaluating some RAD controls from Telerik, just right now I'm experimenting with the RadGrid.
So I have my grid control and enabled client-side binding for having Ajax support. I created an appropriate WCF webservice for fetching the data etc. Everything works really good, including paging etc. Now I wanted to have a button column for deleting some items. I registered the OnItemCommand event of the grid and implemented it accordingly on the server-side. My ASPx code looks like this:
<telerik:RadGrid runat="server" ID="RadGrid1" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" GridLines="None"
OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView DataKeyNames="Id" ClientDataKeyNames="Id">
<Columns>
<telerik:GridBoundColumn DataField="Firstname" HeaderText="Firstname" DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Lastname" HeaderText="Lastname" DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Age" HeaderText="Age" DataType="System.Int32">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"
ButtonType="ImageButton">
</telerik:GridButtonColumn>
</Columns>
<PagerStyle Mode="Slider" />
</MasterTableView>
<ClientSettings>
<DataBinding SelectMethod="GetSampleData" Location="Webservice/GridData.svc" SortParameterType="String">
</DataBinding>
</ClientSettings>
</telerik:RadGrid>
However when clicking on the appropriate button on a grid row the event isn't fired, basically no postback to the server is being done. A solution I found is to add the "EnablePostBackOnRowClick=true" to the ClientSettings, but this would cause a postback on each click on a row, which is not really desired.
Is there a better way to realizing this or does anybody have a hint what could be the problem??
Thx
As far as it seems this is not possible given the answer from the Telerik forum.
you need to handle the client "OnCommand" event, or more appropriately use the client "RowDataBound" command. In the RowDataBound command you can find your rad button and attach an event to it.
The only other way to do this is to handle the client "onclicking" event from the button itself.
Example of binding to the OnCommand and Row DataBound:
<ClientSettings>
<ClientEvents OnCommand="Grid_Command" OnRowDataBound="Grid_RowDataBound" />
</ClientSettings>
then in your javascript wrapped in a rad code block have the following methods:
<script type="javascript">
function Grid_RowDataBound(sender, args) {
var item = args.get_item();
var data = args.get_dataItem();
var btn = $find('DeleteColumn');
btn.add_clicking(delegate); // where delegate is the function you provide for the click
// ... //
}
`
I realize this is ancient, but it still shows up high in Google results.
There is now a solution to this, maybe others as well...
You can achieve a postback by using a template column
<telerik:GridTemplateColumn UniqueName="myuniquename">
<ItemTemplate>
<telerik:RadButton ID="RadButton1" runat="server" ButtonType="StandardButton" AutoPostBack="true" CommandName="MyCommand" UseSubmitBehavior="false" Text="Button Text" />
</ItemTemplate>
</telerik:GridTemplateColumn>
Although I'm not sure if you need the "UseSubmitBehavior" property.
I had the same problem with telerik controls. I solved this problem by recreating the Control from zero with a new name, then rebuilding my structure.
Hope it helps
RegisterWithScriptManager="false" this may work as well..

Resources