0

On my asp.net form, within a table row I add cells and within those cells I add textboxes with an attribute like -

string residentId = (new GUID()).ToString();
string houseId  = (new GUID()).ToString();

HtmlTableRow detailRow = new HtmlTableRow();
detailRow.Attributes.Add("residentId", residentId);
detailRow.Attributes.Add("houseId", houseId);

HtmlTableCell dataCell = new HtmlTableCell();

TextBox tb = new TextBox();
tb.Attributes.Add("componentname", "tbMoveInDate");

tb.Attributes.Add("onchange", "updateMoveInDate()";

cell.Controls.Add(tb);
detailRow.Cells.Add(dataCell);

with a possiblility of 100+ rows, all with distinct IDs of course.

within my javascript I have this function -

function updateMoveInDate() {
    var MoveInDateEle = $("[componentname*='tbMoveInDate']");
}

at this point MoveInDateEle is a collection of all of those text boxes within the table and

var currentRow = $("[componentname*='tbMoveInDate']").parent().parent();

gives me all of the rows. but not my specific row.

How is it possible to get the specific text box I am working with and the specific resident Id and house Id associated with that control?

1
  • Are you clicking on the row you are working with or what does working with mean? Will the textbox be focused? Would you mind explaining a bit more. Thanks Commented Jun 28, 2013 at 12:38

3 Answers 3

2

Modify the C# code like this:

tb.Attributes.Add("onchange", "updateMoveInDate(this)";

and the js function like:

// obj here refers to the current textbox in the scope
// on which the on-change event had occurred...
function updateMoveInDate(obj) {
    var currentRow = $(obj).closest('tr');
}
Sign up to request clarification or add additional context in comments.

1 Comment

@edhedges That's because OP is using $("[componentname*='tbMoveInDate']") which refers to all the textboxes but we are using here this keyword, which refers to the element in the current scope, not to the full collection.
1

You could do

tb.Attributes.Add("onchange", "updateMoveInDate(this)";

And

function updateMoveInDate(txt) {
    var $txt = $(txt);
    var $row = $txt.closest('tr');
    var residentId = $row.attr('residentId');
    var houseId = $row.attr('houseId');
}

Comments

0

Try this

tb.Attributes.Add("onchange", "updateMoveInDate(this)";

in code behind

    string residentId = (new GUID()).ToString();
    string houseId  = (new GUID()).ToString();

    HtmlTableRow detailRow = new HtmlTableRow();
    detailRow.Attributes.Add("residentId", residentId);
    detailRow.Attributes.Add("houseId", houseId);

    HtmlTableCell dataCell = new HtmlTableCell();

    TextBox tb = new TextBox();
    tb.Attributes.Add("componentname", "tbMoveInDate");

    tb.Attributes.Add("onchange", "updateMoveInDate(this)";

    cell.Controls.Add(tb);
    detailRow.Cells.Add(dataCell);

jQuery:

function updateMoveInDate(args) {
    var MoveInDateEle = $(args).closest('tr');

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.