RadGrid Paging does not work fine after changing page - c#

I have a RadGrid in my ASP.Net app and I have set the AllowPaging to True and PageSize to 10, now it load 10 items per RadGridPage which is what I wanted, but as soon as I press Next Page button (Arrow look button) nothing loads and RadGrid gets empty.
How can I make it work normal?
protected void Page_Load(object sender, EventArgs e)
{
PopulateGridOnLoad();
}
private void PopulateGridOnLoad()
{
rgCustomers.DataSource = odsCustomers;
// your datasource type
rgCustomers.MasterTableView.VirtualItemCount = 28;
//your datasource type total/count
rgCustomers.CurrentPageIndex = rgCustomers.MasterTableView.CurrentPageIndex;
rgCustomers.Rebind();
}
protected void grdName_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
rgCustomers.DataSource = odsCustomers;
// your datasource type
rgCustomers.MasterTableView.VirtualItemCount = 28;
//your datasource type total/count
rgCustomers.CurrentPageIndex = rgCustomers.MasterTableView.CurrentPageIndex;
//Donot rebind here
}
protected void btnLoad_Click(object sender, EventArgs e)
{
odsCustomers.SelectParameters["CustomerFullName"].DefaultValue = txtFullName.Text;
odsCustomers.SelectParameters["CustomerMelliCode"].DefaultValue = txtMelliCode.Text;
odsCustomers.SelectParameters["CustomerHomeAddress"].DefaultValue = txtHomeAddressPart.Text;
odsCustomers.SelectParameters["CustomerWorkAddress"].DefaultValue = txtWorkAddressPart.Text;
rgCustomers.DataSource = odsCustomers;
rgCustomers.DataBind();
}

You will have to set up following attributes of the grid in design
<telerik:RadGrid ID="grdName"
AllowPaging="True"
AllowCustomPaging="True"
VirtualItemCount="0" PageSize="15" >
Populate grid on load vb.net
Private Sub PopulateGridOnLoad()
grdName.DataSource = source ' your datasource type
grdName.MasterTableView.VirtualItemCount = source.Total 'your datasource type total/count
grdName.CurrentPageIndex = grdName.MasterTableView.CurrentPageIndex
grdName.Rebind()
End Sub
Populate grid on load c#.net
private void PopulateGridOnLoad()
{
grdName.DataSource = source;
// your datasource type
grdName.MasterTableView.VirtualItemCount = source.Total;
//your datasource type total/count
grdName.CurrentPageIndex = grdName.MasterTableView.CurrentPageIndex;
grdName.Rebind();
}
Override NeedDatasource vb.net
Protected Sub grdName_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdName.NeedDataSource
grdName.DataSource = source ' your datasource type
grdName.MasterTableView.VirtualItemCount = source.Total 'your datasource type total/count
grdName.CurrentPageIndex = grdName.MasterTableView.CurrentPageIndex
'Donot rebind here
End Sub
Override NeedDatasource c#
protected void grdName_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
grdName.DataSource = source;
// your datasource type
grdName.MasterTableView.VirtualItemCount = source.Total;
//your datasource type total/count
grdName.CurrentPageIndex = grdName.MasterTableView.CurrentPageIndex;
//Donot rebind here
}

After a long time I found the solution, the problem involved 2 minor issues :
1.I had both DataSource (in code) and DataSourceID (property) set and they didn't work together well
2.I had both AllowPaging and AllowCustomPaging set to true, when they are both true neither works :) that's the telerik team you know, but they are great I was kidding

You need to define the "onNeedDataSource" radgrid event where you shoud reset the datasource of your grid.
protected void RadGrid_NeedDataSource(object sender, EventArgs e)
{
IsNeedDataSource = true;
}
and than in page OnPreRender event you shoul do smth like:
protected override void OnPreRender(object sender, EventArgs e)
{
DriverLinksGrid.DataSource = value;
DriverLinksGrid.DataBind();
}
I'm not shure, maybe you can bind data right in OnNeedDataSource event. But the DataBind() method may be not not available from there.

Related

How to save option selected in Dropdownlist after selection

