(Re)Creating Treeview by selecting a GridViewRow on the Masterpage - c#

So, I have a GridView on my MasterPage with an onclick Event which returns the ID (there are device components in this grid and I need their ID from the database) from that row.
The Onclick event which is bound in the RowDataBound event
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.selectionGridView, "Select$" + e.Row.RowIndex);
When I click a row the SelectedIndexChanged Event gets fired
protected void selectionGridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridView selectedGridView = sender as GridView;
//Saves the Database ID of the selected data
Session["selectedDevID"] = selectedGridView.SelectedRow.Cells[0].Text;
_Auswahl.FillTree();
}
And this piece of code calls a method from the page I want to show a Treeview with more stuff etc.
_Auswahl.FillTree();
Everthing is fine, all methods are called correctly, but on the _Auswahl Page, I can't add things to the TreeView (or to the Page at all). All my controls return a NullReferenceExeption, and if I initialize them, then this stuff won't show up
I tried everything, I guess I miss some little thing or forgot something, but I can't get this thing right ._.
Edit: I don't want to Redirect to that Page again, I want to "refresh" the treeview (which is located in an UpdatePanel)
Edit2:
This is my ___FillTree()_ Methode (Excluded the whole Linq stuff etc etc und left just some basic stuff)
public void FillTree()
{
//Add child node value
TreeNode child = new TreeNode("Manager");
//Add parent value
TreeNode parent = new TreeNode("Development");
//Add child to parent
parent .ChildNodes.Add(child);
//Add parent to tree
TreeView1 = new TreeView();
TreeView1.Nodes.Add(parent);
TreeView1.DataBind();
TreeView1.ExpandAll();
}

Related

How to get selection in DropDownList control in parent control Page_Load method?

I am relatively new to asp.net.
I have parent custom user control and child custom user control.
As you can see above child control has DropDownList control.
When selection changed in DropDownList control postback accrued and Page_Load(object sender, EventArgs e) method of the parent control is fired,
at this stage(in Page_Load method of the parent control) I need to get the selected value in DropDownList.
Any idea how can I get the selected value in DropDownList in Page_Load method of the parent control?
One option would be to expose either the control on your child control, or an accessor to the value of the same control. For example, in your code behind for the child control you could have a property like
public TextBox MyTextBoxControl
{
get { return MyLocalTextBoxControl; }
}
And then access it on the Page_Load of the main control like so:
protected void Page_Load(object sender, EventArgs e)
{
...
var textValue = MyChildControl.MyTextBoxControl.Text;
...
}
Of course, you will need to decide if it makes better sense from a reusability standpoint whether to expose just the text portion of the control (or whatever property you need at the parent level) or access to the whole control.
For reference, you would expose access only to the text portion of the sub-sub control as follows.
public string MyTextBoxControlText
{
get { return MyLocalTextBoxControl.Text; }
set { MyLocalTextBoxControl.Text = value; }
}

Lost GridView SelectIndexChanged at UserControl

I have a UserControl which contain GridView.I set AutoGenerateSelectButton is true for this GridView.
But it didn't work when I press Select inside UserControl.
Do you have anyidea?
You should move your event handling to the page that owns the UserControl and not in the UserControl
Inside the Page_Load of your page, add this
myUserControl.FindControl("GridView1"));
dvGrid.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
Add the handler to the page
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//access the GridView
GridView grid = (GridView) sender;
//access the selected row
GridViewRow selectedRow = grid.SelectedRow;
//access the selected Primary key - make sure you set the DataKeyNames property of the GridView to the Record Id - in your Markup
string currentRowPrimaryKey = grid.SelectedValue;
//OR
string currentRowPrimaryKey = grid.SelectedDataKey.Value;
}
Now you have several values to play with. You can put a break point and examine the properties of the sender to have more options. Good Luck

attach a panel to TreeView control

