SelectedIndexChanged event of gridview fails to fire randomly - asp.net

Hi I have a Gridview SelectedIndexChanged event. it fires on click of any row in that grid. But sometime (once in 10 tries) it doesnt fire. i have some code inside AgeGrid_SelectedIndexChanged which set the session variable. when it fails not able to get proper value.
Any Idea why it fails randomly?
i have not set EnableEventValidation="false" property for that page. And using visual studio 2005.
<Asp:GridView ID="agendaGrid" runat="server" Width="96%" GridLines="Both" EnableViewState="true" HeaderStyle-CssClass="GridViewHeader "
OnSorting = "AgeGrid_Sorting" CssClass="GridView"
AutoGenerateColumns="false" OnSelectedIndexChanged="AgeGrid_SelectedIndexChanged"
AllowPaging="true" PageSize="10" ItemTemplateType="PopUpMenu"
ShowTemplateFields="true" EnableOnClick="true" RowStyle-Font-Size="13px" RowStyle-VerticalAlign="middle"
EnablePopUpClick="true" OnRowDataBound="AgeGrid_RowDataBound" AllowSorting ="true"
>
</Asp:GridView>
Any Suggestions are welcome.
is there any thing i need to add or delete from page property or Gridview property?

protected void AgeGrid_SelectedIndexChanged(object sender, GridViewPageEventArgs e)
{
agendaGrid.PageIndex = e.NewPageIndex;
BindGrid();
}
may problem in binding data after paging, not eror in pagination.
May its helps you.

Related

Getting an Instance of Dropdown list when clicking update on GridView in Asp.Net

I have a GridView with more than 30 columns. Most are plain controls but for some I have added a template control (DropDownList, Calendar and CheckBox control). Here is the aspx code for the control in question
<asp:TemplateField HeaderText="Field1 Caption" SortExpression="Field1">
<ItemTemplate>
<asp:Label ID="lblConstructionArea" runat="server" Text='<%# Eval("Field1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlField1" EnableViewState="true" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
I wanted a dropdown to be shown on the column when a user clicked on Edit. So I add this code (and the above EditItemTemplate)
protected void gvData_RowEditing(object sender, GridViewEditEventArgs e)
{
string fieldOne = CommonUtils.ExtractControlValue(e,"lblField1",gvData);
gvData.SelectedIndex = e.NewEditIndex;
gvData.EditIndex = e.NewEditIndex;
gvData.DataBind();
BindGridDropDownData(e, CommonUtils.GetConstructionAreas() ,"ddlConstructionArea", constructionArea, "Field1", fieldOne);
}
In the above code I am getting the current available and passing it to another method so that when the dropdown is displayed the selected index can be shown accurately. After this I do a change on the dropdownlist and click on the "Update button" on the GridView and the following event is triggered
protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowEditIndex = e.RowIndex;
GridViewRow gRow = gvData.Rows[rowEditIndex];
DropDownList ddlConstructionArea = (DropDownList) gvData.Rows[rowEditIndex].FindControl("ddlConstructionArea"); //This does not work
ddlConstructionArea = (DropDownList)gRow.FindControl("ddlConstructionArea");//This does not work
ddlConstructionArea = (DropDownList)gvData.Rows[rowEditIndex].Cells[7].FindControl("ddlConstructionArea");//this does not work either
gvData.EditIndex = -1;//this works and the text boxes disappear
gvData.DataBind();//this works and the old data shows up on the gridview
}
I am curious as to how to do an update on a Grid where I have the binding is runtime.
Actually in the markup, you have given ID of dropdownlist as ddlField1 and in codebehind, you are reffering it as ddlConstructionArea. Is this is what causing the update not to function?
The problem was with the way the grid was being bound. I had written code to refresh the grid on page load and anytime I clicked on the Edit button the page was being refreshed and the grid binding was getting triggered instead of the updating event. I removed the code refreshing the grid on page load and put it in the places where it is needed and the order of events were getting triggered the way I would want it to and the update worked perfectly without a problem.

GridView Custom Paging - First, Last, Next, Previous,Numeric

I am using a sql store procedure for custom paging.
Now i want to make a custom pager for gridview.
I have searched on google and find various articles about it.
But i am not getting which i want.
please help me for it if you have any idea about it.
Thanks,
Rajbir
I think you looking for something like this
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text = '<%#Eval("Text") %>' CommandArgument = '<%# Eval("Value") %>' Enabled = '<%# Eval("Enabled") %>' OnClick = "Page_Changed"></asp:LinkButton>
</ItemTemplate>
You will find more form Custom Paging in ASP.Net GridView using SQL Server Stored Procedure
there is nothing to do with Stored Procedure to use Paging in Gridview. Only you need to do is to bind your GridView on OnPageIndexChanging event.
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
Please visit this link for example: ASP.NET GridView with Custom Paging UI

Invalid postback or callback argument - button in Telerik grid

