Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 1st, 2006, 02:53 AM
Authorized User
 
Join Date: Jun 2006
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default Runtime EventHandler

Hi

I have one datagrid name as dg_unit on a form.

I am adding a new datagrid name as dg_diagnosis at runtime in dg_unit datagrid.

I am adding a new checkbox control at runtime in dg_diagnosis grid.

i am adding event handler to checkboxes.But event is not fired.

I give a code which is written by me.

If any correction or missing pls suggest me.

 For Each ditem In dg_diagnosis.Items
                Dim str As String = ditem.Cells(1).Text
                Dim chk As New CheckBox
                chk.ID = "chk"
                ditem.Cells(0).Controls.Add(chk)
                AddHandler chk.CheckedChanged, AddressOf chk_CheckedChanged
            Next

 Public Sub chk_CheckedChanged(ByVal sende As Object, ByVal e As System.EventArgs) Response.Write("hi")
    End Sub

Thanks
monika

 
Old December 1st, 2006, 03:46 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Monika

Looking at your code, I suggest that you set the AutoPostBack property of the checkbox to true, i.e. AutoPostBack = "True" which will fire the OnCheckedChanged event.

However, if you still face problems, let us know.

Regards
Mike

Fortune favours the brave, so don't regret on missed oppurtunities.
 
Old December 1st, 2006, 05:22 AM
Authorized User
 
Join Date: Jun 2006
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mike

Thanks for yrkind response.

As per yr suggestion,i set Autopostback property to true.

But my dg_diagnosis grid has been invisisble and my original grid as dg_unit is coming.

I am not getting what is excetly problem?

if u have any ans pls reply me

Thanks
monika

 
Old December 1st, 2006, 05:33 AM
Authorized User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi , U cannot handle the events of the controls inside the datagrid directly.The child control's events are bubbled upto datagrid. So write eventhandler for datagrid's itemcommand.Then find which conrol fired event ,then resond accordingly

Good Luck

 
Old December 1st, 2006, 06:01 AM
Authorized User
 
Join Date: Jun 2006
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Thanks for yr kind response.

I also got yr point.But can u give me some example how i do that?

I also try to fire my child datagrid cotrol event but it's also not working.

So,if possible u have code.Pls help me

Thanks
monika

 
Old December 1st, 2006, 06:47 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by SriRamaKrishna
 Hi , U cannot handle the events of the controls inside the datagrid directly.The child control's events are bubbled upto datagrid. So write eventhandler for datagrid's itemcommand.Then find which conrol fired event ,then resond accordingly

Good Luck

Hey I do not agree with ur point, I know we have to find the controls inside a datagrid, cast them and then retrive their values like for a textbox, but you can also assign direct method calls to child controls like OnClick etc. For e.g. you can set the OnCheckedChanged on of a checkbox directly to a method like monika is doing, set the autopostback to true and that event will fire.

As far as monika your problem is concerned, you must be missing out something, just let us know ur exact approach while creating the child control and if possible the code, so that we can rectify the issue.

Regards
Mike

Fortune favours the brave, so don't regret on missed oppurtunities.
 
Old December 1st, 2006, 07:26 AM
Authorized User
 
Join Date: Jun 2006
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mike

I am so much confused now.

I give all the details and code.ok i hope u getting some solution.

I have one datagrid name as dg_unit which is on my page.It contains two fields.one is database field and second is linkbutton.

when i click on my linkbutton,my itemcommand event of dg_unit is fired.In this event when i click on link button,runtime datagrid is created name as dg_diagnosis.It contains three fields.One is template column as checkbox,second and third is database field.

Now i want when i click on checkbox,I want values of corresponding checkbox.