I am displaying columns in a GridView and one of the columns is a dropdownlist. I want to be able to save the option selected in the dropdownlist as soon as something is selected. I have done this with one of the columns that has a textbox so I was hoping to do something similar with the DropDownList.
The code for the textbox and dropdownlist:
protected void gvPieceDetails_ItemDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
JobPieceSerialNo SerNo = e.Row.DataItem as JobPieceSerialNo;
if (SerNo != null) {
TextBox txtComment = e.Row.FindControl("txtComment") as TextBox;
txtComment.Text = SerNo.Comment;
txtComment.Attributes.Add("onblur", "UpdateSerialComment(" + SerNo.ID.ToString() + ", this.value);");
DropDownList ddlReasons = (e.Row.FindControl("ddlReasons") as DropDownList);
DataSet dsReasons = DataUtils.GetUnapprovedReasons(Company.Current.CompanyID, "", true, "DBRIEF");
ddlReasons.DataSource = dsReasons;
ddlReasons.DataTextField = "Description";
ddlReasons.DataValueField = "Description";
ddlReasons.DataBind();
ddlReasons.Items.Insert(0, new ListItem("Reason"));
}
}
How to I create an update function for a dropdownlist?
protected void DDLReasons_SelectedIndexChanged(object sender, EventArgs e)
{
string sel = ddlReasons.SelectedValue.ToString();
}
public static void UpdateSerialReason(int SerNoID, string Reasons)
{
JobPieceSerialNo SerNo = new JobPieceSerialNo(SerNoID);
SerNo.Reason = sel; //can't find sel value
SerNo.Update();
}
Dropdownlist:
<asp:DropDownList ID="ddlReasons" runat="server" OnSelectedIndexChanged="DDLReasons_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
I created an OnSelectedIndexChanged function to get the selected value. But how do I then save that value? Is there a way to pass it into the UpdateSerialReason function?
Just move the string sel declaration outside the scope of DDLReasons_SelectedIndexChanged and get the Text of the SelectedItem since it's included in your data source.
private string sel;
protected void DDLReasons_SelectedIndexChanged(object sender, EventArgs e)
{
sel = ddlReasons.SelectedItem.Text;
}
public static void UpdateSerialReason(int SerNoID, string Reasons)
{
JobPieceSerialNo SerNo = new JobPieceSerialNo(SerNoID);
SerNo.Reason = sel; // Should now be available
SerNo.Update();
}
The way you had it previously it was only available in the local scope, i.e, inside the method in which it was being declared and used.
You can get selected value when you call your function:
UpdateSerialReason(/*Some SerNoID*/ 123456, ddlReasons.SelectedValue)
You will lose your value after postback is done if you save value to variable as Equalsk suggested. If you need to use your value on the other page you can save it in session.
If you are working within one asp.net page you can do as I suggested above. Then you can skip the postback on your DropDownList and call UpdateSerialReason when you need :)
And you might want to add property ViewStateMode="Enabled" and EnableViewState="true"

Rad Grid Not Selecting Row On Row Select

I have a Rad Grid that I bind a data source to in the Page Load. I am trying to then capture the row that is selected when the user clicks a row. I have successfully done this with a Rad Grid that has a data source set in the markup. Is it possible to do this with a grid with a dynamically set data source.
Markup
<telerik:RadGrid ID="rgTable" runat="server" OnSelectedIndexChanged="rgTable_OnSelectedIndexChanged">
<ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true">
<Selecting AllowRowSelect="true" />
</ClientSettings>
</telerik:RadGrid>
C#
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["ID"]);
ConnString = Connections.Search(0, 999999, null, null, null, id, null, null, null, null, null)[0].Connection_String;
StoreTableAndViewNames();//This method sets arrTables with ArrayList
rgTable.DataSource = arrTables;
rgTable.DataBind();
}
protected void rgTable_OnSelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (rgTable.SelectedItems.Count > 0)//This test is failing
{
GridDataItem item = (GridDataItem)rgTable.SelectedItems[0];
string table = item.Cells[0].ToString();
StoreColumnNames(table);
rgColumn.DataSource = arrColumns;
rgColumn.DataBind();
}
}
catch (Exception ex)
{
errorlbl.Text = ex.ToString();
}
}
In your code, Data is bind to rgTable on ever page load.
As the result, when a row is selected at client side, data is bind torgTable again before OnSelectedIndexChanged is fired.
The quick fixes is not to bind rgTable on post back.
protected void Page_Load(object sender, EventArgs e)
{
....
if (!IsPostBack)
{
rgTable.DataSource = arrTables;
rgTable.DataBind();
}
}
However, preferred method is to use NeedDataSource event
If you bind data in NeedDataSource event, RadGrid knows when & where to look for data if it is needed.
protected void rgTable_OnNeedDataSource(object sender,
GridNeedDataSourceEventArgs e)
{
rgTable.DataSource = arrTables;
}

Show dropdown selected value in the grid on the same page

