Datagrid Setting Focus and "ontextchanged"

D

Derek Mendez

I am trying to add records to a dataset using the footer of datagrid.
That works fine.

Additional functionality I am having a hard time with is performing a
query based on the text entered in the first textbox of the footer. I
would like to use an “ontextchanged” to initiate the query. I can do
this on a textbox on the webform outside the datagrid but cannot seem to
associate the footer - textbox to perform the operation.

Also I want to reset the focus to the first textbox in the footer after
the “add” button action is performed. I have tried with JavaScript I
think my issue is with assigning the textbox id to associate the action
with.

Any help would be greatly appreciated. Thanks in advance
 
J

Jeffrey Tan[MSFT]

Hi Derek,

Based on my understanding, you meet the problem of manipulating the child
control(TextBox) in DataGrid

To add the OnTextChanged server side event handler for your child TextBox
control in datagrid, you should hook into DataGrid.ItemCreated event, in
the event find your TextBox, do like this:

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGridItem dgi=e.Item;
if(dgi.ItemType==ListItemType.Footer)
{
Control c=dgi.FindControl("TextBox's ID");
TextBox tb=c as TextBox;
tb.TextChanged+=new EventHandler(tb_TextChanged);
}
}

If you did not specify the server side ID to the TextBox, you have to loop
through and find it, like this:

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGridItem dgi=e.Item;
if(dgi.ItemType==ListItemType.Footer)
{
foreach(Control c in dgi.Cells[0].Controls)
{
if(c is TextBox)
{
TextBox tb=c as TextBox;
tb.TextChanged+=new EventHandler(tb_TextChanged);
}
}
}
}

For your second request of set the textfocus, I think the key point is get
the correct client id of your TextBox. You should refer to the TextBox's
ClientID property, for example, in ItemCreated event, you may get it
through tb.ClientID.

==============================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Derek Mendez

Thanks for the response.

This almost gets me there. I am having a couple of problems though…

I do not have textchanged available for an aspx(html view) page – I know
its available for a windows app I may not have clarified that in my
original post.

When setting the ontextchanged event for the textbox in html to call the
sub I am getting a “delegate” error. Do I have to use the ItemCreated
Sub or can I incorporate this code into another sub with a Javascript on
textchange??

Again, any help would be greatly appreciated
 
D

Derek Mendez

This looks ugly I know everything works except the setting focus on the
textbox at the end of this sub. I added in your code and called the
javascript to set focus.

Also I was referring to correct clientID for the textbox but it was
never recognizing it. Maybe my javascript is incorrect.

Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand

If e.CommandName = "Delete" Then

Dim intlid As Integer

intlid = DataGrid1.DataKeys(e.Item.ItemIndex)

SqlDeleteCommand1.Parameters("@lid").Value = intlid

SqlConnection1.Open()

SqlDeleteCommand1.ExecuteNonQuery()

SqlConnection1.Close()

DataGrid1.EditItemIndex = -1

BindcallNo()

End If

If e.CommandName = "Add" Then

Dim fyshort As String

Dim newid As String

Dim fyear As String

Dim fycn As String

Dim txtcn As TextBox

Dim strcn As String

txtcn = e.Item.FindControl("txt_add_cn")

strcn = txtcn.Text

Dim txtname As TextBox

Dim strname As String

Dim datcurrent As Date

Dim ydate As String

Dim nextyr As Date

datcurrent = Date.Now()

nextyr = DateAdd(DateInterval.Year, 1, datcurrent)

ydate = DatePart(DateInterval.Year, datcurrent)

If DatePart(DateInterval.Month, datcurrent) < 7 Then

fyear = Mid(ydate, 3, 2)

Else

fyear = Mid(nextyr, 3, 2)

End If

fyshort = lblFYtxt.Text

txtname = e.Item.FindControl("txt_add_cnname")

strname = txtname.Text

fycn = fyear + strcn


Label1.Visible = True

Label5.Visible = False

Label6.Visible = False

Label7.Visible = False

'Label9.Visible = True

lblduplicatename.Visible = False

lblduplicatecn.Visible = False

lblduplicatedate.Visible = False

lblduplicatehotel.Visible = False

lblduplicateinvoice.Visible = False

lblduplicatePOC.Visible = False

lblduplicatephone.Visible = False

Label12.Visible = False

Label13.Visible = False

lblduplicatename.Text = ""

lblduplicatecn.Text = ""

lblduplicatedate.Text = ""

lblduplicatehotel.Text = ""

lblduplicateinvoice.Text = ""

Label4.Visible = False

Label8.Visible = False


SqlSelectCommand6.Parameters("@cn").Value = strcn

SqlConnection1.Open()

Dim myReader As SqlDataReader =
SqlSelectCommand6.ExecuteReader()

If myReader.HasRows Then

Label1.Text = "The duplicate information is listed
below"



'lblduplicatehotel.Visible = True

