C# 动态表:按单元格将按钮链接到文本框

C# 动态表:按单元格将按钮链接到文本框,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,我有一个动态表,其中每一行都有一个文本框(txtCantitate)和一个按钮(btnMinus)。在文本框中,我有数量(int),在按钮上单击我希望数量减少一。这是我桌上的东西: 你能帮我做一下按钮的代码吗?问题是它是一个动态按钮。。。每个记录上都有相同的ID。。。我不知道怎么做 我在项目C#、.NET4.5、js和Jquery上使用的语言 cell = new HtmlTableCell(); HtmlInputButton btnMinus = new HtmlInputButton();

我有一个动态表,其中每一行都有一个文本框(txtCantitate)和一个按钮(btnMinus)。在文本框中,我有数量(int),在按钮上单击我希望数量减少一。这是我桌上的东西:

你能帮我做一下按钮的代码吗?问题是它是一个动态按钮。。。每个记录上都有相同的ID。。。我不知道怎么做

我在项目C#、.NET4.5、js和Jquery上使用的语言

cell = new HtmlTableCell();
HtmlInputButton btnMinus = new HtmlInputButton();
btnMinus.ID = "btnMinus";
btnMinus.Value = "-";
cell.Controls.Add(btnMinus);
row.Cells.Add(cell);

cell = new HtmlTableCell();
HtmlInputText txtCantitate = new HtmlInputText();
txtCantitate.ID = "txtCantitate";
txtCantitate.Value = publicatie.Cantitate.ToString();
cell.Controls.Add(txtCantitate);
row.Cells.Add(cell);
这是解决办法

Javascript

function MinusVal(ctrl)
{
    var TextBox = $(ctrl).parent().next().find("input[type=text]");
    var Value = parseInt(TextBox.val());

    TextBox.val(Value - 1);
    return false;
}
C#后端

btnMinus.Attributes.Add("onclick", "MinusVal(this);");

您需要在按钮上设置单击事件,该事件将执行您想要的操作:

您需要设置文本框和按钮的ID,以匹配您首先所在的行+单元格索引。。。由于这些是HTMLControl,您实际上没有它们的索引,因此您必须找到一种方法以某种方式将它们放入其中(对不起,我不会为您编写此代码)

然后您必须设置事件处理程序

服务器端单击事件处理程序setter(实际事件处理程序代码见下文):

客户端单击事件处理程序设置程序:

btnMinus.Attributes.Add("onclick","JavaScript:myButtonClick(this);");
如果要在服务器端执行事件处理程序代码:

private void myButtonClick(object sender, EventArgs e)
{
   Button tmp = sender as Button;
   string[] id = tmp.ID.Split(new string[]{"_"}, StringSplitOptions.None);
   string textbox_ID = "txtCantitate" + "_" + id[1] + "_" + id[2];
   TextBox txt = this.Controls.FindControl(textbox_ID) as TextBox;
   int val = -1;
   string finaltext = "";
   if(int.TryParse(txt.Text, out val))
      finaltext = (val-1).ToString();
   else
      finaltext = "Invalid number, Cannot decrement!";

   txt.Text = finaltext;
}
如果要在客户端执行事件处理程序代码:

function myButtonClick(object sender)
{
   //i'll let you figure this one out for yourself if you want to do it client-side, but it's very similar to the server-side one as far as logic is concerned... 
}

我想使用服务器端aproach,但“sender.ID.Split(新字符串[]{“{”},StringSplitOptions.None);”不起作用,无法识别ID。。。还有Ideeas?检查我刚刚在解决方案中发布的代码更改。。。你应该试着玩玩它。。。可能是Id而不是Id。。。ETC id解析正常,忘记发布。。。但现在我有另一个问题。。。FindControl无法识别,它始终为空;我尝试过:TextBox txt=this.Controls.FindControl(id)作为TextBox;con控制=FindControl(id);TextBox txt=FindControl(id)作为TextBox;HtmlInputText txt=(HtmlInputText)FindControl(id);什么都没有…我有一个名为“MeniulPrincipal.Master”的母版页,表名为“listaProduse”,它位于一个div中,在一个名为“contentplaceholder continue”的contentplaceholder中。。。我为…寻找解决方案。。。3个小时什么都没有…终于找到了解决办法。。。我必须首先调用该表:HtmlInputText txtBox=(HtmlInputText)listaProduse.FindControl(id);谢谢你的帮助!
private void myButtonClick(object sender, EventArgs e)
{
   Button tmp = sender as Button;
   string[] id = tmp.ID.Split(new string[]{"_"}, StringSplitOptions.None);
   string textbox_ID = "txtCantitate" + "_" + id[1] + "_" + id[2];
   TextBox txt = this.Controls.FindControl(textbox_ID) as TextBox;
   int val = -1;
   string finaltext = "";
   if(int.TryParse(txt.Text, out val))
      finaltext = (val-1).ToString();
   else
      finaltext = "Invalid number, Cannot decrement!";

   txt.Text = finaltext;
}
function myButtonClick(object sender)
{
   //i'll let you figure this one out for yourself if you want to do it client-side, but it's very similar to the server-side one as far as logic is concerned... 
}