I have a drop down which has the list of delegates. When the user selects a delegate then I populate the available meet-timings in the second dropdown using the selectedindexchange event of the 1st dropdown
Aspx page
<asp:DropDownList ID="delegate_ddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddldelegates_SelectedIndexChanged" Width="200px"></asp:DropDownList>
<asp:DropDownList ID="delegatetime_ddl" runat="server" Width="90px"></asp:DropDownList>
<asp:Button ID="adddelegate" runat="server" Text="Add" onclick="adddelegate_Click"/>
.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds1 = getdata.getdelegatelist();
delegate_ddl.DataSource = ds1.Tables[0];
delegate_ddl.DataTextField = "DELEGATE_NAME";
delegate_ddl.DataValueField = "DELEGATE_ID";
delegate_ddl.DataBind();
delegate_ddl.Items.Insert(0, "--Select--");
}
}
protected void ddldelegates_SelectedIndexChanged(object sender, EventArgs e)
{
string delselection = delegate_ddl.SelectedValue.ToString();
DataSet ds2 = getdata.getdelegatetimelist(delselection);
if (ds2.Tables[0].Rows.Count > 0)
{
delegatetime_ddl.DataSource = ds2.Tables[0];
delegatetime_ddl.DataTextField = "TIMESLOT";
delegatetime_ddl.DataValueField = "TIMEID";
delegatetime_ddl.DataBind();
}
else
{
time_lbl.Text = "No slots Open";
}
}
protected void adddelegate_Click(object sender, EventArgs e)
{
string delegateselected = delegate_ddl.SelectedValue.ToString();
string timeslotselected = delegatetime_ddl.SelectedValue.ToString();
getdata.delegatemeetinsert(personidd, delegateselected, timeslotselected);
}
Now the data gets inserted – but my question here is
As soon as the user click add button I would like to display the delegate selected and the time slot selected in a some sort of a grid view or dynamic table below with a delete option.
Can someone please provide a code sample in C# to achieve the above
Instead of using Gridview for displaying data use some basic control like Labels,it will reduce lot's of unncessary code. use Asp.net 'Panel' control and encapsulate all label and button(for delete purpose) into Panel then hide/show according to need. here is the outline of code that might help you,
if(!page.IsPostBack) // This goes into Page_Load
{
Panel1.Visible=false;
}
protected void adddelegate_Click(object sender, EventArgs e) // add this additional code
{
Panel.Visible=True;
GetDelegate()// This method retrieve the delegate you inserted..
Lable1.Text= "Set here Delegate name you just Retrieved"
Label2.Text="Delegate time you retrived"
}
protected void BtnRemovedelegate_Click(object sender, EventArgs e)
{
string Personidd= retrieve person id
string delegateName= Lable1.text;
String timeslot=Label2.Text
SomeDeleteMethod(personidd, delegateName, timeslot);
Panel1.Visible=false;
}
Hope you get the idea..

FormView Data Binding

I have for fields in my datasource that I want to combine and display in one label field. I have added a procedure to capture the databinding action but I don't how to get the data out of the datasource. I am displaying this information on a FormView is that makes anly difference. Can I get an example in c#?
For example -
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
Payout.Text = datasource.field1 + datasource.field2;
}
I'm not fully sure but it seems like you're looking for something like the following:
protected void DisplayPayOut(object sender, EventArgs e)
{
Label Payout = FormView1.FindControl("PayoutLabel") as Label;
object dataItem = DataBinder.GetDataItem(FormView1);
Payout.Text = DataBinder.Eval(dataItem, "field1NameHere").ToString() + DataBinder.Eval(dataItem, "field2Namehere").ToString();
}

Setting SelectedValue of a data bound DropDownList

I have an asp.net dropDownList which is automatically bound to a sqlDataSource to values of client type on page load. On page load I am also creating a Client object, one of it's properties is ClientType. I am trying to set the SelectedValue of the ddl according to the value of the ClientType property of the Client object unsuccessfully. I recieve the following error message "System.ArgumentOutOfRangeException: 'ddlClientType' has a SelectedValue which is invalid because it does not exist in the list of items". I understand that this is because the list has not yet been populated when I'm trying to set the selected value. Is there a way of overcoming this problem? Thank you!
You have to use the DataBound Event, it will be fired, once databinding is complete
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
// You need to set the Selected value here...
}
If you really want to see the value in the Page load event, then call the DataBind() method before setting the value...
protected void Page_Load(object sender, EventArgs e)
{
DropdownList1.DataBind();
DropdownList1.SelectedValue = "Value";
}
Before setting a selected value check whether item is in list and than select it by index
<asp:DropDownList id="dropDownList"
AutoPostBack="True"
OnDataBound="OnListDataBound"
runat="server />
protected void OnListDataBound(object sender, EventArgs e)
{
int itemIndex = dropDownList.Items.IndexOf(itemToSelect);
if (itemIndex >= 0)
{
dropDownList.SelectedItemIndex = itemIndex;
}
}
EDIT: Added...
If you are doing binding stuff in Page Load, try to follow this way:
Move all binding related code in overriden DataBind() method
In Page_Load of Page add: (in case of control do not call DataBind directrly, this is a responsibility of a parent page)
if (!IsPostBack)
{
Page.DataBind(); // only for pages
}

Categories

Resources