How to insert a Root Node in Tererik RadTreeView? - asp.net

I have a RadTreeView telerik control which is working great for me with Drag/Drop, Add, Rename and Delete functionalities. But I want to insert a root node which cannot be updated/rename and deleted.

Try this to add a root node in treeview.
protected void Page_Load(object sender, EventArgs e)
{
tree1.DataBind();
RadTreeNode root = new RadTreeNode("Root");
while (tree1.Nodes.Count > 0)
{
root.Nodes.Add(tree1.Nodes[0]);
}
tree1.Nodes.Add(root);
}

This is the answer,
protected void radTreeView_DataBound(object sender, EventArgs e)
{
RadTreeNode node = new RadTreeNode("Root", "-1");
node.AllowEdit = false;
node.Expanded = true;
node.EnableContextMenu = false;
node.PostBack = false;
node.AllowDrag = false;
var nodes = new List<RadTreeNode>();
foreach (RadTreeNode n in radTreeView.Nodes)
{
nodes.Add(n);
}
node.Nodes.AddRange(nodes);
radTreeView.Nodes.Clear();
radTreeView.Nodes.Add(node);
}

Related

how could i collect all the radio button that chosen in submit button?

Here is my Code:
public partial class Play : System.Web.UI.UserControl
{
int surveyId = 153;
protected void Page_Load(object sender, EventArgs e)
{
// surveyId = int.Parse(Request[SurveyContext.SurveyID]);
FillQuestion();
}
void FillQuestion()
{
IList<Eskadenia.Framework.Survey.Entitis.Question> QuestionLst = Eskadenia.Framework.Survey.Data.QuestionManager.GetQuestionBySurveyId(surveyId);
try
{
RepPlay.DataSource = QuestionLst;
RepPlay.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.InnerException);
}
}
protected void RepPlay_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RadioButtonList rblAnswers = ((RadioButtonList)e.Item.FindControl("rblAnswers"));
IList<Eskadenia.Framework.Survey.Entitis.Answer> answerlst;
answerlst = Eskadenia.Framework.Survey.Data.AnswerManager.GetAnswerByQuestionID(((Eskadenia.Framework.Survey.Entitis.Question)(e.Item.DataItem)).ID);
for (int i = 0; i < answerlst.Count; i++)
{
rblAnswers.Items.Add(new ListItem(answerlst[i].Name, answerlst[i].ID.ToString()));
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Redirect("~/Surveys.aspx");
}
}
First: you should databind the repeater only if(!IsPostBack):
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
FillQuestion();
}
Otherwise all changes are lost and events aren't triggered.
According to the core issue how to get all selected ListItems in the repeater, is this helpful:
var allSelectedItems = RepPlay.Items.Cast<RepeaterItem>()
.Select(ri => new{
ItemIndex = ri.ItemIndex,
SelectedItem = ((RadioButtonList) ri.FindControl("rblAnswers"))
.Items.Cast<ListItem>()
.First(li => li.Selected)
});
This presumes that you want the ItemIndex as identifier, if you need the ID you have to tell us where it's stored. You could also use ri.FindControl("HiddenIDControl") to get it.
Since you have commented that you want a dictionary, you could use this query:
Dictionary<int, string> questionAnswers = RepPlay.Items.Cast<RepeaterItem>()
.ToDictionary(ri => ri.ItemIndex, ri => ((RadioButtonList)ri.FindControl("rblAnswers"))
.Items.Cast<ListItem>()
.First(li => li.Selected).Value);
Here is the same without LINQ, you're right, in this case the loop is even simpler:
Dictionary<int, string> questionAnswers = new Dictionary<int, string>();
foreach (RepeaterItem item in RepPlay.Items)
{
string selectedValue = ((RadioButtonList)item.FindControl("rblAnswers")).SelectedValue;
questionAnswers.Add(item.ItemIndex, selectedValue);
}
I have noticed that my LINQ approaches were too complicated anyway, you can always use RadioButtonList.SelectedValue since a RadioButtonList doesn't support multi-selection.

Cant Get Data To Return To My Grid View Using Linq in ASP.NET

