.NET Framework - Get part of string
Asked By DavidC
12-Mar-10 09:53 AM
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';
--
David
MyTextBox.ClientID
(1)
CheckBox
(1)
GridView
(1)
ASP.NET
(1)
TextBox
(1)
FindControl
(1)
StrRaw.lastIndexOf
(1)
MyCheckBox
(1)
Mark Rae [MVP] replied to DavidC
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 does not represent a very good solution - what are you
actually trying to do...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
DavidC replied to Mark Rae [MVP]
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.
--
David
Mark Rae [MVP] replied to DavidC
[please do not top-post]
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 is 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 will not compile, but should hopefully be close enough to
demonstrate the technique...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Is ASP.NET engine the same as CLR .NET Framework Hello! In a book that I read it says The ASP.NET engine execute the code when the a web request is received. So my question is if ASP.NET engine is the same as CLR because it is CLR that is executing the code and not the ASP.NET engine or I am I wrong here ? / / Tony C# Discussions ASP.NET (1) NET framework
How to launch a Windows application from a ASP.Net web application .NET Framework Hello Community I have successfully created a C# Windows application with SQL Server in the back end. I have also successfully created an ASP.NET web application using C# and SQL Server in th back end. Any more applications will be created with ASP.Net. I do not want to rewrite the Windows application so I am curious, from the ASP.NET application if I create a put a button or a link on the ASP.NET
asp.net runtime understanding .NET Framework Hello! I read in a book and the text say the into action. The client request a file, for example default.aspx from the server. All ASP.NET Web pages usually have the file extension .aspx. Because this file extension is registered with IIS, or known by the Visual Web Devepoper Web server, the ASP.NET runtime and the ASP.NET worker process get into the picture. With the first request to the file default.aspx
Is IIS 6.0 with ASP.NET unable to do a HTTP compression? .NET Framework Is IIS 6.0 with ASP.NET unable to do a HTTP compression? I asked our supplier to implement a HTTP compression gzip or deflate) for the web pages generated using ASP.NET running on IIS (the server reply says: X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727"). The supplier insists it is not possible with ASP
me to a starters tutorial for asp / c# I am looking for the basics. Zach. ASP.NET Discussions WebMatrix (1) ASP.NET (1) Arne Pearltree Supportsupport (1) VB (1) PM Zach (1) Visual (1) Regards Zach (1) PM (1) Online: http: / / www.asp.net / get-started http: / / www.w3schools.com / aspnet / default.asp Books: Maybe one of Wrox'es ASP.NET books. Arne - - Pearltree Support support@pearltree.org 0031 85 7730573 www.pearltree.org Saw the