【问题标题】:How can I change my Button's text dynamically based on my Gridview Row click?如何根据我的 Gridview Row 单击动态更改按钮的文本?
【发布时间】:2019-04-23 15:32:56
【问题描述】:

我正在使用 ASP.NET C#。我有一个 Gridview(下面显示的示例gridview)和一个按钮。当我点击 gridview 的行时,我的按钮文本需要根据 gridview 的信息而改变。

例如:如果我点击第一行,我的 Button 的文本应该是 Alpha(操作名称)。如果我点击第三行,它应该有查理在它上面等等。用户可以多次单击任何行,并且每次新单击时按钮都会发生变化。

我已经学会了使用 gridview 的 onRowDataBound 和 SelectedIndexChanged 事件来处理 gridview 中的值。我能够使用标签打印出 gridview 的每一行。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView2, "Select$" + e.Row.RowIndex);
                e.Row.ToolTip = "Click to select this row. ";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowIndex == GridView1.SelectedIndex)
                {
                    row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
                    row.ToolTip = string.Empty;
                    String ID;
                    alertNo = row.Cells[0].Text;
                    String Operation;
                    Operation = row.Cells[1].Text;
                    String Month;
                    Month = row.Cells[2].Text
                }
                else
                {
                    row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
                    row.ToolTip = "Click to select this row";
                }
            }

        Label1.Visible = true;
        Label2.Visible = true;
        Label3.Visible = true;

        }

但是我不明白如何使用此方法更改按钮的文本。

任何帮助将不胜感激(Javascript/JQuery/C#)。如果问题仍然不清楚,请告诉我。

【问题讨论】:

  • 欢迎来到 Stack Overflow!按钮上是否有可用作参考的 ID?您是否希望通过 Javascript 做到这一点?如果是这样,您可以通过 Id 找到元素并操作文本。

标签: javascript c# jquery


【解决方案1】:

在您的 C# 代码中,您可以尝试以下操作:

String Operation;
Operation = row.Cells[1].Text;
Button1.Text = Operation; // Add this line just after you get the Operation value

你可以用 Javascript/jQuery 做得更好/更快,但是因为你已经有大部分使用 C#。

编辑 1:这是一个工作的纯 jQuery 示例https://jsfiddle.net/gy31Lufr/2/

【讨论】:

    【解决方案2】:

    你的 HTML 应该有一个与每个按钮相关联的 id 属性,就像这样;

    <button id="button2" name="Click button two!" type="button" > Click button two! </button>
    ...
    <button id="buttonN" name="Click button N!" type="button" > Click button N! </button> ```
    
    Now in the JQuery code , you can do a DOM traversal ,example;
    $("#button1").click(function(){
      this.attr("name","Alpha (Operation name)");
    });
    
    $("#button2").click(function(){
      this.attr("name","Charlie on it");
    });
    
    This is a solution based on HTML and Jquery , the key ingredient is for every button to have an unique ID.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 2019-09-11
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多