|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dynamically created control in Datagrid problemI am using the ITEMDATABOUND event to add a control to the template column, if column (3) has a value of 'checkox' then add the checkbox control else, add a textbox control to the template column. That part works fine, the user can enter data. My problem is getting the data from that dynamically created cell. I'm using a button_click event in hopes of cycling through the datagrid and retrieving the template column values to write back to the dataset. The only reference I know of is the e argument, but I cannot expose it in my custom routine. How can I reference the template column, it does not have an ID and the FINDCONTROL so far has been ineffective for me. Hi Wanky,
You can use datagrid.Items(rowIndex).Cells(colIndex).Contrls(0[or 1)) to get the reference. Or you can assign ID to a control when you add it, so late you can get it. BTW, it's better to add control in ItemCreated event. HTH Elton Wang elton_w***@hotmail.com >-----Original Message----- the template column,>I created a datagrid with 4 bound columns and 1 template column. >I am using the ITEMDATABOUND event to add a control to >if column (3) has a value of 'checkox' then add the through the datagridcheckbox >control else, add a textbox control to the template column. > >That part works fine, the user can enter data. > >My problem is getting the data from that dynamically created cell. >I'm using a button_click event in hopes of cycling >and retrieving the template column values to write back to the dataset.> cannot expose it>The only reference I know of is the e argument, but I >in my custom routine. an ID and the> >How can I reference the template column, it does not have Show quoteHide quote >FINDCONTROL so far has been ineffective for me. > >-- >Message posted via http://www.dotnetmonster.com >. > thanks,
I'll try that. I sent an email with an attachment of the print screen. I can't seen to grad the right event handler in the item created event. Which language do you use, c# or vb.net?
Show quoteHide quote >-----Original Message----- >thanks, >I'll try that. >I sent an email with an attachment of the print screen. > >I can't seen to grad the right event handler in the item >created event. > >-- >Message posted via http://www.dotnetmonster.com >. > Elton,
I get the following error when I tried this code: For y = 0 To maxrec If DataGrid1.Items(y).Cells(4).Controls(0) Is GetType (TextBox) Then Dim str As String = CType(DataGrid1.Items(y).Cells(4).Controls(0), TextBox).Text() End If y += 1 Next Specified argument was out of the range of valid values. Parameter name: index Followng code snippet shows how to loop thru datagrid
items (rows) to get control reference: Dim ctrl As Control For Each item As DataGridItem In datagrid.Items ctrl = item.Cells(4).Controls(0) ' or try Controls(1) depending on how to you add it ' If you assign ctrl ID, you can ctrl = item.FindControl("ctrlID") Dim ctrlType As String = item.Cells(2).Text If ctrlType.Equals("Textbox") Then Dim txtBox As TextBox = CType(ctrl, TextBox) ' Process as text box Else Dim ck As CheckBox = CType(ctrl, CheckBox) ' process as checkbox End If ' Or you can dynamically find control type If TypeOf ctrl Is TextBox Then Dim txtBox As TextBox = CType(ctrl, TextBox) ' Process as text box Else Dim ck As CheckBox = CType(ctrl, CheckBox) 'process as checkbox End If Next HTH Elton Wang elton_w***@hotmail.com Show quoteHide quote I haven't tried your stuff yet, but this is how I created the controls.
For some reason the ID reference doesn't take. Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim str = RTrim(DataGrid1.DataSource.Tables (DataGrid1.DataMember).Rows(e.Item.DataSetIndex)(2)) Select Case RTrim(DataGrid1.DataSource.Tables (DataGrid1.DataMember).Rows(e.Item.DataSetIndex)(2)) Case "Textbox", "Other", "memo" Dim tb = New System.Web.UI.WebControls.TextBox e.Item.Cells(4).Controls.Add(tb) e.Item.Cells(4).ID = "Txtbx" Case "Checkbox" Dim cb = New System.Web.UI.WebControls.CheckBox e.Item.Cells(4).Controls.Add(cb) e.Item.Cells(4).ID = "Chkbx" End Select End If End Sub I prefer to use same ID for both TextBox and CheckBox, so
it is easy to get control first then to figure out what kind control it is. And it's better to use For Each loop rather than For index loop. HTH Elton >-----Original Message----- Object, ByVal e As>I haven't tried your stuff yet, but this is how I created the controls. >For some reason the ID reference doesn't take. > >Private Sub DataGrid1_ItemDataBound(ByVal sender As >System.Web.UI.WebControls.DataGridItemEventArgs) Handles System.Web.UI.WebControls.TextBox>DataGrid1.ItemDataBound > >If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = >ListItemType.AlternatingItem Then > Dim str = RTrim(DataGrid1.DataSource.Tables >(DataGrid1.DataMember).Rows(e.Item.DataSetIndex)(2)) > Select Case RTrim(DataGrid1.DataSource.Tables >(DataGrid1.DataMember).Rows(e.Item.DataSetIndex)(2)) > Case "Textbox", "Other", "memo" > Dim tb = New > e.Item.Cells(4).Controls.Add(tb) System.Web.UI.WebControls.CheckBox> e.Item.Cells(4).ID = "Txtbx" > Case "Checkbox" > Dim cb = New Show quoteHide quote > e.Item.Cells(4).Controls.Add(cb) > e.Item.Cells(4).ID = "Chkbx" > End Select > End If > End Sub > >-- >Message posted via http://www.dotnetmonster.com >. > Hello again,
I tried the suggested code, and I got the following error: Specified argument was out of the range of valid values. Parameter name: index ..controls(0) or .controls(1) both return the error. It seems that the control I created during the ITEMDATABOUND event doesn't really exist as far the datagrid is concerned. I mean, I can see it, enter data into, but I am finding it extremely difficult to extract that data out of the control When I try the ID route, the code doesn't error but the variable I used during the assignment returns a value of nothing. CODE AREA ******************************************************** Private Sub GetValues() Dim c As Control Dim dg As DataGridItem For Each dg In DataGrid1.Items c = dg.Cells(4).Controls(0) Dim ctlType As String = RTrim(dg.Cells(2).Text) If ctlType.Equals("Textbox") Then Dim tb As TextBox = CType(c, TextBox) Else Dim cb As CheckBox = CType(c, CheckBox) End If Next End Sub I'm not sure what causes the problem. It might be that
the .NET can't save dynamically created control data after data binding in viewstate (I just guess.). If that, you can statically add both TextBox and Checkbox (given different id, i.e. txtID, ckID) in cell(4), then when in ItemDataBound, set one invisible accordingly. In your GetValues method, using If ctlType.Equals("Textbox") Then Dim tb As TextBox = CType(dg.FindContrl("txtID"), TextBox) Else Dim cb As CheckBox = CType(dg.FindContrl("ckID"), CheckBox) End If HTH Elton Wang Show quoteHide quote >-----Original Message----- variable I used>Hello again, > >I tried the suggested code, and I got the following error: >Specified argument was out of the range of valid values. Parameter name: >index > >..controls(0) or .controls(1) both return the error. > >It seems that the control I created during the ITEMDATABOUND event >doesn't really exist as far the datagrid is concerned. >I mean, I can see it, enter data into, but I am finding it extremely >difficult to extract that data out of the control > >When I try the ID route, the code doesn't error but the Show quoteHide quote >during the assignment returns a value of nothing. > >CODE AREA ******************************************************** > Private Sub GetValues() > Dim c As Control > Dim dg As DataGridItem > For Each dg In DataGrid1.Items > c = dg.Cells(4).Controls(0) > Dim ctlType As String = RTrim(dg.Cells (2).Text) > If ctlType.Equals("Textbox") Then > Dim tb As TextBox = CType(c, TextBox) > Else > Dim cb As CheckBox = CType(c, CheckBox) > End If > Next > End Sub > >-- >Message posted via http://www.dotnetmonster.com >. > I have a hunch that viewstate maybe an area to concentrate on.
I'm starting to think that I'm losing state as I envoke the getvalues sub. At some point the control is recreated before I can trap the value. My problem is that this database will have over 200 data entry points, which could change depending on the user. I can think of no other way to efficiently create a form to handle the data points entry, other than a datagrid. Any ideas? I don't think there are big problems to get solution. In
one way or another we can find a means to get the reference of the control when the page is postback. >-----Original Message----- think of no other>I have a hunch that viewstate maybe an area to concentrate on. >I'm starting to think that I'm losing state as I envoke the >getvalues sub. At some point the control is recreated before >I can trap the value. > >My problem is that this database will have over 200 data entry >points, which could change depending on the user. I can >way to efficiently create a form to handle the data points entry, otherShow quoteHide quote Hi Wanky,
Following is solution. I'm sure it works. ' Add both textbox and checkbox in datagrid <asp:TemplateColumn HeaderText="Choice"> <ItemTemplate> <asp:CheckBox ID="ckChoice" Runat="server"></asp:CheckBox> <asp:TextBox ID="txtChoice" Runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateColumn> ' in ItemDataBound set one of control invisible according to type Dim drv As DataRowView = e.Item.DataItem If drv(FieldTypeIndex).ToString.Equals("Textbox") Then e.Item.FindControl("ckChoice").Visible = False Else e.Item.FindControl("txtChoice").Visible = False End If ' in save button Click retrieve control type depending on type filed For Each item As DataGridItem In Me.dgdSearch.Items If item.Cells(FieldTypeIndex).Text.Equals("Textbox") Then Dim txtCtrl As TextBox = CType(item.FindControl ("txtChoice"), TextBox) ' process Else Dim ck As CheckBox = CType(item.FindControl ("ckChoice"), CheckBox) ' process End If Next If you still have problem, please let me know. HTH Elton Wang >-----Original Message----- the template column,>I created a datagrid with 4 bound columns and 1 template column. >I am using the ITEMDATABOUND event to add a control to >if column (3) has a value of 'checkox' then add the through the datagridcheckbox >control else, add a textbox control to the template column. > >That part works fine, the user can enter data. > >My problem is getting the data from that dynamically created cell. >I'm using a button_click event in hopes of cycling >and retrieving the template column values to write back to the dataset.> cannot expose it>The only reference I know of is the e argument, but I >in my custom routine. an ID and the> >How can I reference the template column, it does not have Show quoteHide quote >FINDCONTROL so far has been ineffective for me. > >-- >Message posted via http://www.dotnetmonster.com >. >
Select button in datagrid
Datagrid Add new Row problkem Condition Edit Action Need Help in creating a HyperlinkButton in Datagrid How to properly Format zip code in a datagrid REPOST: MSDN UNIVERSAL NG SUPPORT, WHERE ARE YOU? generate DataGrid at Runtime..... PROBLEM !!!! GUI of Datagrid To stop text wraping in an datagrid asp.net 2003 MSDN UNIVERSAL NG SUPPORT: Where Are You?! <--- 3rd Re-Post |
|||||||||||||||||||||||