Not sure why but i cant get my data to return. It starts out saying there is not data records to display. But when i select the month and year of the information i am looking for the grid view disappears. Any suggestions
NutritionEntities context;
protected void Page_Load(object sender, EventArgs e)
{
context = new NutritionEntities();
if (!IsPostBack)
{
for (int i = DateTime.Now.Year; i > 1988; i--)
{
ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
}
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
you have verify the binding of your control grid, i suggest add only object to the datasource of the grid on the pageLoad when the postback its false like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
}
GridView1.DataBind();
}
the problem is when you add the objects to datasource , when any event refresh the web page like select the control is unbinding, i hope help you,, see you!!

PageIndexChanging Event in search button in asp.net?

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grid();
}
}
protected void btnsearch_Click(object sender, EventArgs e)
{
objRetailPL.ZoneName = ddlzone.SelectedValue;
//IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
//String datetime = txtdate.Text.ToString();
//DateTime dt = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
//objRetailPL.date =dt;
DataTable dtsearch = new DataTable();
dtsearch = objRetailBAL.searchzone(objRetailPL);
GVRate.DataSource = dtsearch;
GVRate.DataBind();
}
protected void GVRate_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GVRate.PageIndex = e.NewPageIndex;
grid();
}
catch (Exception ex)
{
}
}
I have this code for searching records.And for that grid i added page index,but it is not coming properly.When click on search button it is showing more n of records at that time page index is not working. It is calling First grid before clicking search button..How can I change my code please help me.....
Try this ...
private void grid()
{
if(!string.IsNullOrEmpty(yourSearchTextBox.Text)
{
// Then call search method. Make sure you bind the Grid in that method.
}
else
{
// Normally Bind the Grid as you are already doing in this method.
}
}
you have to check whether there is any search Text or not in that grid() method like...

Listview change applies after 2nd postback?

i have a listview and a button out of this list view, on button click i want to add a "insert" row defined in InsertItemTemplate. The problem is when i click the button, this row is added(i know this because when a do any postback aftewards this row really shows), but isnt shown/rendered. So the question is: why this change doesn´t apply on the first postback - button click? here is my code:
EDIT:
Whole Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem ByName = new ListItem("By name", "Name");
ListItem ByPhone = new ListItem("By phone", "Phone");
ListItem ByEmail = new ListItem("By email", "Email");
FilterTypeDDL.Items.Add(ByName);
FilterTypeDDL.Items.Add(ByPhone);
FilterTypeDDL.Items.Add(ByEmail);
FilterTypeDDL.DataBind();
}
}
//protected void ListView_ItemCommand(object sender, ListViewCommandEventArgs e)
//{
//switch (e.CommandName)
//{
//case "EditItem":
// break;
//case "InsertItem":
// if (Page.IsValid)
// {
// string NameTxt = ((TextBox)(ListView.InsertItem.FindControl("NameTextBox"))).Text.Trim();
// string PhoneTxt = ((TextBox)(ListView.InsertItem.FindControl("PhoneTextBox"))).Text.Trim();
// string EmailTxt = ((TextBox)(ListView.InsertItem.FindControl("EmailTextBox"))).Text.Trim();
// DAORestaurant.InsertRestaurant(NameTxt, PhoneTxt, EmailTxt);
// ListView.InsertItemPosition = InsertItemPosition.None;
// ListView.DataSource = DAORestaurant.GetRestaurants();
// ListView.DataBind();
// break;
// }
// break;
//case "CancelCreation":
// ListView.InsertItemPosition = InsertItemPosition.None;
// ListView.DataSource = DAORestaurant.GetRestaurants();
// ListView.DataBind();
// break;
//case "Articles":
// Session["Restaurant"] = e.CommandArgument.ToString();
// Control ArticlesCtrl = LoadControl("~/Controls/Article.ascx");
// ListViewItem Item = (ListViewItem)e.Item;
// Item.FindControl("CtrlPlaceHolder").Controls.Add(ArticlesCtrl);
//}
//}
protected void closeButton_Click(object sender, EventArgs e)
{
}
protected void newArticleButton_Click(object sender, EventArgs e)
{
}
protected void NewRestaurantBtn_Click(object sender, EventArgs e)
{
ListView.InsertItemPosition = InsertItemPosition.LastItem;
//SetDataSource();
//ListView.DataBind();
}
protected void ValidateName(object source, ServerValidateEventArgs args)
{
string NameTxt = ((TextBox)(ListView.InsertItem.FindControl("NameTextBox"))).Text.Trim();
args.IsValid = (NameTxt.Length > 2 && NameTxt.Length < 51);
}
protected void ValidateUniqueness(object source, ServerValidateEventArgs args)
{
string NameTxt = ((TextBox)(ListView.InsertItem.FindControl("NameTextBox"))).Text.Trim();
args.IsValid = DAORestaurant.IsUnique(NameTxt);
}
protected void ValidatePhone(object source, ServerValidateEventArgs args)
{
string PhoneTxt = ((TextBox)(ListView.InsertItem.FindControl("PhoneTextBox"))).Text.Trim();
Regex regex = new Regex(#"^\d{3}\s\d{3}\s\d{3}$");
args.IsValid = regex.IsMatch(PhoneTxt);
}
protected void ValidateEmail(object source, ServerValidateEventArgs args)
{
string EmailTxt = ((TextBox)(ListView.InsertItem.FindControl("EmailTextBox"))).Text.Trim();
Regex regex = new Regex(#"^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$");
args.IsValid = regex.IsMatch(EmailTxt);
}
protected void ShowAllBtn_Click(object sender, EventArgs e)
{
Session["ALL"] = true;
ListView.DataSource = DAORestaurant.GetRestaurants();
ListView.DataBind();
}
protected void FilterBtn_Click(object sender, EventArgs e)
{
string filterType = FilterTypeDDL.SelectedValue;
string substring = StringTB.Text.Trim().ToUpper();
Session["ALL"] = false;
Session["FilterType"] = filterType;
Session["Substring"] = substring;
ListView.DataSource = DAORestaurant.GetRestaurants(substring, filterType);
ListView.DataBind();
}
protected void ListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView.EditIndex = e.NewEditIndex;
//SetDataSource();
//ListView.DataBind();
}
protected void ListView_ItemInserting(object sender, ListViewInsertEventArgs e)
{
}
protected void ListView_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
if (e.CancelMode == ListViewCancelMode.CancelingInsert)
{
ListView.InsertItemPosition = InsertItemPosition.None;
}
else
{
ListView.EditIndex = -1;
}
//SetDataSource();
//ListView.DataBind();
}
private void SetDataSource()
{
if ((bool)Session["ALL"])
{
ListView.DataSource = DAORestaurant.GetRestaurants();
}
else
{
ListView.DataSource = DAORestaurant.GetRestaurants((string)Session["Substring"], (string)Session["FilterType"]);
}
}
The code commented out is what i used before, i´ve switched to what you can see now, but the problem still persists. Only when i uncomment those 2 commented lines in each event, the changes apply instantly, but i know i cannot use such a method that many times, and it should not even be there.
Because of the order of execution. Try setting it in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack &&
!string.IsNullOrEmpty(Request.Form[NewRestaurantBtn.ClientID]))
{
ListView.InsertItemPosition = InsertItemPosition.LastItem;
}
}

