Category Archives: CheckBox

Treeview - SelectedNodeChanged event never fired

Hi

I have a Treeview control on my master page, the treeview is bound to a sitemap.

<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" NodeIndent="10" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" ShowCheckBoxes="All" AutoGenerateDataBindings="False">
   <DataBindings
>
      <asp:TreeNodeBinding DataMember="SiteMapNode" NavigateUrl="~/Default.aspx" NavigateUrlField="Url" Text="Title" TextField="Title" ToolTipField="Description" Value="Title"
/>
   </DataBindings
>
</asp:TreeView
>

But when I select a node in the view, the SelectedNodeChanged event isn't fired. The site just navigates to the new page.
ViewState is enabled.

Thanks for your help in advance.

Hi,
  The TreeView can do either 1 of 2 things.  Navigate or postback.  If it navigates, then you simply load the next page regardless of what page it is (the same page or different).  If it posts back, then it fires events on the same page.  Since you have navigate urls, that implies you are navigating and you won't get postback events.
--
Danny

Hi Danny

Thanks for your reply. I've reached the same conclusion, after several of hours debugging my app and discusses with colleges :)
Shame we don't live in the same timezone, you could have safed me a lot of wasted time.
Thanks again

///Rune 

I've searched numerous pages about this issue, and this post seems to sum-up the consensus.

Surely a TreeView and web.sitemap is one of the most common approaches to Navigation in ASP.NET 2005 (notice that it's on the "Navigation" tab in the toolbar). So why are navigating and coding (postback) mutually exclusive?

I used to get around this oddity of not having a SelectedNodeChange event (before the RTM) using the Page.SaveStateComplete event like this:

If masterTreeView.SelectedValue <> "" Then
   masterTreeView.SelectedNode.Expand()
End If

And it worked okay; but now even that won't work. It's like MS is going out of their way to make things more difficult. All I want is the exact same functionality as the +/- signs, but without seeing the symbols (our designers decision, not mine), but you're saying this is impossible now?

Well, after a few hours and way more code than I would have liked, I have the previous functionality back again. I'm basically doing all the expand/collapse functionality on my own in the SaveStateComplete. If you don't want to use the +/- symbols, you're in trouble.

yekcim1125

Surely a TreeView and web.sitemap is one of the most common approaches to Navigation in ASP.NET 2005 (notice that it's on the "Navigation" tab in the toolbar). So why are navigating and coding (postback) mutually exclusive?

Here's an example that might help explain this.  Lets say you have a TreeView on a page at:  http://www.msn.com/testpage.aspx  And lets say you have another (or even an identically coded) TreeView on a page at:  http://www.microsoft.com/somepage.aspx.  Lets say the first TreeView has a node with a NavigateUrl=http://www.microsoft.com/somepage.aspx.  If you are viewing the msn page and you click on the link to the microsoft.com page, would you expect that a SelectedNodeChanged event fires on the msn site?  the microsoft site?  neither? 

I think the obvious answer is neither.  There isn't any difference between this scenario and navigating to another page on the same site except that this is more clear (I think).  That is why navigation and postbacks are mutually exclusive.  Submitting a form and navigating to a new url are separate actions. 

If you think it should act a different way, I'd be happy to listen to your feedback.  Thanks.
--
Danny 

 

Is there a way to make the treeview generate events knowingly instead of on postback?

Basically I want to use the treeview the way you do when you install Visual Studio 2005 (tri-state checkbox). I think the treeview lacks some flexibility. Is there a way to enable event firing programmatically.

I am using the TreeView in ASP .Net 2.0

I would like to be able to browse a document via the NavigateUrl on a treenode in a separate frame to the treeview, as specified by the Target="blah" on the node. I would then like to be able to indicate the currently selected node, which should be the node that was navigated/selected.

At the moment I can only indicate the parent of the node. When there are many children, I cannot indicated the currently navigated/selected node

What I don't understand is; Why can't the Select events fire if the target is specifying a different location(frame)  to the treeview (frame)?

Without this feature it seems impossible to indicate to the user which document is the currently navigated/selected node.

Regards

Mike

first, same level nodes must have different values to distinguish selected node. Else treeview shows parent node selected.

second, if you use navigate url treeview works in navigate mode and disables selection events. So you cannot determine the selected tree node.

To solve this you can try to populate tree node texts with javascript that populates some hidden field values at the target frame...

Extra information about treenode class : http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.treenode.aspx

Ozan K. BAYRAM

Thanks so much for this post.  I spent several hours fighting this issue and was pretty close to giving up.  A real world example that worked!

 

 

 

Can you answer a simple question?  What event do I need to register to handle this?

protected

virtual void SMI_CommandTest(Object sender, TreeNodeEventArgs e)

 

[RESOLVED] Populating a dynamically generated checklist control in code-behind not working

<div style="margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); " mce_style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; ">

(The following is a complete re-edit of the original post which was rambling, confusing etc...)

I have a form with a dynamically generated checklist:

<asp:CheckBoxList ID="cblGames" runat="server" DataSourceID="sqlGames" DataTextField="Game" DataValueField="Id" </asp:CheckBoxList>

 <asp:SqlDataSource ID="sqlGames" runat="server" ConnectionString="<%$ ConnectionStrings:Games %>" 

SelectCommand="SELECT [Id], [Game], [ShowWaitingList] FROM [Aanbod] WHERE ([ShowWaitingList] = @ShowWaitingList)">

 <SelectParameters><asp:Parameter DefaultValue="true" Name="ShowWaitingList" Type="Boolean" /></SelectParameters>

</asp:SqlDataSource>

The following code-behind is supposed to check those boxes that correspond to values stored in a database. It fails and I don't know why. Any ideas?

regards, Guy (p.s. [AanbodId] and [Id] are equivalent.)


if    (!Page.IsPostBack && Request.QueryString["id"] != null)
        {
               // Code that gets the data goes here. Removed for brevity's sake //
              ListItem currentCheckBox = cblGames.Items.FindByValue(reader["AanbodId"].ToString());
              if (currentCheckBox != null)
              {
                  currentCheckBox.Selected = true;
              }
             

            }
        }


</div>


First of all, here's a tip using the Insert Code tool:  Once you've inserted a code block, don't edit it.  That's probably how you ended up with all those spurious <SPAN> tags in your C#.  (No disrespect intended - just something I had to learn the hard way.)

Secondly, .aspx pages by default post back to themselves, so you shouln't need to redirect a page back to itself.  If you use Response.Redirect to do that, you'll loose all the information saved in viewstate, which may or may not explain why you're having this problem.

HI, (this post can be ignored, see re-edit above)


I tried to edit the code down because it's a little convoluted (I'm a newbie) and I hoped just presenting the key  would make it easier to understand the problem. I'm sure those span tags didn't show in the preview!


