Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TextBox ID and clientId inside of DataGrid

Status
Not open for further replies.

cjburkha

Programmer
Jul 30, 2004
76
0
0
US
Hi all,

I have tapped into the Item_Created event of DataGrid to customize it a little more how I would like.

In the footer (If e.Item.ItemType = ListItemType.Footer Then)
I am adding two TextBoxes and a button, and I would like to validate the data in the textboxes when the button is clicked, and I would like to do this with javascript.

So, here is what I did:
Code:
''add the textbox
                Dim addEvent As TextBox = New TextBox
                addEvent.Rows = 2
                addEvent.ID = "addEvent"

                addEvent.Attributes.Add("onclick", "if (this.value == 'Enter the event') this.value = '';")
                addEvent.Text = "Enter the event here"
                e.Item.Cells(3).Controls.Add(addEvent)

''add the button
 Dim addButton As Button = New Button
		            addButton.Text = "Add"
		            addButton.ID = "addButton"
		            AddHandler addButton.Click, AddressOf bnAddEvent
		            addButton.CommandName = "Button_clicked"
		            addButton.Attributes.Add("onclick", "validateAddEvent('" + addEvent.uniqueId + "','" + addDes.ClientId + "' );")
		            e.Item.Cells(5).Controls.Add(addButton)

However, the output html is not quite as I would expect or like

id="DG_Events__ctl4_addEvent"
but addEvent.uniqueId does not resolve to id="DG_Events__ctl4_addEvent" but simply addEvent as seen here:
onclick="validateAddEvent('addEvent','addDes' );"
ClientId resolves to the same thing.

I have a feeling that I am calling ClientId too soon? The other things are not being added yet to clientId?

Can anyone shed some light on why clientID and uniqueId are resolving to Id?

Thanks for you help

CJB
 
>>addEvent

is the unique id, when it is added to another control the parent controls's name is simply appended to this control's name.

i would suggest that u use the onblur event of the textfield to validate the field (like the onclick event). another approach would be to set the textbox to a variable like so:
.Attributes.Add("onclick", "if (this.value == 'Enter the event') this.value = '';TheTextBox=this;")

now in ur Submit button:
addButton.Attributes.Add("onclick", "validateAddEvent(TheTextBox,'" + addDes.ClientId + "' );")

now in ur validateAddEvent u will have the text box object itself...



Known is handfull, Unknown is worldfull
 
Thanks for the reply.

is the unique id, when it is added to another control the parent controls's name is simply appended to this control's name.

Thats what I thought I was doing here
e.Item.Cells(3).Controls.Add(addEvent)

Which I call before I add Attributes.Add to the button.

i would suggest that u use the onblur event of the textfield to validate the field
I have default text in the TB, and I want make sure the user doesn't hit 'add' before they changed the default text. If they don't change the text, onblur will never fire.

.Attributes.Add("onclick", "if (this.value == 'Enter the event') this.value = '';TheTextBox=this;")

now in ur Submit button:
addButton.Attributes.Add("onclick", "validateAddEvent(TheTextBox,'" + addDes.ClientId + "' );")

This is an excellent solution, and I will use it, although I think getting ClientId correct would be a little more elegent.

Thanks for your help

CJB
 
>>I have default text in the TB, and I want make sure the user doesn't hit 'add' before they changed the default text. If they don't change the text, onblur will never fire

Wrong, the onblur event will fire always when the focus leaves the textbox (what u r refering to is the onchange evet)...

Known is handfull, Unknown is worldfull
 
Edit:

[.Attributes.Add("onclick", "if (this.value == 'Enter the event') this.value = '';TheTextBox=this;")

now in ur Submit button:
addButton.Attributes.Add("onclick", "validateAddEvent(TheTextBox,'" + addDes.ClientId + "'

Actually, I didn't read quite close enough, as the onclick event will never fire. That is the check I am trying to do, I want to make sure the user entered some text.

I like the idea though, and will try to apply it in some other way.
 
Wrong, the onblur event will fire always when the focus leaves the textbox (what u r refering to is the onchange evet)

Sure, I understand that, but I am trying to check for the instance where the user never enters the TB at all, they just skip over it and click the add button.
 
in that case the TheTextBox will be undefined, try this:

function validateAddEvent(TheTextBxObject,SecondParam)
{
if(typeof(TheTextBxObject)=="undefined")
{
alert("Hey, whats the big idea missing the textbox???")
}
}

Known is handfull, Unknown is worldfull
 
Thank you for your reply, that would work just fine.

However, I found a different way to do it. I called attributes.add during the datagrid.load event, and by then the ClientId was what I expected

Code:
DG_Loaded(ByVal sender As Object, ByVal e As EventArgs)
       		 Dim addButton as Button = ctlHolder.FindControl("addButton")
   			 addButton.Attributes.Add("onclick", "validateAddEvent('" + eventNameCtrl.ClientId + "','" + eventDesCtrl.ClientId + "' );")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top