datagrid editcommand question

B

bill yeager

I'm doing an inline edit of an Admin table using the
Datagrid. There are three columns in my Admin table. I
want to dynamically disable the corresponding "CareType"
field (see below) in the grid if the record is part of an
existing relationship, so the user can't update it.
The "CareTypeID" field is the key and is invisible to the
user.

Here is the db table columns:
CareTypeID
CareType
CareTypeDesc

Here is my code in the DataGrid1.EditCommand event:
<code>
Dim intRecdsFnd As Integer
intRecdsFnd = ValidateChild.ValidateChildTable
("CallType", DataGrid1.DataKeys(e.Item.ItemIndex), CType
(Application("strDataSource"), String))
Dim txtCareType As TextBox = DirectCast
(e.Item.FindControl("txtCareType"), TextBox)
If intRecdsFnd = 0 Then
txtCareType.Enabled = True
Else
txtCareType.Enabled = False
End If
ViewState.Item("intRecdsFnd") = Convert.ToInt32
(intRecdsFnd)
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindData()
</code>

When executing the following line above in order to
disable the field that I want,
<code>
Dim txtCareType As TextBox = DirectCast(e.Item.FindControl
("txtCareType"), TextBox)
</code>
the value comes back as "Nothing"!

Here is my corresponding code in the HTML of the aspx page
for the column:
<code>
<asp:TemplateColumn HeaderText="CareType">

<ItemTemplate>

<asp:Label runat="server" Text='<%# DataBinder.Eval
(Container, "DataItem.CareType") %>'>

</asp:Label>

</ItemTemplate>

<FooterTemplate>

<asp:TextBox id="txtFtrCareType" Columns="20"
runat="server" MaxLength="30"></asp:TextBox>

</FooterTemplate>

<EditItemTemplate>

<asp:TextBox id=txtCareType Columns="20"
runat="server" MaxLength="20" Text='<%# DataBinder.Eval
(Container, "DataItem.CareType") %>'>

</asp:TextBox>

<asp:RequiredFieldValidator
id="Requiredfieldvalidator1" runat="server"
ControlToValidate="txtCareType" ErrorMessage="CareType is
required!"

CssClass="invalid" Font-Bold="True" />

</EditItemTemplate>

</asp:TemplateColumn>
</code>

Just to see, I get a hit if I use an ID value for the
Label in the ItemTemplate, but I don't get a hit when
trying to get the value from the EditItemTemplate.
However, the Label value doesn't do me any good, since I
need to disable the field when the user presses the Edit
command.

I tried moving the following line:
<code>
Dim txtCareType As TextBox = DirectCast(e.Item.FindControl
("txtCareType"), TextBox)
</code>

after the BindData() routine (which executes the
DataGrid1.ItemDataBound event), but still didn't help.


How come I am not getting a hit on the ID value of the
textbox during the EditCommand?
 
B

bill yeager

I solved the problem by moving the code into the
ItemDataBound event like so:

[ code ]
If e.Item.ItemType = ListItemType.EditItem Then 'Check
whether or not to enable the CareType
Dim intRecdsFnd As Integer
intRecdsFnd =
ValidateChild.ValidateChildTable("CallType",
DataGrid1.DataKeys(e.Item.ItemIndex), CType(Application
("strDataSource"), String))
Dim txtCareType As TextBox = DirectCast
(e.Item.FindControl("txtCareType"), TextBox)
If intRecdsFnd = 0 Then
txtCareType.Enabled = True
Else
txtCareType.Enabled = False
End If
End If
[ / code ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top