Anyway, the page only redirects after a new record is inserted. This insures that the user is presented with a blank form to enter the next record. I see the usefulness of viewstate for many tasks, but think it less than useful for database inserts which most of the time involve moving onto the next record. I considered resetting all entry fields to blanks in the code behind, but a redirect is simpler (if not  exactly elegant).

So I am still stuck with the original problem. Is it clear what I want to be done and what the problem is?

Both scenarios/solutions I've tried take place with viewstate (not that it is used). Neither give me the result I want.


regards, Guy

Are you running the above code in the CheckBoxList.DataBound handler or where?  (It won't work before that event.)

(And you're right about preview mode; it's useless.  You just have to learn the hard way, like all of us, that the code block starts and ends with the two blank lines above and below the code, and that once you click Insert, the only safe way to edit it is in HTML mode.  And even in HTML mode, you can't put HTML tags between the <PRE> and </PRE> tags.)

[RESOLVED] Avoid loding data when user unselect

I have checkbox control in Gridview when i selected checkbox entire row will be loaded into corresponding Textboxes, up to here every thing fine when i unchecked again entire row is populating/loading into corresponding Textboxes i dont want to be happen this when i unchecked data should not be loaded.

Anothere one is if i checked "A" record earlier, simulataniously when i checked "B" record "A" record should be un checked automatically but for me it is not happening both records are checked.

how can i acheive above to please help,Thanks

Gridview with checkbox

<asp:GridView ID="GVStatus" runat="server" CellPadding="3" ForeColor="#333333"
            GridLines="Vertical" Height="16px" Width="180px"  AutoGenerateColumns="false"
            EnableModelValidation="True" style="margin-top: 0px" Font-Size="Medium"
                   ToolTip="List of Jobs  from the Database">
             <RowStyle BackColor="#EFF3FB" />
             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
             <HeaderStyle  BackColor="#507CD1"  Width="50px" Font-Bold="True" ForeColor="White" />
             <EditRowStyle BackColor="#2461BF" />
             <AlternatingRowStyle BackColor="White" />
             <Columns>
                 <asp:TemplateField>
                     <ItemTemplate>
                         <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" QuotesKey='<%#Eval("WONumber").ToString() %>' OnCheckedChanged="chkSelect_CheckedChanged"  />
                     </ItemTemplate>
                 </asp:TemplateField>
 </Columns>


 

Hi,

 

Show us your  chkSelect_CheckedChanged method.


Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim indx As Integer
        Dim connectionString As String = "Database=pjt;" & "Data Source=localhost;" & "User Id=root;Password=prasad123"
        Dim query As String = "Select WONumber,PartNumber,Rev,customer,CRep,AEng,Orderqty,PReceived,FabDue,FabComp,AssyDue,AssyComp,TestDue,TestComp,Notes,Shippedqty,status from status"
        'Dim query As String = "Select partnumber from status"
        Dim connection As New MySqlConnection(connectionString)
        Dim da As New MySqlDataAdapter(query, connection)
        Dim ds As New DataSet()        Dim myAdapter As New MySqlDataAdapter
        myAdapter = New MySqlDataAdapter(query, connection)
        myAdapter.Fill(ds, "status")
        Dim dt As DataTable = New DataTable() 'DataTable Initializes a new instance of the DataTable class with no arguments.
        da.Fill(dt)        Dim checkbox As CheckBox = CType(sender, CheckBox)
        Dim row As GridViewRow = CType(checkbox.NamingContainer, GridViewRow)
        Dim i As Integer        For i = 0 To dt.Rows.Count - 1 Step i + 1
            If (dt.Rows(i)("WONumber").ToString = row.Cells(1).Text.ToString()) Then
                indx = i            End If        Next
        txtWON.Text = dt.Rows(indx)("WONumber").ToString()
        txtPartNumber.Text = dt.Rows(indx)("PartNumber").ToString()
        txtRev.Text = dt.Rows(indx)("Rev").ToString()
        txtCust.Text = dt.Rows(indx)("Customer").ToString()
        DDCR.Text = dt.Rows(indx)("CRep").ToString()
        DDAE.Text = dt.Rows(indx)("AEng").ToString()
        txtOQty.Text = dt.Rows(indx)("OrderQty").ToString()
        DDPR.Text = dt.Rows(indx)("PReceived").ToString
        txtFDD.Text = dt.Rows(indx)("FabDue").ToString()
        txtFCD.Text = dt.Rows(indx)("FabComp").ToString()
        txtADD.Text = dt.Rows(indx)("AssyDue").ToString
        txtACD.Text = dt.Rows(indx)("AssyComp").ToString()
        txtTDD.Text = dt.Rows(indx)("TestDue").ToString()
        txtTCD.Text = dt.Rows(indx)("TestComp").ToString()
        txtDes.Text = dt.Rows(indx)("Notes").ToString()
        txtShipQty.Text = dt.Rows(indx)("ShippedQty").ToString()
        DDStatus.Text = dt.Rows(indx)("Status").ToString()    End Sub

Hi, Give your chkSelect_CheckedChanged event handler as below.

 

1. First you clear your textboxes.

2. In the loop, you uncheck previous checkboxes

3. Finally if you have checked any checkbox, then that line will have its value filled in the textboxes.

Just make sure that you are using the same query for filling the datatable and also for binding the gridview.

Hope this helps.

 

 

Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        txtWON.Text = ""        txtPartNumber.Text = ""        txtRev.Text = ""
        txtCust.Text = ""        DDCR.Text = ""        DDAE.Text = ""
        txtOQty.Text = ""        DDPR.Text = ""        txtFDD.Text = ""
        txtFCD.Text = ""        txtADD.Text = ""        txtACD.Text = ""
        txtTDD.Text = ""        txtTCD.Text = ""        txtDes.Text = ""
        txtShipQty.Text = ""        DDStatus.Text = ""
        Dim indx As Integer
        Dim connectionString As String = "Database=pjt;" & "Data Source=localhost;" & "User Id=root;Password=prasad123"
        Dim query As String = "Select WONumber,PartNumber,Rev,customer,CRep,AEng,Orderqty,PReceived,FabDue,FabComp,AssyDue,AssyComp,TestDue,TestComp,Notes,Shippedqty,status from status"
        'Dim query As String = "Select partnumber from status"
        Dim connection As New MySqlConnection(connectionString)
        Dim da As New MySqlDataAdapter(query, connection)
        Dim ds As New DataSet()        Dim myAdapter As New MySqlDataAdapter
        myAdapter = New MySqlDataAdapter(query, connection)
        myAdapter.Fill(ds, "status")
        Dim dt As DataTable = New DataTable() 'DataTable Initializes a new instance of the DataTable class with no arguments.
        da.Fill(dt)        Dim checkbox As CheckBox = CType(sender, CheckBox)
        Dim row As GridViewRow = CType(checkbox.NamingContainer, GridViewRow)
        Dim i As Integer        For i = 0 To dt.Rows.Count - 1 Step i + 1
            If (dt.Rows(i)("WONumber").ToString = row.Cells(1).Text.ToString()) Then
                indx = i            Else
                Dim loopCheckBox As CheckBox = CType(GVStatus.Rows(i).Cells(0).Controls(1), CheckBox)
                loopCheckBox.Checked = False            End If        Next
        If (checkbox.Checked) Then
            txtWON.Text = dt.Rows(indx)("WONumber").ToString()
            txtPartNumber.Text = dt.Rows(indx)("PartNumber").ToString()
            txtRev.Text = dt.Rows(indx)("Rev").ToString()
            txtCust.Text = dt.Rows(indx)("Customer").ToString()
            DDCR.Text = dt.Rows(indx)("CRep").ToString()
            DDAE.Text = dt.Rows(indx)("AEng").ToString()
            txtOQty.Text = dt.Rows(indx)("OrderQty").ToString()
            DDPR.Text = dt.Rows(indx)("PReceived").ToString
            txtFDD.Text = dt.Rows(indx)("FabDue").ToString()
            txtFCD.Text = dt.Rows(indx)("FabComp").ToString()
            txtADD.Text = dt.Rows(indx)("AssyDue").ToString
            txtACD.Text = dt.Rows(indx)("AssyComp").ToString()
            txtTDD.Text = dt.Rows(indx)("TestDue").ToString()
            txtTCD.Text = dt.Rows(indx)("TestComp").ToString()
            txtDes.Text = dt.Rows(indx)("Notes").ToString()
            txtShipQty.Text = dt.Rows(indx)("ShippedQty").ToString()
            DDStatus.Text = dt.Rows(indx)("Status").ToString()        End If
    End Sub

 

Wow pretty precise solution Lateef,Thank you so much,Have a Great day.

[RESOLVED] call the code begind method in a aspx page

hello,

       how can we call the code begind method in a aspx page?

for ex:

code behind:

==========

public bool sample()
        {
            return true;
        }

 

aspx:

===========

<asp:CheckBox ID="chkPFCeilingFlag" runat="server" TabIndex="15" Checked='<%# sample() %>'
Enabled="true" CssClass="CheckBox"></asp:CheckBox>

but it is not called.

 

hi,

 this.chkPFCeilingFlag.DataBind();

 bcos this '<%# sample() %>' code will be executed while calling databind()

Hello,


You can set the Checked property of CheckBox on PageLoad event like this:

<asp:CheckBox ID="chkPFCeilingFlag" runat="server" TabIndex="15" Enabled="true" CssClass="CheckBox"></asp:CheckBox>


<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">        If Not Page.IsPostBack Then</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">            chkPFCeilingFlag.Checked = sample()</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">        End If</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">    End Sub</div>

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        { 

              chkPFCeilingFlag.Checked = sample();

        }

    }


Please mark as Answer if it helps you.....Nitesh Garg

As the two people above me have stated, when you use <%# %>, it will perform these actions on the databinding event for the containing control.

I don't see why you would actually want to do it like this and not in the code behind, as this just seems to be a sloppy method to achieve whatever it is that you want.

Seeing as this isn't a dynamic control inside a template for a gridview/dataview/... you should probably just set the value in the page_load event.

you have to call chkPFCeilingFlag.DataBind() in page Load event

thanks


As per looking your code I dont understand what is the requiremet to write this kind of code in aspx page that you can achieve easily by writing it in page_load event.

 

But anyways...  Checked='<%# sample() %>' this want works to achieve from aspx page you can do it from javascript by following code.

function AlertMessage() {


            if ('<%=sample() %>' == 'True');
            {
               document.getElementById('chkPFCeilingFlag').checked = 1;
                

            }
        }


After that you need to call this function after in your page but make sure call this function in after your control load mean write this function exection code after the checkbox conrols aspx code (by line no)

 

Mark as answer if this answer for your question

nice.it is worked.if that function is in another one class then how can we call it.


If your current problem has been solved, mark the thread as resolved or mark the post as an answer. Start a new thread for new question.

The javascript method is not the best approach, as you know what you want to render, and it won't work is javascript is turned of.

server controls (those with a runat=server) only support binding expressions. if you want to use binding expressions, for a page where the controls are not in a repeater, etc, in page load you can just call:

  Page.DataBind();

if you want more control, (say you have a repeater), just wrap the controls you want to databind, in a panel, then call databind() on the panel in page load.

  pnlName.DataBind();

of course you could just call databind on the one control.

 cntName.DataBind()

and any expressions on it will work.