Get part of string

  • Thread starter Thread starter DavidC
  • Start date Start date
D

DavidC

I have an asp.net application that dumps out a GridView from a datasource. I
want to access the controls on the rows via Javascript at the client rather
than use Ajax. For example I have a control id below as example of a table
row:

gvChecks_ctl02_txtClearedAmount

I am using js below but it only gives me text up to the first underscore. I
want to be able to dig out everything between the 2 underscores. In above
example I need to return 'ctl02'. Can someon help? Thanks.

var sid = obj.id;
var unloc = sid.indexOf('_', 0);
var vrowid = sid.substr(0, unloc);
var sctlid = 'gvChecks_' + vrowid + '_txtClearedAmount';
 
gvChecks_ctl02_txtClearedAmount

I am using js below but it only gives me text up to the first underscore.
I
want to be able to dig out everything between the 2 underscores. In above
example I need to return 'ctl02'. Can someone help? Thanks.

The JavaScript you need is:

var strRaw = 'gvChecks_ctl02_txtClearedAmount';
var intFIO = strRaw.indexOf('_');
var intLIO = strRaw.lastIndexOf('_');
var strExtract = strRaw.substring(intFIO + 1, intLIO);

However, this probably doesn't represent a very good solution - what are you
actually trying to do...?
 
I have a GridView where I want to check a checkbox and have it fill a textbox
on the same row with a value in another textbox in the same row. This is for
a bank reconciliation so the cleared amount should default to the check
amount when the box is checked. I am doing this in read-only mode
(ITEMTEMPLATE) of the GridView.

I tried your suggestion and it worked because I know the first and last part
of the control ID name. Is there a better way as I may use Ajax UpdatePanel
in the future and not sure if this would break my code. Thanks.
 
[please don't top-post]
I have a GridView where I want to check a checkbox and have it fill a
textbox
on the same row with a value in another textbox in the same row. This is
for
a bank reconciliation so the cleared amount should default to the check
amount when the box is checked. I am doing this in read-only mode
(ITEMTEMPLATE) of the GridView.

I tried your suggestion and it worked because I know the first and last
part
of the control ID name. Is there a better way as I may use Ajax
UpdatePanel
in the future and not sure if this would break my code.

In my experience, evaluating the munged control names is *ALWAYS* the wrong
solution. As you correctly say, many things can cause these to change,
thereby almost certainly breaking the code.

I'd almost certainly use AJAX for this.

Alternatively, it's possible to assign custom attributes to web controls.
That would mean that you use the RowBound event of the GridView to assign
the name of the "related" TextBox to each CheckBox e.g.

e.Row.Cells[5].FindControl("MyCheckBox").Attributes.Add("TextBox",
MyTextBox.ClientID);

N.B. the above line won't compile, but should hopefully be close enough to
demonstrate the technique...
 
Back
Top