I am beginner in c#. In my project, I populated a xml file inside a TreeView control. If the xml file is large, the TreeView control is showing the data with scroll bars. Beside this, whenever the user double clicks a node I am showing a panel beside the selected node something like this..
When I scroll the TreeView Control :
My question is how to make the panel attached to treeView control so that eventhough the user scrolls the TreeView control the panel should also move along with the selected node.
Well, hard to do since TreeView doesn't have a Scroll event. It isn't reliable anyway since nodes can be expanded and collapsed, changing the position and visibility of the node. The backup plan is to use a Timer. This worked well:
private void timer1_Tick(object sender, EventArgs e) {
var node = treeView1.SelectedNode;
if (node == null || !node.IsVisible) panel1.Visible = false;
else {
panel1.Visible = true;
var nodepos = treeView1.PointToScreen(node.Bounds.Location);
var panelpos = panel1.Parent.PointToClient(nodepos);
panel1.Top = panelpos.Y;
}
}

TreeNode SelectNodeChanged Event c#

Problem:
I have 5 nodes in Treeview list including child nodes. Whenever I select one node it should display the name of the node in the combo box automatically without the need of pressing a button.
When I use ONselectednodechanged event it only fires once. So when I click on the node for the first time it fires the event but it does not do anything after that.
Could someone please give me some suggestions on what to do. I want it to automatically display the name of the node in the combo box when I click on it.
c#, ASP.net
Here is what I wrote so far:
protected void nav_tree_items_SelectedNodeChanged(object sender, EventArgs e)
{
if (nav_view_add.Visible == true)
{
panel_helper.GroupingText = "Add";
nav_multi.SetActiveView(nav_view_add); //set active view
nav_btn_save_add.CausesValidation = true;
DataRow[] row1 = TableClass.MainTable.Select("ItemId = " + nav_tree_items.SelectedValue);
nav_dd_parent.SelectedValue = row1[0]["ItemId"].ToString().Trim();
}
When the program is executed it does not even make the panel visible.
<asp:TreeView ID="nav_tree_items" runat="server" Height="100%" ShowLines="True"
Width="123%" onselectednodechanged="nav_tree_items_SelectedNodeChanged">
<HoverNodeStyle CssClass="hoverTreeItem" />
When I work with TreeView, I use the "AfterSelect" event. The stub can be easily generated by double clicking the TreeView control in [Design] mode. You can then do something like the following, using the data however you wish (MessageBox is just an example).
private void naviTree_AfterSelect(object sender, TreeViewEventArgs e)
{
MessageBox.Show("The node you just selected is [" + e.Node.ToString() + "]");
}
Hope this helps at all.

ASP.Net: GridView control and combobox woes

I've got a GridView control and Combobox control that are both successfully populated in my Page_Load event (within a block that checks for IsPostBack == false).
I've got an empty button 'btnClick' event handler which will reload the page when clicked. Both the GridView and Combobox controls have their EnableViewState property set to True. The behaviour I was expecting and hoping for was:
Page will reload with the GridView control still populated.
Page will reload with Combobox still populated and the item selected by the user still set as the selected item.
Unfortunately, the behaviour I'm getting is as follows:
GridView control is now empty and shows no data.
Combobox is now empty.
Code as follows:
public MyPage()
{
this.Load += new EventHandler(Page_Load);
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
DataAccessObj daObj = new DataAccessObj();
foreach (DataRow dataRow in daObj.GetAllData())
{
ListItem listItem = new ListItem(dataRow.ToString(), dataRow.Id.ToString());
myCombobox.Items.Add(listItem);
}
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Do nothing
}
What I would like to do is allow the user to select an item from the Combobox. Upon clicking Submit, the GridView would be repopulated (based upon the selected item). The Combobox will remain populated and show the last selected item.
Can someone help explain where I might be going wrong? TIA
When you click your button, the page is posted back, in your page load, if it is a postback you need to databind the grid appropriately you need to add a condition to your page load event like
Firstly on your btn_click, you need to store the id selected with something like:
if (myCombobox.SelectedItem != null)
{
if (int.TryParse(myCombobox.SelectedItem.Value, out reportedById) == false)
{
reportedById = 0;
ViewState["reportedById"] = reportedById; // Need to remember which one was selected
}
}
Then On your Post Back
else (IsPostBack)
{
if (ViewState["reportedById"]) != null)
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(Convert.ToInt32(ViewState["reportedById"]));
IncidentGrid.DataBind();
myCombobox.SelectedItem.Value = ViewState["reportedById"].ToString(); // set combo
}
else
{
IncidentGrid.DataSource = daObj.GetIncidentsByReportedById(0);
IncidentGrid.DataBind();
}
}

Categories