我有一个asp.net网站,我通过动态方式在DataGrid中创建演示文件。 接下来,我必须检查数量动态字段中的值是否为空或不是数字,或者是否定等。
我想在点击发送按钮时检查它,我想使用jQuery脚本来执行此操作;问题是:如何检索动态TextBox的id?在html页面中,每个都有一个前缀,我不想使用循环。 例如:
protected void DettOrdGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{...
e.Row.Cells.Add(cellQta); //here i add a cell to the row of the grid
...
}
在html页面中:
$(document).ready(function){
$("#btnSaveProvvisorio").delegate("textbox", "change", function(){
var jtbNumBollaQuin;
jtbNumBollaQuin=$('#<%=myTextBoxQta.ClientID%>').text();
alert("Valore quantita : " + jtbNumBollaQuin);
})
}
错误是:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'myTextBoxQta' does not exist in the current context
谢谢!
Here the aspx.net code:
protected void DettOrdGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
myTextBoxQta = new TextBox();
myTextBoxQta.ID = "myTextBoxQta";
myTextBoxQta.Attributes.Add("nomeTextBox", "myTextBoxQta");
myTextBoxQta.Attributes.Add("runat", "server");
myTextBoxQta.Attributes.Add("onChange", "javascript:TotaliOrdDett();");
myTextBoxQta.Style["text-align"] = "center";
i = e.Row.Cells.Count;
i = i - 1;
//Column label
if (e.Row.RowIndex == -1)
{
if (risultato > 0)
{...
}
else //...add a textbox
{
myOracleConnection = myDbClass.dbConnessione("myconn");
//System.Diagnostics.Debug.WriteLine("Cella: " + e.Row.Cells[0].Text);
myQuery = "SELECT QTA_NR, KG_LORDI FROM TABLE WHERE SOC='1' AND ORD=" + ordHidden.Value + " AND ROW_ORD=" + e.Row.Cells[0].Text;
qtaKgInseriti = myDbClass.EseguiSqlSelect(myQuery, myOracleConnection, qtaKgInseriti);
myDbClass.dbDisconnessione(myOracleConnection);
e.Row.Cells[i].Controls.Add(myTextBoxQta);
cellQta.Controls.Add(myTextBoxQta);
e.Row.Cells.Add(cellQta);
//myTextBoxQta.Text = e.Row.RowIndex.ToString();
if (risultato > 0)
{
myTextBoxQta.Text = qtaKgInseriti.Tables[0].Rows[0]["QTA_NR"].ToString();
txtSumQta.Text = (Int32.Parse(txtSumQta.Text) + Int32.Parse(myTextBoxQta.Text)).ToString();
}
e.Row.Cells[i].Controls.Add(myTextBoxKg);
cellKg.Controls.Add(myTextBoxKg);
e.Row.Cells.Add(cellKg);
//myTextBoxKg.Text = e.Row.RowIndex.ToString();
if (risultato > 0)
{
myTextBoxKg.Text = qtaKgInseriti.Tables[0].Rows[0]["KG_LORDI"].ToString();
txtSumKg.Text = (Double.Parse(txtSumKg.Text) + Double.Parse(myTextBoxKg.Text)).ToString();
}
myCompValNr = new CompareValidator();
myCompValNr.ID = "myCompValNr";
myCompValNr.ErrorMessage = "<div style=\"font-size: x-small; text-align: center; color: #FF0000\">Please enter a number grater than zero!</div>";
myCompValNr.ControlToValidate = "myTextBoxQta";
myCompValNr.Type = ValidationDataType.Integer;
myCompValNr.Operator = ValidationCompareOperator.GreaterThan;
myCompValNr.ValueToCompare = "0";
myCompValNr.Display = ValidatorDisplay.Dynamic;
e.Row.Cells[i + 1].Controls.Add(myCompValNr);
myReqFvNr = new RequiredFieldValidator();
myReqFvNr.ControlToValidate = myTextBoxQta.ID;
myReqFvNr.Text = "<div style=\"font-size: x-small; text-align: center; color: #FF0000\">Field Qta cannot be null!</div>";
e.Row.Cells[i + 1].Controls.Add(myReqFvNr);
myCompValKg = new CompareValidator();
myCompValKg.ID = "myCompValKg";
myCompValKg.ErrorMessage = "<div style=\"font-size: x-small; text-align: center; color: #FF0000\">Please enter a number grater than zero!</div>";
myCompValKg.ControlToValidate = "myTextBoxKg";
myCompValKg.Type = ValidationDataType.Double;
myCompValKg.Operator = ValidationCompareOperator.GreaterThan;
myCompValKg.ValueToCompare = "0";
myCompValKg.Display = ValidatorDisplay.Dynamic;
e.Row.Cells[i + 2].Controls.Add(myCompValKg);
myReqFvKg = new RequiredFieldValidator();
myReqFvKg.ControlToValidate = myTextBoxKg.ID;
myReqFvKg.Text = "<div style=\"font-size: x-small; text-align: center; color: #FF0000\">Field Kg cannot be null!</div>";
e.Row.Cells[i + 2].Controls.Add(myReqFvKg);
}
}
谢谢克里斯和其他人, 我找到了解决方案:
$(document).ready(function () {
$("#btnSaveProvvisorio").click(function () {
var i;
var jtbNumBollaQuin;
$('.MyTextBoxClass').each(function(i,v){
jtbNumBollaQuin = $(this).val();
alert("Valore quantita : " + jtbNumBollaQuin);
});
});
});
并设置myTextBoxQta.CssClass =“MyTextBoxClass”;在aspx.cs文件中 和ClientIDMode =“静态”到aspx文件中的btnSaveProvvisorio按钮。
非常感谢! 伊戈尔
答案 0 :(得分:0)
您可以使用data attribute
之类的
<textarea data-clientId="#<%=myTextBoxQta.ClientID%>" ></textarea>
<强>脚本强>
$(document).ready(function){
$("#btnSaveProvvisorio").delegate("textbox", "change", function(){
var jtbNumBollaQuin;
jtbNumBollaQuin=$($(this).data('clientId')).text();
alert("Valore quantita : " + jtbNumBollaQuin);
});
});// check end of document.ready()
同时检查您网页中是否存在id
<%=myTextBoxQta.ClientID%>
。
答案 1 :(得分:0)
页面上不存在名为myTextBoxQta
的ASP.NET控件...它可能在控件的标记上没有runat="server"
。
使用.on
代替.delegate
。
在查看代码之后,您应该通过CSS类来测试代码的工作原理。
myTextBoxQta.CssClass = "MyTextBoxClass";
则...
$(document).ready(function){
$("#btnSaveProvvisorio").delegate("textbox", "change", function(){
var jtbNumBollaQuin;
jtbNumBollaQuin=$('.MyTextBoxClass').text();
alert("Valore quantita : " + jtbNumBollaQuin);
})
}