I've couple of LinkButtons for which I want to manipulate the cssclass property.
Earlier I used each LinkButton individually to change the cssclass and it worked perfectly upon postback. E.g. lnkbtnHome.CssClass = "tab";
But over time the items increased so I thought it could be better way to do it and I decided to use the Listcollection and use foreach loop to do the same.
Below is the code that I'm using currently.
By default (upon page load) the first button is having a different class tabsel and I want to reset the class of all buttons by setting the class to tab. Upon debugging I can see the cssclass getting modified but it's not changing up in the browser.
Am I missing something?
Under Declaration:
static List<LinkButton> lnklist;
Under PageLoad:
lnklist = new List<LinkButton>();
lnklist.Add(lnkbtnHome);
lnklist.Add(lnkbtnSubject);
lnklist.Add(lnkbtnReport);
Upon Postback:
foreach (var lnkbtn in lnklist){
lnkbtn.CssClass = "tab";
}
Never use static fields in ASP.NET!
You:
I used static to avoid losing the items upon postback.
but that's the nature of HTTP. It's a stateless protocol. You should recreate all controls on each postback in the same way as ASP.NET does it. Otherwise you're are vulnerable to various issues since ASP.NET is a multithreading environment. You could store it in the Session, but i would advise against it. LinkButton is a webcontrol which needs to be part of the current page's control collection. This page will be destroyed at the end of it's lifecycle.
I've created a Table User control. In each cell in table, there are checkboxes. How can I access the attributes of selected checkboxes in the default.aspx page.
I've dragged Table user control into default.aspx
<uc1:SchTable ID="SchTime1" runat="server" />
Am relatively new to User Control. Was trying it out because of maintainability.
I managed to get the codes to work by hard coding the table (not using user control) on the same page as default.aspx though
Add a property to the UserControl that accesses and returns the data you want.
In SchTable you can add any number of public properties and methods you want. Some examples:
public IEnumerable<ListItem> SelectedItems
{
get
{
return ACheckboxList.Items.Cast<ListItem>().Where(i => i.Selected);
}
}
public IEnumerable<Checkbox> GetAllCheckboxes()
{
//Find and return the checkboxes here just like you would in the page
}
And then in the Default page, you can access that information:
var selected = SchTime1.SelectedItems;
var checkboxes = SchTime1.GetAllCheckboxes();
There is a MSDN tutorial here that goes more into details on all this.
I have stumbled across a problem with my asp.net form.
Within my form the end user chooses a number of textboxes to be dynamically created, this all works fine with the following code:
protected void txtAmountSubmit_Click(object sender, EventArgs e)
{
int amountOfTasks;
int.TryParse(txtAmountOfTasks.Text, out amountOfTasks);
for (int i = 0; i < amountOfTasks; i++)
{
TextBox txtAddItem = new TextBox();
txtAddItem.ID = "txtAddItem" + i;
txtAddItem.TextMode = TextBoxMode.MultiLine;
questionNine.Controls.Add(txtAddItem);
txtList.Add(txtAddItem.ID);
}
}
However this has also caused a small problem for me, later on in my form on the submit button click, I send the results to the specified person it needs to go to (using smtp email). Again this part is fine, until I am trying to retrieve the text from these dynamically created textboxes.
What I have tried
I have tried using this msdn access server controls ID method however this was not working.
I tried to add these new textboxes to a list, however I was unsure on how to update these textboxes when they have text in them. Therefore my results were returning null because of this.
I have also looked at other questions on SO such as this however they are usually for WPF or winforms, rather than my problem with asp.net (this usually isn't an issue, but I don't need to get the text from every textbox control in my page, just the ones that were dynamically created).
I have also tried changing how I call the code that I hoped would have worked:
string textboxesText = string.Join("\n", txtList.Select(x => x).ToArray());
and then in my concatenated string (email body) I would call:
textboxesText
The problem
As they are dynamically created I am finding it difficult to call them by their id for example: txtExampleID.Text, also as I have to increment the ID's by one each time (so they don't override each other) it has made things a little bit more difficult for me.
I am not asking for a code solution, I would prefer pointers in the right direction as I am still learning.
So to sum it all up: I need to get the text from my dynamically created textboxes to add it to my email body.
The issue is these text boxes need recreated in the Load event of the page, every single time, so that both events and values can be hooked back up and retrieved.
I think the most straight forward approach, in your case, would be to extend idea #1 that you had already tried. Build a List of these controls with enough information to recreate them in Load, but you need to store that List in either ViewState or Session.
ViewState["DynamicControls"] = list;
or
Session["DynamicControls"] = list;
I would use ViewState if you can because it gets destroyed when the user leaves the page.
I use RadTreeView in my master page :
but i face the following problem :
I lose my selections when i click on the nodes of the tree view .and every time i click on the node it makes (Response.Redirect("...")) which make the master entering the (!Page.IsPostback)
every time and bind the tree view again so i lose my selections .
How to fix this problem .
My .aspx :
<telerik:RadTreeView ID="rtv_cm" runat="server" OnNodeExpand="rtv_cm_NodeExpand"
OnNodeClick="rtv_cm_NodeClick" Skin="WebBlue">
</telerik:RadTreeView>
My .cs :
protected void Page_Load(object sender, EventArgs e)
{
if (Session["emp_num"] != null && !string.IsNullOrEmpty(Session["emp_num"].ToString()))
{
if (!Page.IsPostBack)
{
LoadRootNodes(rtv_cm, TreeNodeExpandMode.ServerSide);
}
}
else
{
Response.Redirect("Frm_login.aspx");
}
}
protected void rtv_cm_NodeClick(object sender, RadTreeNodeEventArgs e)
{
dt_childs = (DataTable)Session["dt_childs"];
IEnumerable<DataRow> result = from myRow in dt_childs.AsEnumerable()
where myRow.Field<string>("task_id").TrimEnd() == e.Node.Value.TrimEnd()
select myRow;
if (result != null)
{
if (!string.IsNullOrEmpty(result.ElementAtOrDefault(0).Field<string>("page_name")))
{
Response.Redirect(result.ElementAtOrDefault(0).Field<string>("page_name").TrimEnd(), false);
}
}
}
How I get The menu :
private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
{
dt = Menu_db.GetMenu(Session["emp_num"].ToString(), CMSession.Current.db_code);
IEnumerable<DataRow> result = from myRow in dt.AsEnumerable()
where myRow.Field<string>("task_id").TrimEnd() == "0"
select myRow;
if (result != null && result.Count()>0)
{
DataTable dt_roots = result.CopyToDataTable<DataRow>();
Session["dt"] = dt;
Session["dt_roots"] = dt_roots;
foreach (DataRow row in dt_roots.Rows)
{
RadTreeNode node = new RadTreeNode();
node.Text = row["task_name"].ToString().TrimEnd();
node.Value = row["task_id"].ToString().TrimEnd();
if (Convert.ToInt32(row["count"]) > 0)
{
node.ExpandMode = expandMode;
}
treeView.Nodes.Add(node);
}
}
}
HTTP is what's known as a "stateless protocol". Basically, it's as if the server has a severe case of Alzheimer's disease (no disrespect intended). The server answers your query, but then forgets you were ever there.
When HTTP was used primarily for fetching static documents, this posed no problem, but web applications require some kind of state. There are many ways this is accomplished, but ASP.NET typically makes heavy use of the "ViewState". The view state is simply an <input type="hidden"> tag which contains base-64 formatted byte code. (If you view the source of your rendered HTML page in the browser, you see it - named "__VIEWSTATE"). When the user resubmits the form (by clicking a button, etc), the viewstate data is sent back to server. Basically, it's like reminding the server about what it told you last time. This is how TextBoxes and GridViews are able to seemingly maintain their state between postbacks - they store their state in the "viewstate".
The problem with the viewstate, though, is that it can be easily lost. You must submit the form or the viewstate will not be persisted. If you use an anchor tag or Request.Redirect, you are side-stepping the form and hitting a web page all on your own and, in the process, you are not passing any of the viewstate along.
There is often no way to avoid this, so there are other ways to store your application's state. But, unlike the viewstate, this is something you must do manually for each and every value that you want to persist. You can store the data in a cookie, you can store the data on the server (using Server["key"] or Session["key"]) or you can persist the data in something more concrete, like a database or a text file. All of this must be done manually, however, and then you must reload the data when you reload the page.
In your case, you may want to guess which item was selected based on the current page (since the treeview items and pages seem to be linked) and set the selected item explicitly. If that's not feasible, you could try storing the selected item in the Session, using something like Session["Master_SelectedTreeViewItem"] = treeViewItem.Id;. Then, in Page_Load, get this value (careful, it may be null) and then set the corresponding treeview item as selected.
I would post more code examples, but you haven't provided the code where you are loading the treeview, etc, so it would be difficult to do.
Further Info
ASP.NET State Management Overview (MSDN)
The only way i can think of is storing the selection in session and on (!Page.IsPostBack), look for that Session key and update your selection after the binding.
The problem you face is because essentially, trough your Redirect, you go to a different html page, so ViewState is out the window, nevermind that it is the same address.
If you are binding the tree with urls that link to other page, append the selection using query string, then load the tree selection from the query string from the other page.
If you think that the second page you are redirecting to has same UI as the first, dont redirect instead load it on the same page with the new content for the selection, this will allow you to maintain the state in asp.net if the control supports view state.
I already had this problem, exactly same problem, needs to redirect on TreeViewItemClick.
I solve it storing the last selected item is session, and selection it again on page load.
Somethink like this
protected void rtv_cm_NodeClick(object sender, RadTreeNodeEventArgs e)
{
Session["LastClickedNode"] = e.Node.Value;
...
}
and in your Load method, verifi ir the node needs to be selected, like this.
private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
{
//... your code
if (Session["LastClickedNode"] !=null)
{
if (row["task_id"].ToString().TrimEnd() == Session["LastClickedNode"].ToString())
{
node.Selected = true;
}
}
}
i think this will solve your problem.
As the title says...
If I start my app project with a pivot page(MainPage.xaml) and then choose to click for example the "design two" link in the databinded listbox. Is it possible to bind the "LineThree" text for the "design two" link in to a separate portrait page?
Do I have to make new portrait page for every "LineThree"-link? Or can I just generate the "MainViewModelSampleData.xaml" data to a single portrait page depending on what "LineOne"-link I click in the pivot page in the start?
Hope my question is understandable... :P
If I understand you correctly, you want to have a main page that contains a list of data, and then a details page whose contents are dependent on the item that you clicked in the main page. The answer to your question is then "yes". There are a number of ways to achieve this, some of which include global variables, a custom navigation service, storing a value in isolated storage and so on. My personal preference is to use the context of the NavigationService and to pass an ID or an index in the query string for the target page.
Your call to navigate to the details page then looks like this:
Application.Current.Navigate(string.Format("/Views/DetailsView.xaml?id={0}", id));
In the target page, you override the OnNavigatedTo handler to retrieve the value that you passed and then process it accordingly (i.e. look up the value from your database, or retrieve it from a data collection).
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (this.NavigationContext.QueryString.ContainsKey("id"))
{
var id = this.NavigationContext.QueryString["id"];
// TODO: Do what you need to with the ID.
}
else
{
// I use this condition to handle creating new items.
}
}
How is what you're trying to do different from what is created by default in a new DataBound Application? That lets you select an item in the list on the main page and then displays another page which includes the text from LineThree.
I suggest you look at the sample code created as part of a new DataBound Application.