Can u tell me which event is fired when i click on checkbox?otherwise any other solution can u give me after reading my whole problem.I have found eventhandler.But it is not working.so I give u a code.if u have some idea,pls tell me.

  Private Sub dg_unit_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles dg_unit.ItemCommand
        'Dim edit As String = "true"
        'Dim index As Integer
        'index = e.Item.ItemIndex
        'DataGrid1.EditItemIndex = e.Item.ItemIndex
        Dim SelectString As String = e.Item.Cells(0).Text
        Me.ViewState.Add("SelectString", SelectString)
        DRequestobj = New mast_diagnosisrequest
        Dim conn As New SqlConnection
        Dim Tran As SqlTransaction
        Dim sqlrdr As SqlDataReader
        Try
            conn = DRequestobj.InitializeConnection()
            Tran = conn.BeginTransaction
            sqlrdr = DRequestobj.FetchRelatedDetails(conn, Tran, Me.ViewState("SelectString"))

            Dim div1 As New HtmlControls.HtmlGenericControl("div")
            'Dim dg_diagnosis As New DataGrid
            div1.ID = "div1"
            div1.Controls.Add(dg_diagnosis)
            div1.Style("OVERFLOW") = "Auto"
            div1.Style("WIDTH") = "350px"
            div1.Style("HEIGHT") = "350px"

            Dim col0 As New TemplateColumn
            col0.HeaderText = "Code"
            dg_diagnosis.Columns.Add(col0)
            dg_diagnosis.Columns(0).Visible = True

            dg_diagnosis.HeaderStyle.CssClass = "DataGridFixedHeader"

            Dim ColorObj As Color = New Color
            Dim BrdStyle As BorderStyle = New BorderStyle
            Dim Unt As Unit = New Unit
            Dim FntUnt As FontUnit = New FontUnit
            Dim i As Long
            dg_diagnosis.Font.Name = "Verdana"
            dg_diagnosis.CellPadding = 2
            dg_diagnosis.CellSpacing = 0
            dg_diagnosis.GridLines = GridLines.Both
            dg_diagnosis.PagerStyle.Mode = PagerMode.NextPrev
            dg_diagnosis.HeaderStyle.Font.Name = "Verdana"
            dg_diagnosis.HeaderStyle.Font.Size = FntUnt.Smaller
            dg_diagnosis.BorderStyle = BorderStyle.Solid
            dg_diagnosis.BorderWidth.Pixel(2)
            dg_diagnosis.HeaderStyle.BackColor = ColorObj.FromArgb(248, 248, 131)
            dg_diagnosis.HeaderStyle.ForeColor = ColorObj.FromArgb(157, 30, 68)
            dg_diagnosis.ItemStyle.Font.Size = FntUnt.Smaller
            dg_diagnosis.BorderColor = ColorObj.FromArgb(24, 178, 48)
            dg_diagnosis.AlternatingItemStyle.Font.Size = FntUnt.Smaller
            dg_diagnosis.HeaderStyle.Font.Bold = True
            dg_diagnosis.SelectedItemStyle.Height = Unt.Pixel(1)
            dg_diagnosis.ItemStyle.BackColor = ColorObj.FromArgb(233, 235, 233)
            dg_diagnosis.AlternatingItemStyle.BackColor = ColorObj.LightYellow
            dg_diagnosis.ItemStyle.Height = Unt.Pixel(1)
            dg_diagnosis.ItemStyle.ForeColor = ColorObj.Black
            dg_diagnosis.FooterStyle.BorderColor = ColorObj.FromArgb(24, 178, 48)
            dg_diagnosis.Height = Unt.Pixel(1)
            dg_diagnosis.DataSource = sqlrdr
            dg_diagnosis.DataBind()
            e.Item.Cells(1).Controls.Add(div1)

            Dim ditem As DataGridItem
            For Each ditem In dg_diagnosis.Items
                Dim str As String = ditem.Cells(1).Text
                Dim chk As New CheckBox
                chk.ID = "chk"
                AddHandler chk.CheckedChanged, AddressOf chk_CheckedChanged
                ditem.Cells(0).Controls.Add(chk)
            Next

            'AddHandler dg_diagnosis.ItemCommand, AddressOf dg_diagnosis_ItemCommand

            sqlrdr.Close()
            Tran.Commit()
            DRequestobj.ReleaseConnection(conn)
        Catch ex As Exception
            Tran.Rollback()
            Label1.Text = ex.Message
        Finally
            DRequestobj.ReleaseConnection(conn)
        End Try
end sub

Public Sub chk_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chk.CheckedChanged
    ' Response.Write("hi")
    'End Sub

what i miss in this code,i don't know.

Thanks
monika


 
Old December 1st, 2006, 08:22 AM
Authorized User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Monika , ok , This there is solution. U can create
event handler for checkbox indise griview in the itemcreated eventhabdler of datagrid.

eg.

private void grid_ItemCreated( object sender,
  System.Web.UI.WebControls.DataGridItemEventArgs e)
 {
  ListItemType elemType = e.Item.ItemType;
  if ((elemType == ListItemType.Item)||(elemType == ListItemType.AlternatingItem))
  {
   CheckBox ch = (CheckBox) e.Item.FindControl("cbExample");
   ch.CheckedChanged += new EventHandler(OnChangeHandler);
  }
 }

Think This works.
Good Luck

 
Old December 1st, 2006, 08:59 AM
Authorized User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If u want to tweak more , Follow this link
http://www.geekswithblogs.net/sgreen.../12/14871.aspx


 
Old December 1st, 2006, 09:54 AM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

well my friend SriRamaKrishna, try to understand monika's problem.

Well monika, by looking at your code, I got the root problem, since you are creating the child datagrid at runtime, once you click on the checkbox with autopostback set to true, the page will load again, i.e. page will be submitted and in that case the grid that you created at runtime will disappear and your parent grid will be bound to the normal state, i.e. without child grid, now again if you will click your linkbutton, child grid with checkbox will be created, but on submit of page, the runtime grid will be lost,

to test this just drag a server side button to your page, click on the linkbutton that will generate the child grid at runtime, now when you click on the server side button, you will notice that your parent grid is bound to the normal state & the runtime grid disappears.

The only & suitable resolution I find is to call some javascript function on checkbox click, like setting some values in a hidden control and manipulate accordingly,

in case of any further doubts, plz revert.

Regards
Mike

Fortune favours the brave, so don't regret on missed oppurtunities.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Guide on ProgressChange EventHandler of a Browser sachin635 C# 2008 aka C# 3.0 0 September 15th, 2008 04:21 AM
Java without runtime? MOON22 Java Databases 1 October 24th, 2007 04:42 AM
How to get control in runtime. Somesh C# 2005 1 March 24th, 2007 02:23 PM
EventHandler and GDI+ Function Fightfish C# 1 June 21st, 2005 08:02 AM
eventhandler in VBA for Word nesplb Pro VB 6 0 July 30th, 2003 06:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.