【问题标题】:Dynamic Table: Link button to textbox cell by cell动态表:逐个单元格地将按钮链接到文本框
【发布时间】:2014-05-07 19:03:11
【问题描述】:

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

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

我在项目中使用的语言 C#、.NET 4.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);

【问题讨论】:

    标签: c# javascript jquery asp.net


    【解决方案1】:

    你需要在按钮上设置一个点击事件,它会执行你想要的动作:

    您需要设置文本框和按钮的 ID 以匹配您首先所在的行+单元格索引...因为这些是 HtmlControls,您实际上并没有它们的索引,所以您将拥有想办法把这些东西放进去(对不起,我不会给你写代码)。

    btnMinus.ID = "btnMinus_" + CurrentRowIndex.ToString() + "_" + CurrentCellIndex.ToString();
    txtCantitate.ID = "txtCantitate_" + CurrentRowIndex.ToString() + "_" + CurrentCellIndex.ToString();
    

    那么你将不得不设置事件处理程序...

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

    btnMinus.Click += myButtonClick;
    

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

    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... 
    }
    

    【讨论】:

    • 我想使用服务器端的方法,但是 "sender.ID.Split(new string[] { "_" }, StringSplitOptions.None);"不工作,它无法识别 ID ...和Ideas?
    • 检查我刚刚在我的解决方案中发布的代码更改...您应该尝试使用它...可能是 Id 而不是 ID...等
    • id 解析正常,忘记发帖了...但现在我有另一个问题... FindControl 无法识别它总是为空;我试过: TextBox txt = this.Controls.FindControl(id) as TextBox;控制 con = FindControl(id); TextBox txt = FindControl(id) as TextBox; HtmlInputText txt = (HtmlInputText)FindControl(id);什么都没有……
    • 我有一个名为“MeniulPrincipal.Master”的母版页,该表名为“listaProduse”,位于 div 中,位于名为“ContentPlaceHolderContinut”的内容占位符中......我搜索了一个解决方案... 3 小时,一无所获...
    • 终于找到了解决方案...我不得不先调用表格:HtmlInputText txtBox = (HtmlInputText)listaProduse.FindControl(id);感谢您的所有帮助!
    【解决方案2】:

    这是解决方案,

    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);");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-18
      • 2012-10-19
      • 1970-01-01
      • 2017-07-23
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多