客户端上的jQuery更改ID,并且在服务器上不再可以找到控件?

Posted

技术标签:

【中文标题】客户端上的jQuery更改ID,并且在服务器上不再可以找到控件?【英文标题】:jQuery change id on client, and control can no longer be found on server? 【发布时间】:2012-09-13 20:35:04 【问题描述】:

我有一个网格视图,用户可以在其中添加新行。在添加行时,id 会像这样更改..

$('<div class=".grdLoaded">').load("GridRow.htm", function () 
                    // insert the html
$(_InsertionID).after($(this).html());
// now change the default id(s) that were present on GridRow.htm
// to something that represent current value(s)
$('#grdEntry').find('#grdEntry_ctl00_lblSNO').attr('id', 'SNo_' + _insCurRowNumber + '');
$('#grdEntry').find('#grdEntry_ctl00_lblQty').attr('id', 'Qty_' + _insCurRowNumber + '');
$('#grdEntry').find('#grdEntry_ctl00_lblItemName').attr('id', 'ItemName_' + _insCurRowNumber + '');
$('#grdEntry').find('#grdEntry_ctl00_lblProcess').attr('id', 'Prc_' + _insCurRowNumber + '');
$('#grdEntry').find('#grdEntry_ctl00_lblRemarks').attr('id', 'Remarks_' + _insCurRowNumber + '');
$('#grdEntry').find('#grdEntry_ctl00_lblColor').attr('id', 'Color_' + _insCurRowNumber + '');
$('#grdEntry').find('#grdEntry_ctl00_lblAmount').attr('id', 'Amount_' + _insCurRowNumber + '');

insCurRowNumber 是正在添加的当前行 ID,第一次是 1,第二次是 2,依此类推。 问题是如果我尝试访问服务器上的任何这些控件,像这样

((Label)grdEntry.Rows[i].FindControl("ItemName_1").Text;

或活动,如果这样的话

Page.FindControl("ItemName_1")

我收到了null reference exception。似乎问题是服务器端找不到这些动态生成的 ID。如果这是原因,我该如何规避呢?如果不是原因,那么是什么导致了这种行为,我该如何找到这些控件来访问它们的值?

编辑:响应 Steven Hunt,请求采用 post 格式,这是密钥的集合。

    Request.Form.AllKeys    string[177]   string[]
    [0] "__EVENTTARGET" string
    [1] "__EVENTARGUMENT"   string
    [2] "__LASTFOCUS"   string
    [3] "__VIEWSTATE"   string
    [4] "__EVENTVALIDATION" string
    [5] "check" string
    [6] "txtDueDate"    string
    [7] "txtTime"   string
    [8] "txtCustomerName"   string
    [9] "grdEntry$ctl01$txtQty" string
    [10]    "grdEntry$ctl01$txtName"    string
    [11]    "grdEntry$ctl01$txtProcess" string
    [12]    "grdEntry$ctl01$txtRate"    string
    [13]    "grdEntry$ctl01$txtRemarks" string
    [14]    "tags"  string
    [15]    "grdEntry$ctl01$txtColor"   string
    [16]    "txtRemarks"    string
    [17]    "drpCheckedBy"  string
    [18]    "btnSaveBooking"    string
    [19]    "txtCurrentDue" string
    [20]    "a" string
    [21]    "txtDiscount"   string
    [22]    "txtDiscount_TextBoxWatermarkExtender_ClientState"  string
    [23]    "txtDiscountAmt"    string
    [24]    "txtSrTax"  string
    [25]    "txtTotal"  string
    [26]    "txtAdvance"    string
    [27]    "txtBalance"    string
    [28]    "txtItemName"   string
    [29]    "txtItemSubQty" string
    [30]    "txtNewItemName"    string
    [31]    "txtItemCode"   string
    [32]    "hdntemp"   string
    [33]    "hdnItemCode"   string
    [34]    "txtProcessCode"    string
    [35]    "txtProcessName"    string
    [36]    "txtCNameSearch"    string
    [37]    "txtAddress"    string
    [38]    "txtPhoneNo"    string
    [39]    "grdCustomerSearch$ctl02$lnkBtnCustomerCode"    string
    [40]    "grdCustomerSearch$ctl03$lnkBtnCustomerCode"    string
    [41]    "grdCustomerSearch$ctl04$lnkBtnCustomerCode"    string
    [42]    "grdCustomerSearch$ctl05$lnkBtnCustomerCode"    string
    [43]    "grdCustomerSearch$ctl06$lnkBtnCustomerCode"    string
    [44]    "grdCustomerSearch$ctl07$lnkBtnCustomerCode"    string
    [45]    "grdCustomerSearch$ctl08$lnkBtnCustomerCode"    string
    [46]    "grdCustomerSearch$ctl09$lnkBtnCustomerCode"    string
    [47]    "grdCustomerSearch$ctl10$lnkBtnCustomerCode"    string
    [48]    "grdCustomerSearch$ctl11$lnkBtnCustomerCode"    string
    [49]    "grdCustomerSearch$ctl12$lnkBtnCustomerCode"    string
    [50]    "grdCustomerSearch$ctl13$lnkBtnCustomerCode"    string
    [51]    "grdCustomerSearch$ctl14$lnkBtnCustomerCode"    string
    [52]    "grdCustomerSearch$ctl15$lnkBtnCustomerCode"    string
    [53]    "grdCustomerSearch$ctl16$lnkBtnCustomerCode"    string
    [54]    "grdCustomerSearch$ctl17$lnkBtnCustomerCode"    string
    [55]    "grdCustomerSearch$ctl18$lnkBtnCustomerCode"    string
    [56]    "grdCustomerSearch$ctl19$lnkBtnCustomerCode"    string
    [57]    "grdCustomerSearch$ctl20$lnkBtnCustomerCode"    string
    [58]    "grdCustomerSearch$ctl21$lnkBtnCustomerCode"    string
    [59]    "grdCustomerSearch$ctl22$lnkBtnCustomerCode"    string
    [60]    "grdCustomerSearch$ctl23$lnkBtnCustomerCode"    string
    [61]    "grdCustomerSearch$ctl24$lnkBtnCustomerCode"    string
    [62]    "grdCustomerSearch$ctl25$lnkBtnCustomerCode"    string
    [63]    "grdCustomerSearch$ctl26$lnkBtnCustomerCode"    string
    [64]    "grdCustomerSearch$ctl27$lnkBtnCustomerCode"    string
    [65]    "grdCustomerSearch$ctl28$lnkBtnCustomerCode"    string
    [66]    "grdCustomerSearch$ctl29$lnkBtnCustomerCode"    string
    [67]    "grdCustomerSearch$ctl30$lnkBtnCustomerCode"    string
    [68]    "grdCustomerSearch$ctl31$lnkBtnCustomerCode"    string
    [69]    "grdCustomerSearch$ctl32$lnkBtnCustomerCode"    string
    [70]    "grdCustomerSearch$ctl33$lnkBtnCustomerCode"    string
    [71]    "grdCustomerSearch$ctl34$lnkBtnCustomerCode"    string
    [72]    "grdCustomerSearch$ctl35$lnkBtnCustomerCode"    string
    [73]    "grdCustomerSearch$ctl36$lnkBtnCustomerCode"    string
    [74]    "grdCustomerSearch$ctl37$lnkBtnCustomerCode"    string
    [75]    "grdCustomerSearch$ctl38$lnkBtnCustomerCode"    string
    [76]    "grdCustomerSearch$ctl39$lnkBtnCustomerCode"    string
    [77]    "grdCustomerSearch$ctl40$lnkBtnCustomerCode"    string
    [78]    "grdCustomerSearch$ctl41$lnkBtnCustomerCode"    string
    [79]    "grdCustomerSearch$ctl42$lnkBtnCustomerCode"    string
    [80]    "grdCustomerSearch$ctl43$lnkBtnCustomerCode"    string
    [81]    "grdCustomerSearch$ctl44$lnkBtnCustomerCode"    string
    [82]    "grdCustomerSearch$ctl45$lnkBtnCustomerCode"    string
    [83]    "grdCustomerSearch$ctl46$lnkBtnCustomerCode"    string
    [84]    "grdCustomerSearch$ctl47$lnkBtnCustomerCode"    string
    [85]    "grdCustomerSearch$ctl48$lnkBtnCustomerCode"    string
    [86]    "grdCustomerSearch$ctl49$lnkBtnCustomerCode"    string
    [87]    "grdCustomerSearch$ctl50$lnkBtnCustomerCode"    string
    [88]    "grdCustomerSearch$ctl51$lnkBtnCustomerCode"    string
    [89]    "grdCustomerSearch$ctl52$lnkBtnCustomerCode"    string
    [90]    "grdCustomerSearch$ctl53$lnkBtnCustomerCode"    string
    [91]    "grdCustomerSearch$ctl54$lnkBtnCustomerCode"    string
    [92]    "grdCustomerSearch$ctl55$lnkBtnCustomerCode"    string
    [93]    "grdCustomerSearch$ctl56$lnkBtnCustomerCode"    string
    [94]    "grdCustomerSearch$ctl57$lnkBtnCustomerCode"    string
    [95]    "grdCustomerSearch$ctl58$lnkBtnCustomerCode"    string
    [96]    "grdCustomerSearch$ctl59$lnkBtnCustomerCode"    string
    [97]    "grdCustomerSearch$ctl60$lnkBtnCustomerCode"    string
    [98]    "grdCustomerSearch$ctl61$lnkBtnCustomerCode"    string
    [99]    "grdCustomerSearch$ctl62$lnkBtnCustomerCode"    string
    [100]   "grdCustomerSearch$ctl63$lnkBtnCustomerCode"    string
    [101]   "grdCustomerSearch$ctl64$lnkBtnCustomerCode"    string
    [102]   "grdCustomerSearch$ctl65$lnkBtnCustomerCode"    string
    [103]   "grdCustomerSearch$ctl66$lnkBtnCustomerCode"    string
    [104]   "grdCustomerSearch$ctl67$lnkBtnCustomerCode"    string
    [105]   "grdCustomerSearch$ctl68$lnkBtnCustomerCode"    string
    [106]   "grdCustomerSearch$ctl69$lnkBtnCustomerCode"    string
    [107]   "grdCustomerSearch$ctl70$lnkBtnCustomerCode"    string
    [108]   "grdCustomerSearch$ctl71$lnkBtnCustomerCode"    string
    [109]   "grdCustomerSearch$ctl72$lnkBtnCustomerCode"    string
    [110]   "grdCustomerSearch$ctl73$lnkBtnCustomerCode"    string
    [111]   "grdCustomerSearch$ctl74$lnkBtnCustomerCode"    string
    [112]   "grdCustomerSearch$ctl75$lnkBtnCustomerCode"    string
    [113]   "grdCustomerSearch$ctl76$lnkBtnCustomerCode"    string
    [114]   "grdCustomerSearch$ctl77$lnkBtnCustomerCode"    string
    [115]   "grdCustomerSearch$ctl78$lnkBtnCustomerCode"    string
    [116]   "grdCustomerSearch$ctl79$lnkBtnCustomerCode"    string
    [117]   "grdCustomerSearch$ctl80$lnkBtnCustomerCode"    string
    [118]   "grdCustomerSearch$ctl81$lnkBtnCustomerCode"    string
    [119]   "grdCustomerSearch$ctl82$lnkBtnCustomerCode"    string
    [120]   "grdCustomerSearch$ctl83$lnkBtnCustomerCode"    string
    [121]   "grdCustomerSearch$ctl84$lnkBtnCustomerCode"    string
    [122]   "grdCustomerSearch$ctl85$lnkBtnCustomerCode"    string
    [123]   "grdCustomerSearch$ctl86$lnkBtnCustomerCode"    string
    [124]   "grdCustomerSearch$ctl87$lnkBtnCustomerCode"    string
    [125]   "grdCustomerSearch$ctl88$lnkBtnCustomerCode"    string
    [126]   "grdCustomerSearch$ctl89$lnkBtnCustomerCode"    string
    [127]   "grdCustomerSearch$ctl90$lnkBtnCustomerCode"    string
    [128]   "grdCustomerSearch$ctl91$lnkBtnCustomerCode"    string
    [129]   "txtExtraProcess1"  string
    [130]   "txtExtraRate1" string
    [131]   "txtExtraProcess2"  string
    [132]   "txtExtraRate2" string
    [133]   "txtWholeRemark"    string
    [134]   "drpTitle"  string
    [135]   "txtCName"  string
    [136]   "txtCAddress"   string
    [137]   "txtMobile" string
    [138]   "drpPriority"   string
    [139]   "txtNewPriority"    string
    [140]   "txtAreaLocaton"    string
    [141]   "txtRemarks1"   string
    [142]   "txtBDate"  string
    [143]   "txtADate"  string
    [144]   "hdnMAC"    string
    [145]   "hdnEditItemId" string
    [146]   "hdnOption" string
    [147]   "hdnCustId" string
    [148]   "hdnUpdate" string
    [149]   "BranchId"  string
    [150]   "hdnItems"  string
    [151]   "hdnDataValues" string
    [152]   "hdnFirstAdd"   string
    [153]   "hdnCustomerNameFocusOut"   string
    [154]   "hdnCurrentValue"   string
    [155]   "hdnCurrentGrossAmt"    string
    [156]   "isInEditMode"  string
    [157]   "hdnDefaultItem"    string
    [158]   "hdnDefaultProcess" string
    [159]   "hdnDefaultItemProcessRate" string
    [160]   "hdnTaxBefore"  string
    [161]   "hdnAllDiscount"    string
    [162]   "hdnAllTax" string
    [163]   "hdnUrgentRateApplied"  string
    [164]   "hdnAllDiscountFocusOut"    string
    [165]   "isNetAmountInDecimal"  string
    [166]   "hdnRecompTax"  string
    [167]   "hdnRecompDiscount" string
    [168]   "hdnCustCode"   string
    [169]   "hdnTaxAmtRecomp"   string
    [170]   "hdnDisAmtRecomp"   string
    [171]   "hdnBalance"    string
    [172]   "hdnTotal"  string
    [173]   "hdnDiscountValue"  string
    [174]   "hdnGrdRowCount"    string
    [175]   "hdnAllGridData"    string
    [176]   "hiddenInputToUpdateATBuffer_CommonToolkitScripts"  string

它只包含网格标题的数据(那些带有grdEntry$ctl01$txtQty 的值,而不是行的数据。但是,我找到了解决方法,将所有变量存储在一个临时的数组并将其传递给服务器,工作完成! 不过感谢您的努力。

【问题讨论】:

【参考方案1】:

首先,“id”属性不会在回发时被发送回服务器,这就是“name”属性的用途。其次,您需要在 Request.Form(用于发布)或 Request.QueryString(用于获取)集合中查找您的数据,因为它们不会绑定到控件。 ASP .net 必须参与控件的实例化,当您在 javascript 中创建它们时,它们不会神奇地存在于回发中。

【讨论】:

以上是关于客户端上的jQuery更改ID,并且在服务器上不再可以找到控件?的主要内容,如果未能解决你的问题,请参考以下文章

jquery datatables更新数据到服务器,更新完成后,修改页面上的信息

jQuery 动态更改按钮上的图标或文字

jQuery TableSorter 仅适用于 ID(数字字段),不适用于文本、日期等其他字段

如何在 JQuery 数据表中重置分页

如何更改 OpenCart 产品页面上的原始价格?

jQuery怎样获取并且修改P标签内的值