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..
Related
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.
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!
I've done this a million times without a problem, and I'm stumped at this one.
I have a DataGrid:
<asp:DataGrid ID="gridDeptEmployees" runat="server" AutoGenerateColumns="False"
ItemStyle-CssClass="hovertable" ViewStateMode="Enabled">
<ItemStyle Font-Size="Smaller" />
<Columns>
<asp:TemplateColumn HeaderStyle-CssClass="hidden">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="firstname" HeaderStyle-CssClass="hidden" />
<asp:BoundColumn DataField="lastname" HeaderStyle-CssClass="hidden" />
<asp:BoundColumn DataField="employeeNumber" HeaderStyle-CssClass="hidden" />
</Columns>
</asp:DataGrid>
This datagrid is bound to a list of objects:
Dim userList as New List(Of User)
userList = getList()
gridDeptEmployees.DataSource = ulst
gridDeptEmployees.DataBind()
I can see users in the list when I set a breakpoint and step through it, but the grid is never visible. The visible property is always false!
I've tried explicitly setting the visible property, but it doesn't honor my changes. I even tried adding my DataKeyNames, but that didn't work.
What am I missing here? Should I switch to a different control?
When you can not see a control (not rendered on page), and debug it and see that the visible is false, then is probably because is inside some other hidden control, like a PlaceHolder, or a Panel, or a UserControl.
Check to see if a parent control is hidden to find your reason.
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.
I have a fairly simple asp grid view, tied to an object data source. What I want to have is this gridview update on a button click with results that a dynamic depending on the value within a textbox (it's the basis for a search screen).
So far, everything works as such:
ASPX File:
<h3>Search Parameters</h3>
<div>
Account Name
<asp:TextBox runat="server" ID="AccountName"></asp:TextBox>
</div>
<asp:Button ID="Search" runat="server" Text="Search" OnClick="Search_Click" />
<asp:GridView runat="server" ID="SearchGrid" DataSourceID="ObjectDataSource1" AutoGenerateColumns="False" Width="100%" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" >
<Columns>
<asp:BoundField DataField="PartyID" HeaderText="Party ID" SortExpression="PartyID" />
<asp:BoundField DataField="PartyName" HeaderText="Party Name" SortExpression="PartyName" />
<asp:BoundField DataField="CompleteAddress" HeaderText="Address" SortExpression="CompleteAddress" />
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="DIS.Data.DataSetAccountsTableAdapters.GetAccountsBySearchParametersTableAdapter">
<SelectParameters>
<asp:Parameter Name="PartyName" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
ASPX.CS File:
protected void Search_Click(object sender, EventArgs e)
{
ObjectDataSource1.SelectParameters["PartyName"].DefaultValue = AccountName.Text;
}
At face value, everything works. The user enters text into the account name box, hits search, and the grid view is updated with the appropriate values.
The problem occurs when we get enough row to generate paging. The page links are shown on the GridView, but clicking on them has no effect at all - The values will remain consistently on page 1.
I believe that it may be something to do with the postback when the page link is clicked, but unfortunately my knowledge in this area is not strong enough to actually diagnose what exactly is happening.
Any help would be greatly appreciated
Best regards
OK, on further testing it doesn't appear to be a GridView specific issue.
We aer also using JQuery mobile, and it seems that it's those scripts that are causing the issue. Disabling them removed the problem.