Gridview showing up deleted datas

I just display my uploaded file details in my GridView, so there will be only one row in my GridView - multiple files are not allowed.
When I delete that single row and try to upload a new file, it is showing 2 rows (the new file and the deleted file).
I already tried using GridView.DataSource = null and GridView.DataBind().
Note: I've rebinded my GridView after the delete, but it still shows the deleted file.
protected void DeleteLinkButton_Click(object sender, EventArgs e)
{
if (Session["name"] != null)
{
string strPath = Session["filepath"].ToString();
System.IO.File.Delete(strPath);
GridView2.Rows[0].Visible = false;
Label8.Text = "";
Session["filename"] = null;
Button3.Enabled = true;
}
GridView2.DataBind();
}
In some cases I've had to do something like this:
//page level variable
bool refreshRequired = false;
protected void DeleteLinkButton_Click(object sender, EventArgs e)
{
if (Session["name"] != null)
{
string strPath = Session["filepath"].ToString();
System.IO.File.Delete(strPath);
GridView2.Rows[0].Visible = false;
Label8.Text = "";
Session["filename"] = null;
Button3.Enabled = true;
refreshRequired = true;
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
if(refreshRequired)
{
//whatever you to to set your grids dataset, do it here
//but be sure to get the NEW data
}
}
At the time of Delete, your grid is bound to the old data. When you change the data, you must rebind it to the new.

Resources