'Do While myReader.Read()

'Console.WriteLine(myReader.GetString(0) & " " &
myReader.GetString(1) & " " & myReader.GetString(5))

myReader.Read()

If Not myReader.IsDBNull(0) Then

lblduplicatename.Text = myReader.GetValue(0)

Else

lblduplicatename.Text = "no data"

End If

If Not myReader.IsDBNull(1) Then

lblduplicatecn.Text = myReader.GetValue(1)

Else

lblduplicatecn.Text = "no data"

End If

If Not myReader.IsDBNull(2) Then

lblduplicatedate.Text = myReader.GetValue(2)

Else

lblduplicatedate.Text = "no data"

End If

If Not myReader.IsDBNull(4) Then

lblduplicatehotel.Text = myReader.GetValue(4)

Else

lblduplicatehotel.Text = "no data"

End If

If Not myReader.IsDBNull(5) Then

lblduplicateinvoice.Text = myReader.GetValue(5)

Else

lblduplicateinvoice.Text = "no data"

End If

If Not myReader.IsDBNull(6) Then

lblduplicatePOC.Text = myReader.GetValue(6)

Else

lblduplicatePOC.Text = "no data"

End If



If Not myReader.IsDBNull(7) Then

lblduplicatephone.Text = myReader.GetValue(7)

Else

lblduplicatephone.Text = "no data"

End If

'lblduplicatehotel.Text = myReader.GetString(5)

'Loop

Label5.Visible = True

Label6.Visible = True

Label7.Visible = True

'Label9.Visible = True



lblduplicatename.Visible = True

lblduplicatecn.Visible = True

lblduplicatedate.Visible = True

lblduplicatehotel.Visible = True

lblduplicateinvoice.Visible = True

lblduplicatePOC.Visible = True

lblduplicatephone.Visible = True

Label4.Visible = True

Label8.Visible = True

Label12.Visible = True

Label13.Visible = True

' txtCN.Text = ""

myReader.Close()

SqlConnection1.Close()

Else

myReader.Close()

SqlConnection1.Close()

Label1.Text = "there are no duplicates for this call
number"



End If

If Label1.Text = "there are no duplicates for this call
number" Then



SqlInsertCommand2.Parameters("@FY").Value = fyshort

SqlInsertCommand2.Parameters("@name").Value = strname

SqlInsertCommand2.Parameters("@cn").Value = strcn

SqlInsertCommand2.Parameters("@fycn").Value = fycn

SqlInsertCommand2.Parameters("@ltid").Value = 112
'CType(lbllastid.Text, Integer)

SqlConnection1.Open()

SqlInsertCommand2.ExecuteNonQuery()

SqlConnection1.Close()



BindcallNo()

e.Item.TabIndex = 1



End If

Dim dgi As DataGridItem

dgi = e.Item

If dgi.ItemType = ListItemType.Footer Then

Dim c As Control

c = dgi.FindControl("txt_add_cn")

Dim tb As TextBox

tb = c

lbl_clientid.Text = tb.ClientID()



txtname.Text = ""

txtcn.Text = ""

RegisterStartupScript("focus", "<script
language=""JavaScript"">" & vbCrLf & _

vbTab & "form1." & tb.ClientID & ".focus();" & _

vbCrLf & vbTab & "form1." & tb.ClientID & ".select();" & _

vbCrLf & "<" & "/script>")

End If

'Page.RegisterClientScriptBlock("focus", _

' "<script language=""JavaScript"">" & _

' "form1.<txt_add_cn>.focus();" & _

' "</script> ")



End If



End Sub
 
J

Jeffrey Tan[MSFT]

Hi Derek,

I have found that this post has been posted in this group. I have added a
reply to you in that post, please follow up me there. I will help you.

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Derek Mendez

Hi Jeffrey

Thanks for the response. I never recieved the attachment you sent with
you earlier reply. Thanks in advance.

Derek
 
J

Jeffrey Tan[MSFT]

Hi Derek,

Thanks for your feedback.

You should get and access my attachment through Outlook Express. You can
not see it through IE explorer.

I have tried to use OE to that reply, and I succeeded get the attachment. I
think you should refer to that reply through OE again.

If you still can not get it, please give me a valid e-mail address, I will
sent the project to you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Derek,

Have you refered my attached project? Is your problem resolved?

Please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Derek Mendez

I am still unable to recieve your attached file. If you can, please
send to (e-mail address removed)

Thanks in advance..

Derek
 
J

Jeffrey Tan[MSFT]

Hi Derek,

Oh, I think it is very strange, because I can successfully get the
attachment through OE.

Anyway, I have sent the entire project to you through the E-mail you
provided. Please refer to it.

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Derek,

Based on your feedback, I have re-sent the project to you after changing
the zip extension.

Please refer to it, and feel free to feedback here. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Derek,

Have you received my e-mail? Does it make sense to you?

Please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top