Very famous error message (see below), judging by the number of Google results. But every one of them I've seen suggests to set EnableEventValidation to false. I have searched my entire codebase, and I cannot find the string "EnableEventValidation" anywhere. Moreover, this code used to work; something I have done has obviously broken the pages. But what?
The error happens when I click on a button inside a Telerik RadGrid, declared as:
<telerik:RadGrid ID="MyGrid" Width="100%" ItemStyle-BorderColor="Gainsboro"
ItemStyle-BorderStyle="Solid" ItemStyle-BorderWidth="1px" ActiveItemStyle-BackColor="Bisque"
SelectedItemStyle-BackColor="Black" AllowPaging="True" PageSize="15" runat="server"
AllowSorting="true" OnItemCommand="MyGrid_ItemCommand" AutoGenerateColumns="false"
OnNeedDataSource="MyGrid_NeedDataSource" GridLines="Horizontal" AllowMultiRowSelection="false"
Skin="Black">
<GroupingSettings CaseSensitive="false" />
<MasterTableView Width="100%" DataKeyNames="ID" AllowFilteringByColumn="false" Font-Names="Arial"
Font-Size="10px">
<Columns>
<telerik:GridButtonColumn ButtonType="PushButton" Text="Cancel" CommandName="Cancel"
ConfirmText="Are you sure you want to cancel this?">
</telerik:GridButtonColumn>
...
</Columns>
</MasterTableView>
<PagerStyle Mode="NextPrevAndNumeric" />
<FilterMenu EnableTheming="True">
<CollapseAnimation Duration="200" Type="OutQuint" />
</FilterMenu>
</telerik:RadGrid>
click on the "Cancel" button, and here's the famous error:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Here's the problem: in my Page_Load method I had:
protected void Page_Load(object sender, EventArgs e) {
MyGrid.Rebind();
}
The rebinding of the grid on postback was obviously screwing something up. I changed it to:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
MyGrid.Rebind();
}
}
and everything is working now.
I had the same problem but I had no Grid.Rebind() or Grid.Databind() in my NeedDataSource method or Page_Load method. This happened just after I drag a column to be grouped and then order the grouped column ASC/DESC
I simply added
EnableEventValidation="false"
in the <%# Page %> tag of my .aspx page. The ordering fails but at least I no longer get the error. As a note everything else works perfectly except the ordering of a grouped column
here is the code I use in the NeedDataSource method
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
String connstr = ConfigurationManager.ConnectionStrings["PrimeIntegartionsConnectionString"].ConnectionString;
SqlDataSource Ds = new SqlDataSource(connstr, BuildSql()); //buildsql simply returns a SQLSelect String "select * from example"
RadGrid1.DataSource = Ds;
}

ASP.NET : Access controls declared in TemplateColumn of DataGrid

ASCX File:
<asp:datagrid runat="server" id="gridFormFields" datakeyfield="FieldID"
autogeneratecolumns="False"
onitemcommand="gridFormFields_ItemCommand" onitemdatabound="gridFormFields_ItemDataBound">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:imagebutton runat="server" id="buttonMoveUpFormField" resourcekey="buttonMoveUpFormField"
commandname="Item" commandargument="MoveUp" imageurl="~/images/up.gif" />
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn>
<itemtemplate>
<asp:imagebutton runat="server" id="buttonMoveDownFormField" resourcekey="buttonMoveDownFormField"
commandname="Item" commandargument="MoveDown" imageurl="~/images/dn.gif" />
</itemtemplate>
</asp:templatecolumn>
</columns>
Code behind:
protected void gridFormFields_ItemDataBound(object sender, DataGridItemEventArgs e)
{
(e.Item.FindControl("buttonMoveUpFormField") as ImageButton)
.Visible = gridFormFields.Items.Count > 1 && e.Item.ItemIndex > 0;
(e.Item.FindControl("buttonMoveDownFormField") as ImageButton)
.Visible = gridFormFields.Items.Count > 1 && e.Item.ItemIndex < gridFormFields.Items.Count - 1;
}
In the code behind, the Control returned by FindControl is null. Why?
How can I access the buttonMoveUpFormField and buttonMoveDownFormField controls?
From the code behind, is it possible to access controls which are declared in the ItemTemplate section of the TemplateColumn section of a DataGrid?
Because you need to add code to include "Item" and "AlternatingItem" and exclude all other types, before you try to find that control.
if (e.Item.Type == ...
You can certainly access the controls that are within the ItemTemplate section. I'm dealing with a similar issue. One thing that I've found is, depending what is calling your "gridFormFields_ItemDataBound", you may not have access to those controls yet.
I know that in my instance, I've got an "ItemTemplate" and an "EditItemTemplate", when I click edit, it fires an event "RowEditing" before it is actually switched to "Edit Mode", so the control will not be there yet. I do though have access to the controls in "RowUpdating" which is fired when I click save in the edit mode.
Maybe this helps? For example, your "OnDataBound" might be the event that is trying to access your controls, but you may not have access to them on databound?
Just a thought. I'll edit this if I get any further on mine.

asp.net listbox problem

foreach (Book b in o.list)
{
ListBox_Items.Items.Add(b.Title);
}
After I do this, the titles are now showing up in the listbox.
When I make a selection (Single Mode), ListBox_Items (Screen) is highlighting the row selected, but event SelectedIndexChanged is not triggering.
protected void ListBox_Items_SelectedIndexChanged(object sender, EventArgs e)
{
int i = ListBox_Items.SelectedIndex;
}
ID="ListBox_Items" runat="server" EnableViewState="False" Width="400px" Rows="25" onselectedindexchanged="ListBox_Items_SelectedIndexChanged"
Any ideas ?
Michael
Edit 1 : Thanks to everyone for helping out. Got it to work now. Anyway, I had to turn on EnableViewState to True too. Because I have a "Remove" button to remove items from the listbox control, if EnableViewState is False, whenever I clicked the Remove button, the listbox becomes empty again.
Add AutoPostBack="True" in your aspx tag
Try the following code.
<asp:ListBox ID="ListBox_Items"
runat="server"
EnableViewState="False"
Width="400px"
Rows="25"
OnSelectedIndexChanged="ListBox_Items_SelectedIndexChanged"
AutoPostBack="true"></asp:ListBox>
Do you have anything to make the page post back to the server?
You may need either a submit button, or you can add the property AutoPostBack="true" to your ListBox control.
See this MSDN Article for more information.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback.aspx

Resources